简体   繁体   English

Base64 编码到解码文件转换问题

[英]Base64 Encoded to Decoded File Conversion Problem

I am processing very large files (> 2Gig).我正在处理非常大的文件(> 2Gig)。 Each input file is Base64 encoded, andI am outputting to new files after decoding.每个输入文件都是Base64编码的,我解码后输出到新文件。 Depending on the buffer size (LARGE_BUF) and for a given input file, my input to output conversion either works fine, is missing one or more bytes, or throws an exception at the outputStream.write line (IllegalArgumentException: Last unit does not have enough bits).根据缓冲区大小 (LARGE_BUF) 和给定的输入文件,我的输入到输出转换要么工作正常,要么缺少一个或多个字节,要么在 outputStream.write 行抛出异常(IllegalArgumentException: Last unit does not have enough位)。 Here is the code snippet (could not cut and paste so my not be perfect):这是代码片段(无法剪切和粘贴,所以我不完美):

.
.

final int LARGE_BUF = 1024;
byte[] inBuf = new byte[LARGE_BUF];

try(InputStream inputStream = new FileInputStream(inFile); OutputStream outStream new new FileOutputStream(outFile)) {

   for(int len; (len = inputStream.read(inBuf)) > 0); ) {
      String out = new String(inBuf, 0, len);
      outStream.write(Base64.getMimeDecoder().decode(out.getBytes()));
   }
}

For instance, for my sample input file, if LARGE_BUF is 1024, output file is 4 bytes too small, if 2*1024, I get the exception mentioned above, if 7*1024, it works correctly.例如,对于我的示例输入文件,如果 LARGE_BUF 是 1024,则输出文件太小 4 个字节,如果是 2*1024,我得到上面提到的异常,如果是 7*1024,它可以正常工作。 Grateful for any ideas.感谢任何想法。 Thank you.谢谢你。

First, you are converting bytes into a String, then immediately back into bytes.首先,您将字节转换为字符串,然后立即转换回字节。 So, remove the use of String entirely.因此,完全删除 String 的使用。

Second, base64 encoding turns each sequence of three bytes into four bytes, so when decoding, you need four bytes to properly decode three bytes of original data.其次,base64编码将每个三个字节的序列都变成了四个字节,所以解码时需要四个字节才能正确解码三个字节的原始数据。 It is not safe to create a new decoder for each arbitrarily read sequence of bytes, which may or may not have a length which is an exact multiple of four.为每个任意读取的字节序列创建一个新的解码器是不安全的,它的长度可能是也可能不是 4 的精确倍数。

Finally, Base64.Decoder has a wrap(InputStream) method which makes this considerably easier:最后,Base64.Decoder 有一个wrap(InputStream) 方法,这使这变得相当容易:

try (InputStream inputStream = Base64.getDecoder().wrap(
    new BufferedInputStream(
        Files.newInputStream(Paths.get(inFile))))) {

    Files.copy(inputStream, Paths.get(outFile));
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM