From 2042610f28080c489a1eb9614ed2b534c0518883 Mon Sep 17 00:00:00 2001 From: Abayomi Akanji Date: Fri, 10 Nov 2017 18:14:11 +0100 Subject: [PATCH 1/2] OOM crash fixed for large data The while loop algorithm changed to fix OOM crash in larger files. --- alice/src/main/java/com/rockaport/alice/Alice.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/alice/src/main/java/com/rockaport/alice/Alice.java b/alice/src/main/java/com/rockaport/alice/Alice.java index 16f3fbe..86f6884 100644 --- a/alice/src/main/java/com/rockaport/alice/Alice.java +++ b/alice/src/main/java/com/rockaport/alice/Alice.java @@ -250,7 +250,7 @@ public synchronized void encrypt(File input, File output, char[] password) // write the initialization vector bufferedOutputStream.write(initializationVector); - while ((bytesRead = bufferedInputStream.read(inputStreamBuffer)) > 0) { + while ((bytesRead = bufferedInputStream.read(inputStreamBuffer)) != -1) { // encrypt encryptedBytes = cipher.update(inputStreamBuffer, 0, bytesRead); @@ -323,7 +323,7 @@ public synchronized void encrypt(InputStream input, OutputStream output, char[] // allocate variables int bytesRead; byte[] inputStreamBuffer = new byte[4096]; - while ((bytesRead = bufferedInputStream.read(inputStreamBuffer)) > 0) { + while ((bytesRead = bufferedInputStream.read(inputStreamBuffer)) != -1) { // encrypt bufferedOutputStream.write(cipher.update(inputStreamBuffer, 0, bytesRead)); } @@ -464,7 +464,7 @@ public synchronized void decrypt(File input, File output, char[] password) } // decrypt - while ((bytesRead = bufferedInputStream.read(inputStreamBuffer)) > 0) { + while ((bytesRead = bufferedInputStream.read(inputStreamBuffer)) != -1) { numBytesToProcess = (bytesRead < bytesLeft) ? bytesRead : (int) bytesLeft; if (numBytesToProcess <= 0) { @@ -548,7 +548,7 @@ public synchronized void decrypt(InputStream input, OutputStream output, char[] byte[] inputStreamBuffer = new byte[4096]; // decrypt - while ((bytesRead = bufferedInputStream.read(inputStreamBuffer)) > 0) { + while ((bytesRead = bufferedInputStream.read(inputStreamBuffer)) != -1) { bufferedOutputStream.write(cipher.update(inputStreamBuffer, 0, bytesRead)); } @@ -661,4 +661,4 @@ private void closeStream(Closeable stream) { } } } -} \ No newline at end of file +} From c650dc5ebbf73151072971a8ed132d2e385c115a Mon Sep 17 00:00:00 2001 From: Abayomi Akanji Date: Fri, 10 Nov 2017 18:46:33 +0100 Subject: [PATCH 2/2] byte[] size reduced due to OOM error Byte[] size of the overloaded "Encryption" & "Decryption" methods are reduced to fixed the OOM error. --- alice/src/main/java/com/rockaport/alice/Alice.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/alice/src/main/java/com/rockaport/alice/Alice.java b/alice/src/main/java/com/rockaport/alice/Alice.java index 86f6884..624c2e5 100644 --- a/alice/src/main/java/com/rockaport/alice/Alice.java +++ b/alice/src/main/java/com/rockaport/alice/Alice.java @@ -241,7 +241,7 @@ public synchronized void encrypt(File input, File output, char[] password) // allocate variables int bytesRead; byte[] encryptedBytes; - byte[] inputStreamBuffer = new byte[4096]; + byte[] inputStreamBuffer = new byte[1024]; // setup streams bufferedInputStream = new BufferedInputStream(new FileInputStream(input)); @@ -322,7 +322,7 @@ public synchronized void encrypt(InputStream input, OutputStream output, char[] // allocate variables int bytesRead; - byte[] inputStreamBuffer = new byte[4096]; + byte[] inputStreamBuffer = new byte[1024]; while ((bytesRead = bufferedInputStream.read(inputStreamBuffer)) != -1) { // encrypt bufferedOutputStream.write(cipher.update(inputStreamBuffer, 0, bytesRead)); @@ -455,7 +455,7 @@ public synchronized void decrypt(File input, File output, char[] password) // allocate loop buffers and variables int bytesRead; int numBytesToProcess; - byte[] inputStreamBuffer = new byte[4096]; + byte[] inputStreamBuffer = new byte[1024]; long bytesLeft = input.length() - context.getIvLength(); // subtract the mac length if enabled @@ -545,7 +545,7 @@ public synchronized void decrypt(InputStream input, OutputStream output, char[] // allocate loop buffers and variables int bytesRead; - byte[] inputStreamBuffer = new byte[4096]; + byte[] inputStreamBuffer = new byte[1024]; // decrypt while ((bytesRead = bufferedInputStream.read(inputStreamBuffer)) != -1) {