Skip to content

Commit

Permalink
Delete temporary file on close
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
15 changes: 2 additions & 13 deletions jhdf/src/main/java/io/jhdf/HdfFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -156,24 +154,15 @@ private HdfFile(HdfBackingStorage hdfBackingStorage) {

/**
* Opens an {@link HdfFile} from an {@link InputStream}. The stream will be read fully into a temporary file. The
* file will be cleaned up at application exit.
* file will be deleted when the HdfFile is closed, or at application exit if it was never closed.
*
* @param inputStream the {@link InputStream} to read
* @return HdfFile instance
* @see HdfFile#fromBytes(byte[])
* @see HdfFile#fromByteBuffer(ByteBuffer)
*/
public static HdfFile 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
Files.copy(inputStream, tempFile, StandardCopyOption.REPLACE_EXISTING);
logger.debug("Read stream to temp file [{}]", tempFile.toAbsolutePath());
return new HdfFile(tempFile);
} catch (IOException e) {
throw new HdfException("Failed to open input stream", e);
}
return TempHdfFile.fromInputStream(inputStream);
}

public HdfFile(Path hdfFile) {
Expand Down
42 changes: 42 additions & 0 deletions jhdf/src/main/java/io/jhdf/TempHdfFile.java
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);
}
}
}
14 changes: 12 additions & 2 deletions jhdf/src/test/java/io/jhdf/HdfFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.sameInstance;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.*;

class HdfFileTest {

Expand Down Expand Up @@ -316,6 +315,17 @@ void testReadingFromStreamThrowsWhenStreamCantBeRead() throws IOException {
}
}

@Test
void testReadingFromStreamDeletesTempFileOnClose() throws IOException {
File tempFile;
try (InputStream inputStream = this.getClass().getResource(HDF5_TEST_FILE_PATH).openStream();
HdfFile hdfFile = HdfFile.fromInputStream(inputStream)) {
tempFile = hdfFile.getFile();
assertTrue(tempFile.exists());
}
assertFalse(tempFile.exists());
}

@Test
void testLoadingInMemoryFile() throws IOException, URISyntaxException {
Path path = Paths.get(this.getClass().getResource(HDF5_TEST_FILE_PATH).toURI());
Expand Down

0 comments on commit 060a71b

Please sign in to comment.