Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions gson/src/main/java/com/google/gson/internal/Streams.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
50 changes: 48 additions & 2 deletions gson/src/test/java/com/google/gson/internal/StreamsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
}
}