-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A temp file is created when reading an HdfFile from an InputStream. Delete the temp file when the HdfFile is closed, or at application exit if the HdfFile was never closed. Fixes #262
- Loading branch information
Ivan Wick
committed
Oct 18, 2024
1 parent
100e683
commit 060a71b
Showing
3 changed files
with
56 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.jhdf; | ||
|
||
import io.jhdf.exceptions.HdfException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardCopyOption; | ||
|
||
class TempHdfFile extends HdfFile { | ||
private static final Logger logger = LoggerFactory.getLogger(TempHdfFile.class); | ||
|
||
private TempHdfFile(Path tempFile) { | ||
super(tempFile); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
super.close(); | ||
logger.info("Deleting temp file on close [{}]", getFileAsPath().toAbsolutePath()); | ||
boolean deleteSuccess = getFile().delete(); | ||
if (!deleteSuccess) { | ||
logger.warn("Could not delete temp file [{}]", getFileAsPath().toAbsolutePath()); | ||
} | ||
} | ||
|
||
public static TempHdfFile fromInputStream(InputStream inputStream) { | ||
try { | ||
Path tempFile = Files.createTempFile(null, "-stream.hdf5"); // null random file name | ||
logger.info("Creating temp file [{}]", tempFile.toAbsolutePath()); | ||
tempFile.toFile().deleteOnExit(); // Auto cleanup in case close() is never called | ||
Files.copy(inputStream, tempFile, StandardCopyOption.REPLACE_EXISTING); | ||
logger.debug("Read stream to temp file [{}]", tempFile.toAbsolutePath()); | ||
return new TempHdfFile(tempFile); | ||
} catch (IOException e) { | ||
throw new HdfException("Failed to open input stream", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters