Skip to content

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

Closed
Divyansh151005 wants to merge 1 commit into
google:masterfrom
Divyansh151005:fix/base-encoding-close-idempotent
Closed

Divyansh151005 wants to merge 1 commit into
google:masterfrom
Divyansh151005:fix/base-encoding-close-idempotent

Conversation

@Divyansh151005

@Divyansh151005 Divyansh151005 commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Closeable.close() specifies that closing an already-closed stream must have no effect. Today, calling close() twice on a BaseEncoding.encodingStream() can write extra padding characters on the second call.

Reproduction:

StringWriter w = new StringWriter();
OutputStream out = BaseEncoding.base64().encodingStream(w);
out.write(0);
out.close();
System.out.println(w); // AA==
out.close();
System.out.println(w); // AA==A=== (incorrect)

Fixes #5284

Solution

Track a closed flag on the encoding stream and return immediately from subsequent close() calls. Reset bitBufferLength after flushing any remaining bits on the first close.

Testing

$ export JAVA_HOME=~/.m2/jdks/jdk-26.0.2+10/Contents/Home
$ ./mvnw -B -Dtoolchain.skip -P!standard-with-extra-repos test \
    -Dmaven.javadoc.skip=true -Dsurefire.toolchain.version=17 \
    -Dtest=BaseEncodingTest#testEncodingStream_closeIsIdempotent \
    -DfailIfNoTests=false -pl guava-tests -am

[INFO] Running com.google.common.io.BaseEncodingTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS

@google-cla

google-cla Bot commented Aug 30, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@cgdecker

Copy link
Copy Markdown
Member

We can't review your PR unless you sign the CLA.

@cgdecker cgdecker added the P4 no SLO label Sep 15, 2026
Fixes google#5284

RELNOTES=`io`: Fixed a bug in which calling `close()` more than once on a `BaseEncoding.encodingStream()` could write extra padding characters.
@Divyansh151005
Divyansh151005 force-pushed the fix/base-encoding-close-idempotent branch from bab7f45 to 162a171 Compare October 17, 2026 15:09
@Divyansh151005

Copy link
Copy Markdown
Contributor Author

@cgdecker Thanks for the heads-up. The CLA check was failing because of an extra co-author trailer on the commits, not my own signature. I've rewritten the commit without it (code unchanged), and cla/google is now passing. This should be ready for review whenever someone has time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P4 no SLO

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BaseEncoding.encodingStream().close() is non-idempotent

2 participants