diff --git a/src/main/java/org/codehaus/plexus/components/io/resources/Deferred.java b/src/main/java/org/codehaus/plexus/components/io/resources/Deferred.java index 7db54f3..ffb040d 100644 --- a/src/main/java/org/codehaus/plexus/components/io/resources/Deferred.java +++ b/src/main/java/org/codehaus/plexus/components/io/resources/Deferred.java @@ -47,9 +47,10 @@ public Deferred(final PlexusIoResource resource, PlexusIoResourceCollection owne .get() : null; if (dfos != null) { - InputStream inputStream = owner.getInputStream(resource); - IOUtils.copy(inputStream, dfos); - IOUtils.closeQuietly(inputStream); + try (InputStream inputStream = owner.getInputStream(resource); + DeferredFileOutputStream closeable = dfos) { + IOUtils.copy(inputStream, dfos); + } } } diff --git a/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResource.java b/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResource.java index dd262cb..dbd0a53 100755 --- a/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResource.java +++ b/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResource.java @@ -108,11 +108,11 @@ private static DeferredFileOutputStream asDeferredStream( .setThreshold(5000000) .setPrefix("p-archiver") .get(); - InputStream inputStream = supplier.getContents(); - InputStream transformed = transToUse.transform(resource, inputStream); - IOUtils.copy(transformed, dfos); - IOUtils.closeQuietly(inputStream); - IOUtils.closeQuietly(transformed); + try (InputStream inputStream = supplier.getContents(); + InputStream transformed = transToUse.transform(resource, inputStream); + DeferredFileOutputStream closeable = dfos) { + IOUtils.copy(transformed, dfos); + } return dfos; } diff --git a/src/test/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResourceDeferredTest.java b/src/test/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResourceDeferredTest.java new file mode 100644 index 0000000..04ffd54 --- /dev/null +++ b/src/test/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResourceDeferredTest.java @@ -0,0 +1,116 @@ +package org.codehaus.plexus.components.io.resources; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; + +import org.apache.commons.io.IOUtils; +import org.codehaus.plexus.components.io.attributes.FileAttributes; +import org.codehaus.plexus.components.io.functions.InputStreamTransformer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Tests for PlexusIoFileResource with DeferredFileOutputStream scenarios. + * This specifically tests the fix for issue #118 where temp files were deleted + * before they could be accessed, causing FileNotFoundException. + */ +public class PlexusIoFileResourceDeferredTest { + + @TempDir + File tempDir; + + @Test + void testFileResourceWithTransformerCanReadContentsMultipleTimes() throws IOException { + // Create a test file with content larger than typical buffer size + File testFile = new File(tempDir, "test-file.txt"); + byte[] largeContent = new byte[10000]; // 10KB + for (int i = 0; i < largeContent.length; i++) { + largeContent[i] = (byte) ('A' + (i % 26)); + } + Files.write(testFile.toPath(), largeContent); + + // Create a transformer that modifies the content + InputStreamTransformer transformer = (resource, inputStream) -> { + // Simple transformer that reads and returns the same content + return inputStream; + }; + + // Create PlexusIoFileResource with transformer + PlexusIoFileResource resource = + new PlexusIoFileResource(testFile, testFile.getName(), new FileAttributes(testFile), null, transformer); + + // First read - this should work and not delete the temp file + try (InputStream is1 = resource.getContents()) { + byte[] read1 = IOUtils.toByteArray(is1); + assertEquals(largeContent.length, read1.length); + } + + // Second read - this should also work if the temp file wasn't prematurely deleted + // This is the key test - without the fix, this would throw FileNotFoundException + try (InputStream is2 = resource.getContents()) { + byte[] read2 = IOUtils.toByteArray(is2); + assertEquals(largeContent.length, read2.length); + } + } + + @Test + void testFileResourceWithTransformerLargeFile() throws IOException { + // Create a large file that exceeds the DeferredFileOutputStream threshold (5MB) + File testFile = new File(tempDir, "large-test-file.bin"); + byte[] chunk = new byte[1024 * 1024]; // 1MB chunks + for (int i = 0; i < chunk.length; i++) { + chunk[i] = (byte) i; + } + + // Write 6MB to exceed the 5MB threshold + try (OutputStream os = Files.newOutputStream(testFile.toPath())) { + for (int i = 0; i < 6; i++) { + os.write(chunk); + } + } + + InputStreamTransformer transformer = (resource, inputStream) -> inputStream; + + PlexusIoFileResource resource = + new PlexusIoFileResource(testFile, testFile.getName(), new FileAttributes(testFile), null, transformer); + + // Verify we can read the content - this tests that the temp file + // created by DeferredFileOutputStream is properly accessible + long size = resource.getSize(); + assertTrue(size > 5_000_000, "File should be larger than 5MB threshold"); + + try (InputStream is = resource.getContents()) { + assertNotNull(is); + byte[] firstBytes = new byte[1024]; + int read = is.read(firstBytes); + assertEquals(1024, read); + } + } + + @Test + void testFileResourceWithTransformerSmallFile() throws IOException { + // Test with a small file that stays in memory (below 5MB threshold) + File testFile = new File(tempDir, "small-test-file.txt"); + String content = "Hello, World!"; + Files.write(testFile.toPath(), content.getBytes(StandardCharsets.UTF_8)); + + InputStreamTransformer transformer = (resource, inputStream) -> inputStream; + + PlexusIoFileResource resource = + new PlexusIoFileResource(testFile, testFile.getName(), new FileAttributes(testFile), null, transformer); + + // Multiple reads should work for small files too + for (int i = 0; i < 3; i++) { + try (InputStream is = resource.getContents()) { + String readContent = IOUtils.toString(is, StandardCharsets.UTF_8); + assertEquals(content, readContent); + } + } + } +}