使用流式传输的 Android File To Base64 有时会丢失 2 个字节

2022-01-21 00:00:00 file base64 android java

我写信是为了寻求解决以下困难的正确解决方案:

i am writing to ask for the proper solution for solving the following difficulties:

我需要将文件编码为 Base64 格式,我无法将文件变小,因此我肯定会遇到 OutOfMemory 异常,这就是我使用 Streaming 方法解决它的原因.文件编码后,我立即通过代码和在线工具对其进行解码.有时会发现解码后的内容在文件末尾缺少 2 个字节,但并非总是如此.它确实影响了对文件的进一步处理.

I am required to encode the file in Base64 format, and there is no way for me to make the file small, as a result, i will surely suffered from OutOfMemory Exception, that why i used Streaming approach for solving it. After the file is encoded, i have decoded it immediately by code and also online-tools. The decoded content was found missing 2 bytes at the end of the file sometimes, but not always. It did affected further processing to the file.

希望有人可以提供帮助,可能是由于白痴错误造成的.还是谢谢.

Hope someone could help and may be caused by an idiot mistake. Still thanks.

代码如下:

FileOutputStream fout = new FileOutputStream(path + ".txt");
//this is for printing out the base64 content
FileInputStream fin = new FileInputStream(path);

System.out.println("File Size:" + path.getTotalSpace());

ByteArrayOutputStream os = new ByteArrayOutputStream();
Base64OutputStream base64out = new Base64OutputStream(os,Base64.NO_WRAP);

byte[] buffer = new byte[3 * 512];
int len = 0;
while ((len = fin.read(buffer)) >= 0) {
    base64out.write(buffer, 0, len);
}

System.out.println("Encoded Size:" + os.size());

String result = new String(os.toByteArray(), "UTF-8");

fout.write(os.toByteArray());
fout.close();
base64out.close();
os.close();
fin.close();

return result;

推荐答案

这里是在使用 Base64OutputStream 的情况下有效的解决方案.我将在代码之后解释其中的一些:

Here is the solution that works in the case of using Base64OutputStream. I will explain some of them after code:

代码如下:

FileOutputStream fout = new FileOutputStream(path + ".txt");
FileInputStream fin = new FileInputStream(path);

System.out.println("File Size:" + path.length());

ByteArrayOutputStream os = new ByteArrayOutputStream();
Base64OutputStream base64out = new Base64OutputStream(os,Base64.NO_WRAP);

byte[] buffer = new byte[3 * 512];
int len = 0;
while ((len = fin.read(buffer)) >= 0) {
    base64out.write(buffer, 0, len);
}

System.out.println("Encoded Size:" + os.size());

base64out.flush();
base64out.close();//this did the tricks. Please see explanation.

String result = new String(os.toByteArray(), "UTF-8");      

fout.write(os.toByteArray());
fout.flush();
fout.close();
os.close();
fin.close();

return result;

实际上,在使用 Dawnkeeper 的建议并添加了 flush() 之后,我只移动了一行代码.在处理任何数据之前,这些技巧是在 base64out.close() 中完成的.由于关闭 Base64OutputStream 可能会将 Base64 的 Ending Padding 写入输出.我的想法来自 org.apache.myfaces.trinidad.util.Base64OutputStream close() 方法.这就是我尝试的原因.

Actually, after using the suggestion from Dawnkeeper by added flush(), i have moved one line of code only. The tricks is did in the base64out.close() before processing any data. As closing the Base64OutputStream might write Ending Padding of Base64 to the Output. I got my idea from org.apache.myfaces.trinidad.util.Base64OutputStream close() method. That's why i tried.

在数据处理之前关闭 OutputStream 可能看起来很奇怪,但在这种情况下它只关闭 Base64OutputStream 而不是同时关闭 ByteArrayOutputStream.因此,数据仍然可以从 ByteArrayOutputStream 中检索.

It might seems strange closing the OutputStream before data processing, but it only close Base64OutputStream but not ByteArrayOutputStream at the same time in this case. Therefore, the data could still retrieve from ByteArrayOutputStream.

感谢 Dawnkeeper 的努力,希望这个问题可以帮助其他遭受 OutOfMemory 异常的人.

Thanks Dawnkeeper efforts, and hope this question could helps other suffered in OutOfMemory Exception.

相关文章