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();