From 0e15ddbf6bdb7ffd5287282bea16574f1948245b Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Wed, 1 Jul 2026 10:29:18 -0400 Subject: [PATCH] PropertyUtils: stop closing caller's InputStream --- .../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 199def0d..70e62a9f 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 static Properties loadProperties(@Nullable InputStream is) { 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 static Properties loadOptionalProperties(final @Nullable InputStream inpu 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 1055425c..f1335976 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.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 void loadEmptyFile() throws Exception { 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 void loadValidURL() throws IOException { 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();