Skip to content

Commit

Permalink
Merge pull request #10310 from IQSS/10052-use-try-with-resources-to-c…
Browse files Browse the repository at this point in the history
…lose-fileinputstream-gzippedin

10052 use try with resources to close fileinputstream gzippedin
  • Loading branch information
pdurbin committed Apr 16, 2024
2 parents 05107de + a21f44f commit 131e76c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
17 changes: 4 additions & 13 deletions src/main/java/edu/harvard/iq/dataverse/util/FileUtil.java
Expand Up @@ -501,24 +501,15 @@ public static String determineFileType(File f, String fileName) throws IOExcepti

if ("application/x-gzip".equals(fileType)) {
logger.fine("we'll run additional checks on this gzipped file.");
// We want to be able to support gzipped FITS files, same way as
// if they were just regular FITS files:
FileInputStream gzippedIn = new FileInputStream(f);
// (new FileInputStream() can throw a "filen not found" exception;
// however, if we've made it this far, it really means that the
// file does exist and can be opened)
InputStream uncompressedIn = null;
try {
uncompressedIn = new GZIPInputStream(gzippedIn);
try (FileInputStream gzippedIn = new FileInputStream(f);
InputStream uncompressedIn = new GZIPInputStream(gzippedIn)) {
if (isFITSFile(uncompressedIn)) {
fileType = "application/fits-gzipped";
}
} catch (IOException ioex) {
if (uncompressedIn != null) {
try {uncompressedIn.close();} catch (IOException e) {}
}
logger.warning("IOException while processing gzipped FITS file: " + ioex.getMessage());
}
}
}
if ("application/zip".equals(fileType)) {

// Is this a zipped Shapefile?
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/util/FileUtilTest.java
Expand Up @@ -371,4 +371,13 @@ public void testHdf4File() throws IOException {
assertEquals("application/octet-stream", contentType);
}

@Test
public void testGZipFile() throws IOException {
String path = "src/test/resources/fits/";
String pathAndFile = path + "FOSy19g0309t_c2f.fits.gz";
File file = new File(pathAndFile);
String contentType = FileUtil.determineFileType(file, pathAndFile);
assertEquals("application/fits-gzipped", contentType);
}

}
Binary file added src/test/resources/fits/FOSy19g0309t_c2f.fits.gz
Binary file not shown.

0 comments on commit 131e76c

Please sign in to comment.