|
| 1 | +package tools.jackson.core.unittest.base64; |
| 2 | + |
| 3 | +import java.io.ByteArrayInputStream; |
| 4 | +import java.io.ByteArrayOutputStream; |
| 5 | +import java.io.StringWriter; |
| 6 | + |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +import tools.jackson.core.*; |
| 10 | +import tools.jackson.core.json.JsonFactory; |
| 11 | +import tools.jackson.core.unittest.JacksonCoreTestBase; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 16 | + |
| 17 | +// [core#1622]: When a binary content length is known, the encoding/read buffer |
| 18 | +// should be sized from that hint (capped) so large content needs far fewer |
| 19 | +// InputStream reads than the small default buffer would require. |
| 20 | +class BinaryWriteBufferSize1622Test |
| 21 | + extends JacksonCoreTestBase |
| 22 | +{ |
| 23 | + // Cap mirrored from JsonGeneratorImpl.MAX_BASE64_ENCODE_BUFFER_LENGTH |
| 24 | + private final static int MAX_BUFFER = 64 * 1024; |
| 25 | + |
| 26 | + private final JsonFactory JSON_F = new JsonFactory(); |
| 27 | + |
| 28 | + private final Base64Variant VARIANT = Base64Variants.MIME; |
| 29 | + |
| 30 | + /** |
| 31 | + * {@link ByteArrayInputStream} that records the largest {@code len} ever |
| 32 | + * requested via {@link #read(byte[], int, int)}, which equals the size of |
| 33 | + * the read buffer the generator allocated. |
| 34 | + */ |
| 35 | + static class ReadSizeRecordingInputStream extends ByteArrayInputStream { |
| 36 | + int maxRequestedRead = 0; |
| 37 | + |
| 38 | + ReadSizeRecordingInputStream(byte[] buf) { |
| 39 | + super(buf); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public synchronized int read(byte[] b, int off, int len) { |
| 44 | + if (len > maxRequestedRead) { |
| 45 | + maxRequestedRead = len; |
| 46 | + } |
| 47 | + return super.read(b, off, len); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void sizeHintAppliedByteBacked() throws Exception { |
| 53 | + // 50_000 is below the 64kB cap, so the read buffer should be sized to it |
| 54 | + _testSizeHint(true, 50_000, 50_000); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void sizeHintAppliedCharBacked() throws Exception { |
| 59 | + _testSizeHint(false, 50_000, 50_000); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void sizeHintCappedByteBacked() throws Exception { |
| 64 | + // 200_000 exceeds the cap, so the read buffer should be limited to it |
| 65 | + _testSizeHint(true, 200_000, MAX_BUFFER); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void sizeHintCappedCharBacked() throws Exception { |
| 70 | + _testSizeHint(false, 200_000, MAX_BUFFER); |
| 71 | + } |
| 72 | + |
| 73 | + private void _testSizeHint(boolean useBytes, int dataLength, int expectedMaxRead) |
| 74 | + throws Exception |
| 75 | + { |
| 76 | + byte[] input = new byte[dataLength]; |
| 77 | + for (int i = 0; i < input.length; ++i) { |
| 78 | + input[i] = (byte) (i * 31 + 7); |
| 79 | + } |
| 80 | + ReadSizeRecordingInputStream in = new ReadSizeRecordingInputStream(input); |
| 81 | + |
| 82 | + byte[] rawJson; |
| 83 | + if (useBytes) { |
| 84 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 85 | + try (JsonGenerator g = JSON_F.createGenerator(ObjectWriteContext.empty(), out, JsonEncoding.UTF8)) { |
| 86 | + g.writeBinary(VARIANT, in, dataLength); |
| 87 | + } |
| 88 | + rawJson = out.toByteArray(); |
| 89 | + } else { |
| 90 | + StringWriter sw = new StringWriter(); |
| 91 | + try (JsonGenerator g = JSON_F.createGenerator(ObjectWriteContext.empty(), sw)) { |
| 92 | + g.writeBinary(VARIANT, in, dataLength); |
| 93 | + } |
| 94 | + rawJson = sw.toString().getBytes("UTF-8"); |
| 95 | + } |
| 96 | + |
| 97 | + // The generator should have requested reads as large as the (capped) hint, |
| 98 | + // which is much bigger than the small default buffer used before the fix. |
| 99 | + assertEquals(expectedMaxRead, in.maxRequestedRead, |
| 100 | + "read buffer should be sized from the length hint (capped)"); |
| 101 | + assertTrue(in.maxRequestedRead <= MAX_BUFFER, |
| 102 | + "read buffer must never exceed the cap"); |
| 103 | + |
| 104 | + // ...and the produced base64 must still decode back to the original bytes. |
| 105 | + try (JsonParser p = JSON_F.createParser(ObjectReadContext.empty(), rawJson)) { |
| 106 | + assertEquals(JsonToken.VALUE_STRING, p.nextToken()); |
| 107 | + byte[] decoded = p.getBinaryValue(VARIANT); |
| 108 | + assertArrayEquals(input, decoded); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments