Skip to content

Commit

Permalink
Do not read the full file in-memory when computing checksums
Browse files Browse the repository at this point in the history
  • Loading branch information
melix committed Jan 22, 2020
1 parent 674481e commit 43f9ae6
Showing 1 changed file with 11 additions and 2 deletions.
Expand Up @@ -15,11 +15,12 @@
*/
package org.gradle.internal.hash;

import com.google.common.io.Files;
import org.gradle.api.UncheckedIOException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

class ChecksumHasher implements FileHasher {

Expand All @@ -32,7 +33,15 @@ public ChecksumHasher(HashFunction hashFunction) {
@Override
public HashCode hash(File file) {
try {
return hashFunction.hashBytes(Files.toByteArray(file));
PrimitiveHasher hasher = hashFunction.newPrimitiveHasher();
byte[] buffer = new byte[4096];
int len;
try (InputStream in = new FileInputStream(file)) {
while ((len = in.read(buffer)) >= 0) {
hasher.putBytes(buffer, 0, len);
}
}
return hasher.hash();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down

0 comments on commit 43f9ae6

Please sign in to comment.