Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete temporary file on close #636

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
51 changes: 51 additions & 0 deletions jhdf/src/main/java/io/jhdf/TempHdfFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* This file is part of jHDF. A pure Java library for accessing HDF5 files.
*
* https://jhdf.io
*
* Copyright (c) 2024 James Mudd
*
* MIT License see 'LICENSE' file
*/
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);
}
}
}
13 changes: 13 additions & 0 deletions jhdf/src/test/java/io/jhdf/HdfFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
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.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class HdfFileTest {

Expand Down Expand Up @@ -316,6 +318,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
Loading