diff options
Diffstat (limited to 'sonar-home/src/main/java/org/sonar/home/cache/FileHashes.java')
-rw-r--r-- | sonar-home/src/main/java/org/sonar/home/cache/FileHashes.java | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/sonar-home/src/main/java/org/sonar/home/cache/FileHashes.java b/sonar-home/src/main/java/org/sonar/home/cache/FileHashes.java index 89a973fd2f4..28066cf5b94 100644 --- a/sonar-home/src/main/java/org/sonar/home/cache/FileHashes.java +++ b/sonar-home/src/main/java/org/sonar/home/cache/FileHashes.java @@ -36,6 +36,8 @@ import java.security.MessageDigest; */ public class FileHashes { + private static final int STREAM_BUFFER_LENGTH = 1024; + public String of(File file) { try { return of(new FileInputStream(file)); @@ -51,10 +53,7 @@ public class FileHashes { DigestInputStream digestInputStream = null; try { MessageDigest digest = MessageDigest.getInstance("MD5"); - digestInputStream = new DigestInputStream(input, digest); - while (digestInputStream.read() != -1) { - } - byte[] hash = digest.digest(); + byte[] hash = digest(input, digest); return toHex(hash); } catch (Exception e) { @@ -66,6 +65,16 @@ public class FileHashes { } } + private byte[] digest(InputStream input, MessageDigest digest) throws IOException { + final byte[] buffer = new byte[STREAM_BUFFER_LENGTH]; + int read = input.read(buffer, 0, STREAM_BUFFER_LENGTH); + while (read > -1) { + digest.update(buffer, 0, read); + read = input.read(buffer, 0, STREAM_BUFFER_LENGTH); + } + return digest.digest(); + } + static String toHex(byte[] bytes) { BigInteger bi = new BigInteger(1, bytes); return String.format("%0" + (bytes.length << 1) + "X", bi); |