From b7336a40834fabcc325b636c543e2c30ee9099da Mon Sep 17 00:00:00 2001 From: MukjepScarlet <93977077+mukjepscarlet@users.noreply.github.com> Date: Thu, 23 Oct 2025 15:53:39 +0800 Subject: [PATCH 1/2] Make AppendableWriter do flush and close if delegation object supports --- .../java/com/google/gson/internal/Streams.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/gson/src/main/java/com/google/gson/internal/Streams.java b/gson/src/main/java/com/google/gson/internal/Streams.java index 46df853f5a..b46dfe2a70 100644 --- a/gson/src/main/java/com/google/gson/internal/Streams.java +++ b/gson/src/main/java/com/google/gson/internal/Streams.java @@ -26,7 +26,9 @@ import com.google.gson.stream.JsonToken; import com.google.gson.stream.JsonWriter; import com.google.gson.stream.MalformedJsonException; +import java.io.Closeable; import java.io.EOFException; +import java.io.Flushable; import java.io.IOException; import java.io.Writer; import java.util.Objects; @@ -89,10 +91,18 @@ public void write(char[] chars, int offset, int length) throws IOException { } @Override - public void flush() {} + public void flush() throws IOException { + if (appendable instanceof Flushable) { + ((Flushable) appendable).flush(); + } + } @Override - public void close() {} + public void close() throws IOException { + if (appendable instanceof Closeable) { + ((Closeable) appendable).close(); + } + } // Override these methods for better performance // They would otherwise unnecessarily create Strings or char arrays From 855215bbd9bb7360e1e3251cdb2f38f5ae283ddc Mon Sep 17 00:00:00 2001 From: MukjepScarlet <93977077+mukjepscarlet@users.noreply.github.com> Date: Thu, 23 Oct 2025 16:06:23 +0800 Subject: [PATCH 2/2] Add test --- .../com/google/gson/internal/StreamsTest.java | 50 ++++++++++++++++++- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/gson/src/test/java/com/google/gson/internal/StreamsTest.java b/gson/src/test/java/com/google/gson/internal/StreamsTest.java index 049a7ee0a1..93f3e5d027 100644 --- a/gson/src/test/java/com/google/gson/internal/StreamsTest.java +++ b/gson/src/test/java/com/google/gson/internal/StreamsTest.java @@ -19,15 +19,59 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertThrows; +import java.io.Closeable; +import java.io.Flushable; import java.io.IOException; import java.io.Writer; import org.junit.Test; public class StreamsTest { + private static class TestAppendable implements Appendable, Flushable, Closeable { + boolean closed = false; + int flushCount = 0; + Appendable append; + + TestAppendable(Appendable append) { + this.append = append; + } + + @Override + public void close() { + closed = true; + } + + @Override + public void flush() { + flushCount++; + } + + @Override + public TestAppendable append(CharSequence csq) throws IOException { + append.append(csq); + return this; + } + + @Override + public TestAppendable append(CharSequence csq, int start, int end) throws IOException { + append.append(csq, start, end); + return this; + } + + @Override + public TestAppendable append(char c) throws IOException { + append.append(c); + return this; + } + } + @Test public void testWriterForAppendable() throws IOException { StringBuilder stringBuilder = new StringBuilder(); - Writer writer = Streams.writerForAppendable(stringBuilder); + TestAppendable appendable = new TestAppendable(stringBuilder); + Writer writer = Streams.writerForAppendable(appendable); + + assertThat(appendable.closed).isFalse(); + assertThat(appendable.flushCount).isEqualTo(0); writer.append('a'); writer.append('\u1234'); @@ -62,7 +106,9 @@ public void testWriterForAppendable() throws IOException { writer.flush(); writer.close(); - // flush() and close() calls should have had no effect + assertThat(appendable.closed).isTrue(); + assertThat(appendable.flushCount).isEqualTo(1); + assertThat(stringBuilder.toString()).isEqualTo(actualOutput); } }