Skip to content

Commit 9a43620

Browse files
authored
Merge pull request #437 from heypinch/master
Hash function allocates too much memory for large files
2 parents 86dc386 + d40a563 commit 9a43620

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -888,11 +888,12 @@ static void hash(String path, String algorithm, Promise promise) {
888888
MessageDigest md = MessageDigest.getInstance(algorithms.get(algorithm));
889889

890890
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];
892893

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);
896897
}
897898

898899
StringBuilder hexString = new StringBuilder();

0 commit comments

Comments
 (0)