Java 加密时发生OutOfMemory错误
首先,这是我得到的错误Java 加密时发生OutOfMemory错误,java,android,encryption,out-of-memory,Java,Android,Encryption,Out Of Memory,首先,这是我得到的错误 java.lang.OutOfMemoryError at coderaustin.com.FileEncryptor.encryptFile(FileEncryptor.java:56) at coderaustin.com.Main.onCreate(Main.java:41) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069) at android.app
java.lang.OutOfMemoryError
at coderaustin.com.FileEncryptor.encryptFile(FileEncryptor.java:56)
at coderaustin.com.Main.onCreate(Main.java:41)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
at android.app.ActivityThread.access$2300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:144)
at android.app.ActivityThread.main(ActivityThread.java:4937)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
显然,我知道错误是什么,我只是不知道如何避免它。这就是我的应用程序的功能
选择一个文件,然后单击“加密”。所以很明显,它从那里获取文件,并使用此代码对其进行加密
try {
FileInputStream inFile = new FileInputStream(f.getAbsolutePath());
FileOutputStream outFile = new FileOutputStream(f.getAbsoluteFile() + ".des");
PBEKeySpec keySpec = new PBEKeySpec(text.toCharArray());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey passwordKey = keyFactory.generateSecret(keySpec);
byte[] salt = new byte[8];
Random rnd = new Random();
rnd.nextBytes(salt);
int iterations = 100;
PBEParameterSpec paramaterSpec = new PBEParameterSpec(salt, iterations);
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
cipher.init(Cipher.ENCRYPT_MODE, passwordKey, paramaterSpec);
outFile.write(salt);
byte[] input = new byte[inFile.available()];
int bytesRead;
while ((bytesRead = inFile.read(input)) != -1)
{
byte[] output = cipher.update(input, 0, bytesRead);
if (output != null) outFile.write(output);
}
byte[] output = cipher.doFinal();
if (output != null) outFile.write(output);
f.delete();
inFile.close();
outFile.flush();
outFile.close();
是的,我知道代码很难看,但它能工作。有什么建议吗
谢谢大家:
编辑:这是第56行
字节[]输入=新字节[infle.available] 奇怪:如果文件很大,我可以看出这是个问题。为什么要一次读取整个文件,而不是将其分成小块进行读取和处理 编辑:试试这个
byte[] input = new byte[4096];
int bytesRead;
while ((bytesRead = inFile.read(input, 0, 4096)) != -1)
{
byte[] output = cipher.update(input, 0, bytesRead);
if (output != null) outFile.write(output);
}
哇,我觉得自己很愚蠢,这是一个非常好的观点,也是一个我应该看到的明显的观点。那么我最好的选择是什么呢?谢谢您的帮助。请记住,Android应用程序的堆大小通常是16或24MB,因此,任何大于此大小的文件都将是一个问题,即使您有数百兆的可用内存。感谢您的帮助,效果非常好,@sargas这应该可以解决这一问题,即使它们只有16或24MB的堆大小,对吗?因为您只加载了4096个字节。@Austin:我会将这里的缓冲区大小增加到64k左右,因为更大的缓冲区会更快一些。当然,测试以找出收益递减点在缓冲区大小上的位置可能是一个好主意。或者使用具有BufferedOutputStream的类,它几乎可以为您做所有事情。