Browse Source

Merge pull request #437 from heypinch/master

Hash function allocates too much memory for large files
Travis Nuttall 5 years ago
parent
commit
9a43620de3
No account linked to committer's email address
1 changed files with 5 additions and 4 deletions
  1. 5
    4
      android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java

+ 5
- 4
android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java View File

888
             MessageDigest md = MessageDigest.getInstance(algorithms.get(algorithm));
888
             MessageDigest md = MessageDigest.getInstance(algorithms.get(algorithm));
889
 
889
 
890
             FileInputStream inputStream = new FileInputStream(path);
890
             FileInputStream inputStream = new FileInputStream(path);
891
-            byte[] buffer = new byte[(int)file.length()];
891
+            int chunkSize = 4096 * 256; // 1Mb
892
+            byte[] buffer = new byte[chunkSize];
892
 
893
 
893
-            int read;
894
-            while ((read = inputStream.read(buffer)) != -1) {
895
-                md.update(buffer, 0, read);
894
+            int bytesRead;
895
+            while ((bytesRead = inputStream.read(buffer)) != -1) {
896
+                md.update(buffer, 0, bytesRead);
896
             }
897
             }
897
 
898
 
898
             StringBuilder hexString = new StringBuilder();
899
             StringBuilder hexString = new StringBuilder();