Skip to content

Commit

Permalink
try buffering for IQSS#9166
Browse files Browse the repository at this point in the history
  • Loading branch information
qqmyers committed Nov 14, 2022
1 parent 32ced00 commit 1b85e80
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions src/main/java/edu/harvard/iq/dataverse/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -685,29 +685,21 @@ public static String calculateChecksum(String datafile, ChecksumType checksumTyp
// from MD5Checksum.java
public static String calculateChecksum(InputStream in, ChecksumType checksumType) {
MessageDigest md = null;
try {

try (BufferedInputStream buf = new BufferedInputStream(in, 32 * 1024)){
// Use "SHA-1" (toString) rather than "SHA1", for example.
md = MessageDigest.getInstance(checksumType.toString());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}

byte[] dataBytes = new byte[1024];
byte[] dataBytes = new byte[1024];

int nread;
try {
while ((nread = in.read(dataBytes)) != -1) {
int nread;
while ((nread = buf.read(dataBytes)) != -1) {
md.update(dataBytes, 0, nread);
}
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (IOException ex) {
throw new RuntimeException(ex);
} finally {
try {
in.close();
} catch (Exception e) {
}
}

return checksumDigestToString(md.digest());
}

Expand Down

0 comments on commit 1b85e80

Please sign in to comment.