Android文件为Base64使用流有时错过了2个字节 [英] Android File To Base64 using streaming sometimes missed 2 bytes

查看:201
本文介绍了Android文件为Base64使用流有时错过了2个字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写信,要求解决以下难题得到妥善解决:

我需要连接code以Base64格式的文件,也没有办法,我做小的文件,作为一个结果,我肯定会从内存溢出异常苦,那为什么我用流媒体的方式进行解决它。
该文件是EN codeD之后,我有去codeD就立即被code和也在网上工具。解$ C $光盘含量被发现在该文件的末尾有时缺少2个字节,但并非总是如此。它没有受影响的进一步处理该文件。

希望有人能够帮助,并且可以通过一个白痴的错误造成的。还是感谢。

下面是code:

 的FileOutputStream FOUT =新的FileOutputStream(路径+名.txt);
//这是打印出的base64内容
的FileInputStream鳍=新的FileInputStream(路径);的System.out.println(文件大小:+ path.getTotalSpace());ByteArrayOutputStream OS =新ByteArrayOutputStream();
Base64OutputStream base64out =新Base64OutputStream(OS,Base64.NO_WRAP);字节[]缓冲区=新的字节[3 * 512];
INT LEN = 0;
而((LEN = fin.read(缓冲液))> = 0){
    base64out.write(缓冲液,0,LEN);
}的System.out.println(恩codeD尺寸:+ os.size());字符串结果=新的String(os.toByteArray(),UTF-8);fout.write(os.toByteArray());
fout.close();
base64out.close();
os.close();
fin.close();返回结果;


解决方案

下面是一个使用Base64OutputStream的情况下的工作方案。之后,code,我将解释其中的一些:

下面是code:

 的FileOutputStream FOUT =新的FileOutputStream(路径+名.txt);
的FileInputStream鳍=新的FileInputStream(路径);的System.out.println(文件大小:+ path.length());ByteArrayOutputStream OS =新ByteArrayOutputStream();
Base64OutputStream base64out =新Base64OutputStream(OS,Base64.NO_WRAP);字节[]缓冲区=新的字节[3 * 512];
INT LEN = 0;
而((LEN = fin.read(缓冲液))> = 0){
    base64out.write(缓冲液,0,LEN);
}的System.out.println(恩codeD尺寸:+ os.size());base64out.flush();
base64out.close(); //这个做的小伎俩。请参阅解释。字符串结果=新的String(os.toByteArray(),UTF-8);fout.write(os.toByteArray());
fout.flush();
fout.close();
os.close();
fin.close();返回结果;

其实,通过加齐平()使用建议从Dawnkeeper后,我提出的$ C $只有c一行。
招数的确是在处理任何数据之前base64out.close()。由于关闭Base64OutputStream可能会写的Base64结束的填充到输出。
我有我的想法,从<一个href=\"http://myfaces.apache.org/trinidad/trinidad-1_2/trinidad-api/apidocs/org/apache/myfaces/trinidad/util/Base64OutputStream.html\"相对=nofollow> org.apache.myfaces.trinidad.util.Base64OutputStream close()方法。这就是为什么我试过了。

这似乎有点奇怪,数据处理之前关闭的OutputStream,但它只是接近Base64OutputStream但在这种情况下,不能同时ByteArrayOutputStream。因此,数据仍然可以从ByteArrayOutputStream检索。

由于Dawnkeeper的努力,并希望这个问题能够帮助其他遭受了内存不足的异常。

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

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.

Here is the Code:

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;

解决方案

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

Here is the 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;

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.

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.

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

这篇关于Android文件为Base64使用流有时错过了2个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆