Skip to content

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

Open
Divyansh151005 wants to merge 2 commits into
google:masterfrom
Divyansh151005:fix/base-encoding-close-idempotent
Open

Make BaseEncoding.encodingStream().close() idempotent.#8642
Divyansh151005 wants to merge 2 commits into
google:masterfrom
Divyansh151005:fix/base-encoding-close-idempotent

Conversation

@Divyansh151005

@Divyansh151005 Divyansh151005 commented Aug 30, 2026

Copy link
Copy Markdown

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

Fixes google#5284

RELNOTES=`io`: Fixed a bug in which calling `close()` more than once on a `BaseEncoding.encodingStream()` could write extra padding characters.

Co-authored-by: Cursor <cursoragent@cursor.com>
@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.

Co-authored-by: Cursor <cursoragent@cursor.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant