|
11 | 11 | import java.io.IOException;
|
12 | 12 | import java.io.InputStream;
|
13 | 13 | import java.nio.file.Files;
|
| 14 | +import java.security.DigestInputStream; |
14 | 15 | import java.security.MessageDigest;
|
15 | 16 | import java.util.Objects;
|
16 | 17 |
|
@@ -86,20 +87,16 @@ public String getChecksum(String box) {
|
86 | 87 |
|
87 | 88 | private String calculateHash(String box) {
|
88 | 89 | LOG.debug("calculating [{}] hash for box [{}]", hashAlgorithm.name(), box);
|
89 |
| - try (InputStream boxDataStream = Files.newInputStream(new File(box).toPath())) { |
| 90 | + try (InputStream boxDataStream = Files.newInputStream(new File(box).toPath()); |
| 91 | + InputStream digestInputStream = new DigestInputStream(boxDataStream, messageDigest)) { |
90 | 92 | LOG.trace("buffering box data (buffer size [{}]b) ...", streamBufferLength);
|
91 | 93 | final byte[] buffer = new byte[streamBufferLength];
|
92 |
| - int read = boxDataStream.read(buffer, 0, streamBufferLength); |
93 |
| - |
94 |
| - while (read > -1) { |
95 |
| - messageDigest.update(buffer, 0, read); |
96 |
| - read = boxDataStream.read(buffer, 0, streamBufferLength); |
97 |
| - } |
| 94 | + //noinspection StatementWithEmptyBody |
| 95 | + while (digestInputStream.read(buffer) > 0) ; |
98 | 96 | } catch (IOException e) {
|
99 | 97 | LOG.error("Error during processing file [{}], message: [{}]", box, e.getMessage());
|
100 | 98 | throw new RuntimeException(
|
101 |
| - "Error while getting checksum for file " + box + " reason: " + e.getMessage(), e |
102 |
| - ); |
| 99 | + "Error while getting checksum for file " + box + " reason: " + e.getMessage(), e); |
103 | 100 | }
|
104 | 101 |
|
105 | 102 | return getHash(messageDigest.digest());
|
|
0 commit comments