This is an automated email from the ASF dual-hosted git repository. elharo pushed a commit to branch fix/propertyutils-stream-close in repository https://gitbox.apache.org/repos/asf/maven-shared-utils.git
commit 0e15ddbf6bdb7ffd5287282bea16574f1948245b Author: Elliotte Rusty Harold <[email protected]> AuthorDate: Wed Jul 1 10:29:18 2026 -0400 PropertyUtils: stop closing caller's InputStream --- .../apache/maven/shared/utils/PropertyUtils.java | 11 +++------ .../maven/shared/utils/PropertyUtilsTest.java | 27 ++++++++++++++++------ 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/apache/maven/shared/utils/PropertyUtils.java b/src/main/java/org/apache/maven/shared/utils/PropertyUtils.java index 199def0..70e62a9 100644 --- a/src/main/java/org/apache/maven/shared/utils/PropertyUtils.java +++ b/src/main/java/org/apache/maven/shared/utils/PropertyUtils.java @@ -90,11 +90,7 @@ public class PropertyUtils { try { Properties result = new Properties(); if (is != null) { - try (InputStream in = is) { - result.load(in); - } catch (IOException e) { - // ignore - } + result.load(is); } return result; } catch (Exception e) { @@ -168,9 +164,8 @@ public class PropertyUtils { Properties properties = new Properties(); if (inputStream != null) { - try (InputStream in = inputStream) // reassign inputStream to autoclose - { - properties.load(in); + try { + properties.load(inputStream); } catch (IllegalArgumentException | IOException ex) { // ignore and return empty properties } diff --git a/src/test/java/org/apache/maven/shared/utils/PropertyUtilsTest.java b/src/test/java/org/apache/maven/shared/utils/PropertyUtilsTest.java index 1055425..f133597 100644 --- a/src/test/java/org/apache/maven/shared/utils/PropertyUtilsTest.java +++ b/src/test/java/org/apache/maven/shared/utils/PropertyUtilsTest.java @@ -29,6 +29,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.Properties; @@ -50,7 +51,6 @@ public class PropertyUtilsTest { @Test @SuppressWarnings("deprecation") - // @ReproducesPlexusBug( "Should return null on error like url and file do" ) public void loadNullInputStream() { assertEquals(new Properties(), PropertyUtils.loadProperties((InputStream) null)); } @@ -110,13 +110,11 @@ public class PropertyUtilsTest { public void loadEmptyURL() throws Exception { assertEquals( new Properties(), - PropertyUtils.loadProperties( - newFile(tempFolder, "empty").toURI().toURL())); + PropertyUtils.loadProperties(newFile(tempFolder, "empty").toURI().toURL())); assertEquals( new Properties(), - PropertyUtils.loadOptionalProperties( - newFile(tempFolder, "optional").toURI().toURL())); + PropertyUtils.loadOptionalProperties(newFile(tempFolder, "optional").toURI().toURL())); } @Test @@ -154,11 +152,26 @@ public class PropertyUtilsTest { try (OutputStream out = Files.newOutputStream(valid.toPath())) { value.store(out, "a test"); assertEquals(value, PropertyUtils.loadProperties(valid.toURI().toURL())); - assertEquals( - value, PropertyUtils.loadOptionalProperties(valid.toURI().toURL())); + assertEquals(value, PropertyUtils.loadOptionalProperties(valid.toURI().toURL())); } } + @Test + @SuppressWarnings("deprecation") + public void streamIsNotClosedByLoadProperties() throws IOException { + ByteArrayInputStream stream = new ByteArrayInputStream("a=b".getBytes(StandardCharsets.ISO_8859_1)); + PropertyUtils.loadProperties(stream); + assertEquals(0, stream.available()); + } + + @Test + @SuppressWarnings("deprecation") + public void streamIsNotClosedByLoadOptionalProperties() throws IOException { + ByteArrayInputStream stream = new ByteArrayInputStream("a=b".getBytes(StandardCharsets.ISO_8859_1)); + PropertyUtils.loadOptionalProperties(stream); + assertEquals(0, stream.available()); + } + private static File newFile(File parent, String child) throws IOException { File result = new File(parent, child); result.createNewFile();
