Skip to content

Commit

Permalink
try-with-resources
Browse files Browse the repository at this point in the history
  • Loading branch information
ErykKul committed Nov 10, 2023
1 parent 160f9f7 commit eee3d87
Showing 1 changed file with 8 additions and 21 deletions.
29 changes: 8 additions & 21 deletions src/main/java/edu/harvard/iq/dataverse/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1469,37 +1469,24 @@ public static void validateDataFileChecksum(DataFile dataFile) throws IOExceptio
}

StorageIO<DataFile> storage = dataFile.getStorageIO();
InputStream in = null;
String recalculatedChecksum = null;

try {
in = getOriginalFileInputStream(storage, dataFile.isTabularData());
try (InputStream inputStream = getOriginalFileInputStream(storage, dataFile.isTabularData())) {
recalculatedChecksum = FileUtil.calculateChecksum(inputStream, checksumType);
} catch (IOException ioex) {
in = null;
}

if (in == null) {
String info = BundleUtil.getStringFromBundle("dataset.publish.file.validation.error.failRead", Arrays.asList(dataFile.getId().toString()));
logger.log(Level.INFO, info);
throw new IOException(info);
}

String recalculatedChecksum = null;
try {
recalculatedChecksum = FileUtil.calculateChecksum(in, checksumType);
} catch (RuntimeException rte) {
logger.log(Level.SEVERE, "failed to calculated checksum, one retry", rte);
recalculatedChecksum = null;
IOUtils.closeQuietly(in);
}

if (recalculatedChecksum == null) { //retry once
storage = dataFile.getStorageIO();
try {
in = getOriginalFileInputStream(storage, dataFile.isTabularData());
recalculatedChecksum = FileUtil.calculateChecksum(in, checksumType);
} catch (RuntimeException rte2) {
logger.log(Level.SEVERE, "failed to calculated checksum, no retry", rte2);
recalculatedChecksum = null;
try (InputStream inputStream = getOriginalFileInputStream(storage, dataFile.isTabularData())) {
recalculatedChecksum = FileUtil.calculateChecksum(inputStream, checksumType);
}
} finally {
IOUtils.closeQuietly(in);
}

if (recalculatedChecksum == null) {
Expand Down

0 comments on commit eee3d87

Please sign in to comment.