Skip to content

Commit 4fd8c85

Browse files
authored
Make ByteSourceJsonBootstrapper use StringReader for < 8KiB byte[] inputs (#1081)
1 parent 31a8519 commit 4fd8c85

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/main/java/com/fasterxml/jackson/core/json/ByteSourceJsonBootstrapper.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public final class ByteSourceJsonBootstrapper
2222
public final static byte UTF8_BOM_2 = (byte) 0xBB;
2323
public final static byte UTF8_BOM_3 = (byte) 0xBF;
2424

25+
// [jackson-core#488] Limit in bytes for input byte array length to use StringReader instead of InputStreamReader
26+
private static final int STRING_READER_BYTE_ARRAY_LENGTH_LIMIT = 8192;
27+
2528
/*
2629
/**********************************************************
2730
/* Configuration
@@ -230,6 +233,12 @@ public Reader constructReader() throws IOException
230233
InputStream in = _in;
231234

232235
if (in == null) {
236+
int length = _inputEnd - _inputPtr;
237+
if (length <= STRING_READER_BYTE_ARRAY_LENGTH_LIMIT) {
238+
// [jackson-core#488] Avoid overhead of heap ByteBuffer allocated by InputStreamReader
239+
// when processing small inputs up to 8KiB.
240+
return new StringReader(new String(_inputBuffer, _inputPtr, length, enc.getJavaName()));
241+
}
233242
in = new ByteArrayInputStream(_inputBuffer, _inputPtr, _inputEnd);
234243
} else {
235244
/* Also, if we have any read but unused input (usually true),

0 commit comments

Comments
 (0)