Skip to content
Open
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
11 changes: 11 additions & 0 deletions guava-tests/test/com/google/common/io/BaseEncodingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,17 @@ private static void testStreamingDecodes(BaseEncoding encoding, String encoded,
}
}

@GwtIncompatible // Writer
public void testEncodingStream_closeIsIdempotent() throws IOException {
StringWriter writer = new StringWriter();
OutputStream encodingStream = base64().encodingStream(writer);
encodingStream.write(0);
encodingStream.close();
assertThat(writer.toString()).isEqualTo("AA==");
encodingStream.close();
assertThat(writer.toString()).isEqualTo("AA==");
}

public void testToString() {
assertThat(base64().toString()).isEqualTo("BaseEncoding.base64().withPadChar('=')");
assertThat(base32Hex().omitPadding().toString())
Expand Down
6 changes: 6 additions & 0 deletions guava/src/com/google/common/io/BaseEncoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ public OutputStream encodingStream(Writer out) {
int bitBuffer = 0;
int bitBufferLength = 0;
int writtenChars = 0;
boolean closed = false;

@Override
public void write(int b) throws IOException {
Expand All @@ -667,6 +668,10 @@ public void flush() throws IOException {

@Override
public void close() throws IOException {
if (closed) {
return;
}
closed = true;
if (bitBufferLength > 0) {
int charIndex = (bitBuffer << (alphabet.bitsPerChar - bitBufferLength)) & alphabet.mask;
out.write(alphabet.encode(charIndex));
Expand All @@ -677,6 +682,7 @@ public void close() throws IOException {
writtenChars++;
}
}
bitBufferLength = 0;
}
out.close();
}
Expand Down