From 2d98a6467741a24ef23da0bc32667fdd97368a8a Mon Sep 17 00:00:00 2001 From: George Gastaldi Date: Mon, 8 Jul 2024 20:44:29 -0300 Subject: [PATCH] Fix `Properties.store(OutputStream os, String comment)` This fixes the store method taking an `OutputStream` by flushing and closing the writer created to wrap it. --- pom.xml | 2 +- src/main/java/org/codejive/properties/Properties.java | 5 ++++- .../java/org/codejive/properties/TestProperties.java | 9 +++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index f87cfa6..829a016 100644 --- a/pom.xml +++ b/pom.xml @@ -187,4 +187,4 @@ https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ - \ No newline at end of file + diff --git a/src/main/java/org/codejive/properties/Properties.java b/src/main/java/org/codejive/properties/Properties.java index 3f28fa9..5b2e9e5 100644 --- a/src/main/java/org/codejive/properties/Properties.java +++ b/src/main/java/org/codejive/properties/Properties.java @@ -910,7 +910,9 @@ public void store(Path file, String... comment) throws IOException { * @throws IOException Thrown when any IO error occurs during operation */ public void store(OutputStream out, String... comment) throws IOException { - store(new OutputStreamWriter(out, StandardCharsets.ISO_8859_1), comment); + store( + new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.ISO_8859_1)), + comment); } /** @@ -938,6 +940,7 @@ public void store(Writer writer, String... comment) throws IOException { writer.write(pos.raw()); pos.next(); } + writer.flush(); } /** diff --git a/src/test/java/org/codejive/properties/TestProperties.java b/src/test/java/org/codejive/properties/TestProperties.java index 43304e1..c74449b 100644 --- a/src/test/java/org/codejive/properties/TestProperties.java +++ b/src/test/java/org/codejive/properties/TestProperties.java @@ -118,6 +118,15 @@ void testStore() throws IOException, URISyntaxException { assertThat(sw.toString()).isEqualTo(readAll(f)); } + @Test + void testStoreOutputStream() throws IOException, URISyntaxException { + Path f = getResource("/test-escaped.properties"); + Properties p = Properties.loadProperties(f); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + p.store(os); + assertThat(os.toString()).isEqualTo(readAll(f)); + } + @Test void testStoreCrLf() throws IOException, URISyntaxException { Path f = getResource("/testcrlf.properties");