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 850b234
Showing 1 changed file with 7 additions and 17 deletions.
24 changes: 7 additions & 17 deletions src/main/java/edu/harvard/iq/dataverse/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp;
Expand Down Expand Up @@ -672,9 +673,9 @@ private static boolean isGraphMLFile(File file) {
// from MD5Checksum.java
public static String calculateChecksum(String datafile, ChecksumType checksumType) {

FileInputStream fis = null;
InputStream fis = null;
try {
fis = new FileInputStream(datafile);
fis = new BufferedInputStream(new FileInputStream(datafile));
} catch (FileNotFoundException ex) {
throw new RuntimeException(ex);
}
Expand All @@ -686,28 +687,17 @@ public static String calculateChecksum(String datafile, ChecksumType checksumTyp
public static String calculateChecksum(InputStream in, ChecksumType checksumType) {
MessageDigest md = null;
try {
// 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];

int nread;
try {
while ((nread = in.read(dataBytes)) != -1) {
md.update(dataBytes, 0, nread);
}
try (DigestInputStream dis= new DigestInputStream(in, md)){
// Use "SHA-1" (toString) rather than "SHA1", for example.
while (dis.read() != -1);
md = dis.getMessageDigest();
} catch (IOException ex) {
throw new RuntimeException(ex);
} finally {
try {
in.close();
} catch (Exception e) {
}
}

return checksumDigestToString(md.digest());
}

Expand Down

0 comments on commit 850b234

Please sign in to comment.