From 8a3778ae28cc90ebd64252da50993b5927064f4d Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Sun, 5 Jul 2026 11:48:18 +0000 Subject: [PATCH] fix #86: null stream and channel after close() to allow re-open - After close(), set channel and stream to null so open() can re-initialize - Add test for read-close-reopen-read --- .../shared/io/location/FileLocation.java | 3 +++ .../shared/io/location/FileLocationTest.java | 25 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/main/java/org/apache/maven/shared/io/location/FileLocation.java b/src/main/java/org/apache/maven/shared/io/location/FileLocation.java index 3d60340..842dd24 100644 --- a/src/main/java/org/apache/maven/shared/io/location/FileLocation.java +++ b/src/main/java/org/apache/maven/shared/io/location/FileLocation.java @@ -68,6 +68,9 @@ public void close() { // swallow it. } } + + channel = null; + stream = null; } /** {@inheritDoc} */ diff --git a/src/test/java/org/apache/maven/shared/io/location/FileLocationTest.java b/src/test/java/org/apache/maven/shared/io/location/FileLocationTest.java index 4ae737e..04bf20d 100644 --- a/src/test/java/org/apache/maven/shared/io/location/FileLocationTest.java +++ b/src/test/java/org/apache/maven/shared/io/location/FileLocationTest.java @@ -126,6 +126,31 @@ void shouldReadThenClose() throws Exception { location.close(); } + @Test + void shouldReopenAfterClose() throws Exception { + File file = Files.createTempFile("test.", ".file-location").toFile(); + file.deleteOnExit(); + + String testStr = "This is a test"; + + FileUtils.writeStringToFile(file, testStr, "US-ASCII"); + + FileLocation location = new FileLocation(file, file.getAbsolutePath()); + + location.open(); + byte[] buffer = new byte[testStr.length()]; + location.read(buffer); + assertEquals(testStr, new String(buffer, StandardCharsets.US_ASCII)); + location.close(); + + // read again after close should re-open + location.open(); + buffer = new byte[testStr.length()]; + location.read(buffer); + assertEquals(testStr, new String(buffer, StandardCharsets.US_ASCII)); + location.close(); + } + @Test void shouldOpenThenFailToSetFile() throws Exception { File file = Files.createTempFile("test.", ".file-location").toFile();