简体   繁体   English

使用apache commons编解码器的Base64解码在非常大的二进制文件上失败

[英]Base64 decoding using apache commons codec failing on very large binary file

I am developing an encryption tool, and for our encrypted file format I am using Base64 to encode data. 我正在开发加密工具,对于我们的加密文件格式,我使用Base64来编码数据。 I am using apache commons codec to decode files using a Base64InputStream wrapped around a FileInputStream. 我正在使用apache commons编解码器使用围绕FileInputStream的Base64InputStream来解码文件。 This worked like a charm until I tested it on a large music file. 在我在大型音乐文件上测试之前,这就像一个魅力。 For some mysterious reason, when I did this, every byte after and including byte 6028 turned into 0. The code to read it into the byte[] follows: 出于一些神秘的原因,当我这样做时,字节6028之后的每个字节变为0.将其读入byte []的代码如下:

FileInputStream filein = new FileInputStream(filename);
Base64InputStream in = new Base64InputStream(filein,false,76,'\n');
byte[] contents = new byte[known_and_tested_correct_filelength];
in.read(contents);

Now, for whatever reason, after byte 6028, everything in contents is 0. However, contents.length is around 300,000 bytes. 现在,无论出于何种原因,在字节6028之后,内容中的所有contents都是0.但是, contents.length大约是300,000字节。 As you can guess, this did wonders for my application. 你可以猜到,这对我的应用程序来说确实很奇怪。 Does anyone have any inkling of what's going on? 有没有人知道发生了什么?

The semantics of in.read() is not to read ALL the bytes in the buffer provided, but to read as many as "are ready" and let you know how many that was. in.read()的语义不是读取提供的缓冲区中的所有字节,而是读取“准备好”的数量,并告诉您有多少。

You must then repeat the call to in.read() for the next chunk and the next etc until you get a -1. 然后,您必须重复调用in.read()以获取下一个块和下一个块,直到获得-1。

Your current code just gets the first chunk and you discard the size of the chunk. 您当前的代码只获得第一个块,并且您丢弃了块的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用org.apache.commons.codec.binary.base64对Java对象进行Base64编码? - How to Base64 encode a Java object using org.apache.commons.codec.binary.base64? 使用Apache Commons进行base64字符串解码 - base64 String decoding using Apache Commons Base64编码和解码Apache公用 - Base64 encoding and decoding apache commons Java:Commons编解码器和base64解码在服务器上不起作用 - Java: Commons-codec and base64 decoding not working on server java.lang.NoClassDefFoundError:org / apache / commons / codec / binary / Base64 - java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Base64 Selenium SouceLabs-java.lang.NoClassDefFoundError:org / apache / commons / codec / binary / Base64 - Selenium SouceLabs - java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Base64 NoClassFoundError:org.apache.commons.codec.binary/Base64 运行时异常 - NoClassFoundError: org.apache.commons.codec.binary/Base64 runtime exception BouncyCastle和Apache Commons Codec Base64编码之间的区别 - Difference between BouncyCastle and Apache Commons Codec Base64 encoding Android Base64编码和Apache编解码器解码 - Android Base64 Encoding and Apache codec decoding 如何使用 apache.commons.codec Base64 解密 Java 客户端中的字符串(在 .NET 中加密)? - How to decrypt string (encrypted in .NET) in Java client using apache.commons.codec Base64?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM