-
-
Notifications
You must be signed in to change notification settings - Fork 920
Fix #1622: size base64 encoding buffer from binary length hint #1624
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
Merged
cowtowncoder
merged 4 commits into
FasterXML:2.x
from
seonwooj0810:fix/1622-writebinary-buffer-size-hint
Jun 26, 2026
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/test/java/com/fasterxml/jackson/core/base64/BinaryWriteBufferSize1622Test.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| package com.fasterxml.jackson.core.base64; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.StringWriter; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import com.fasterxml.jackson.core.Base64Variant; | ||
| import com.fasterxml.jackson.core.Base64Variants; | ||
| import com.fasterxml.jackson.core.JsonEncoding; | ||
| import com.fasterxml.jackson.core.JsonFactory; | ||
| import com.fasterxml.jackson.core.JsonGenerator; | ||
| import com.fasterxml.jackson.core.JsonParser; | ||
| import com.fasterxml.jackson.core.JsonToken; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| // [core#1622]: When a binary content length is known, the encoding/read buffer | ||
| // should be sized from that hint (capped) so large content needs far fewer | ||
| // InputStream reads than the small default buffer would require. | ||
| class BinaryWriteBufferSize1622Test | ||
| extends com.fasterxml.jackson.core.JUnit5TestBase | ||
| { | ||
| // Cap mirrored from JsonGeneratorImpl.MAX_BASE64_ENCODE_BUFFER_LENGTH | ||
| private final static int MAX_BUFFER = 64 * 1024; | ||
|
|
||
| private final JsonFactory JSON_F = new JsonFactory(); | ||
|
|
||
| private final Base64Variant VARIANT = Base64Variants.MIME; | ||
|
|
||
| /** | ||
| * {@link ByteArrayInputStream} that records the largest {@code len} ever | ||
| * requested via {@link #read(byte[], int, int)}, which equals the size of | ||
| * the read buffer the generator allocated. | ||
| */ | ||
| static class ReadSizeRecordingInputStream extends ByteArrayInputStream { | ||
| int maxRequestedRead = 0; | ||
|
|
||
| ReadSizeRecordingInputStream(byte[] buf) { | ||
| super(buf); | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized int read(byte[] b, int off, int len) { | ||
| if (len > maxRequestedRead) { | ||
| maxRequestedRead = len; | ||
| } | ||
| return super.read(b, off, len); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void sizeHintAppliedByteBacked() throws Exception { | ||
| // 50_000 is below the 64kB cap, so the read buffer should be sized to it | ||
| _testSizeHint(true, 50_000, 50_000); | ||
| } | ||
|
|
||
| @Test | ||
| void sizeHintAppliedCharBacked() throws Exception { | ||
| _testSizeHint(false, 50_000, 50_000); | ||
| } | ||
|
|
||
| @Test | ||
| void sizeHintCappedByteBacked() throws Exception { | ||
| // 200_000 exceeds the cap, so the read buffer should be limited to it | ||
| _testSizeHint(true, 200_000, MAX_BUFFER); | ||
| } | ||
|
|
||
| @Test | ||
| void sizeHintCappedCharBacked() throws Exception { | ||
| _testSizeHint(false, 200_000, MAX_BUFFER); | ||
| } | ||
|
|
||
| private void _testSizeHint(boolean useBytes, int dataLength, int expectedMaxRead) | ||
| throws Exception | ||
| { | ||
| byte[] input = new byte[dataLength]; | ||
| for (int i = 0; i < input.length; ++i) { | ||
| input[i] = (byte) (i * 31 + 7); | ||
| } | ||
| ReadSizeRecordingInputStream in = new ReadSizeRecordingInputStream(input); | ||
|
|
||
| byte[] rawJson; | ||
| if (useBytes) { | ||
| ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
| try (JsonGenerator g = JSON_F.createGenerator(out, JsonEncoding.UTF8)) { | ||
| g.writeBinary(VARIANT, in, dataLength); | ||
| } | ||
| rawJson = out.toByteArray(); | ||
| } else { | ||
| StringWriter sw = new StringWriter(); | ||
| try (JsonGenerator g = JSON_F.createGenerator(sw)) { | ||
| g.writeBinary(VARIANT, in, dataLength); | ||
| } | ||
| rawJson = sw.toString().getBytes("UTF-8"); | ||
| } | ||
|
|
||
| // The generator should have requested reads as large as the (capped) hint, | ||
| // which is much bigger than the small default buffer used before the fix. | ||
| assertEquals(expectedMaxRead, in.maxRequestedRead, | ||
| "read buffer should be sized from the length hint (capped)"); | ||
| assertTrue(in.maxRequestedRead <= MAX_BUFFER, | ||
| "read buffer must never exceed the cap"); | ||
|
|
||
| // ...and the produced base64 must still decode back to the original bytes. | ||
| try (JsonParser p = JSON_F.createParser(rawJson)) { | ||
| assertEquals(JsonToken.VALUE_STRING, p.nextToken()); | ||
| byte[] decoded = p.getBinaryValue(VARIANT); | ||
| assertArrayEquals(input, decoded); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know the answer here but is there a reason not to keep this ordered? 8000 > 2000 so maybe the 16000 should go first.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. This array isn't sorted by value — it's positional: each slot is addressed by the named index constants just above it, and
byteBufferLength(int ix)returnsBYTE_BUFFER_LENGTHS[ix]. So the entries are in index order:[0]BYTE_READ_IO_BUFFER= 8000[1]BYTE_WRITE_ENCODING_BUFFER= 8000[2]BYTE_WRITE_CONCAT_BUFFER= 2000[3]BYTE_BASE64_CODEC_BUFFER= 16000 ← the slot this PR bumpsThe 16000 lands at the end only because the base64 codec buffer happens to be index 3; reordering the array would silently remap every buffer's length to the wrong purpose. Happy to add a brief inline comment naming each slot if you think that'd make the positional intent clearer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah these must not be ordered by size, they are indexed by position as @seonwooj0810 pointed out.
However, I realized something: this will also affect parser-side, if changed; Base64-decoding buffer (in addition to generator-size encoding buffer).
And in fact, not quite sure default really needs changing: if and when actual size is indicated, we'll be using that anyway. I think I'll change default to 4000 as compromise.