Skip to content

Navigation Menu

Sign in
Sign up

Make BaseEncoding.encodingStream().close() idempotent. #8642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Divyansh151005 wants to merge 2 commits into google:master
base: master
Choose a base branch
Loading
from Divyansh151005:fix/base-encoding-close-idempotent
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
View file Open in desktop
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
View file Open in desktop
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

AltStyle によって変換されたページ (->オリジナル) /