我正在尝试加密一个500MB的大文件,但是我的代码抛出了一个内存不足的错误,对于50MB以下的小文件,代码工作正常。我正在使用名为JNCryptor的第三方库进行加密,请看一下我的代码,如果有任何错误,请纠正我。提前谢谢。
public void encrypt() {
String file = Environment.getExternalStorageDirectory() + "/sai/ravi_enc.exe";
byte[] filedata = null;
try {
filedata = IOUtils.toByteArray(new FileInputStream(file));
} catch (IOException e) {
e.printStackTrace();
}
JNCryptor cryptor = new AES256JNCryptor();
String password = "123456789";
try {
byte[] ciphertext = cryptor.decryptData(filedata, password.toCharArray());
String Outfile = Environment.getExternalStorageDirectory() + "/sai/ravi_dec.exe";
writeFile(ciphertext, Outfile);
System.out.println("Done");
} catch (CryptorException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void writeFile(byte[] data, String fileName) throws IOException {
FileOutputStream out = new FileOutputStream(fileName);
out.write(data);
out.close();
}发布于 2015-09-04 20:30:15
这取决于JNCryptor来很好地管理内存,特别是在它使用临时缓冲区的情况下。最好在流上工作而不是在缓冲区上工作。只是为了测试,如果你直接把fileData写到你的outFile中,你会得到OutOfMemoryError吗?
writeFile(filedata, Outfile);https://stackoverflow.com/questions/32397907
复制相似问题