Skip to content

Commit

Permalink
Make ByteSourceJsonBootstrapper use StringReader for < 8KiB byte[…
Browse files Browse the repository at this point in the history
…] inputs (#1081)
  • Loading branch information
schlosna authored Aug 18, 2023
1 parent 31a8519 commit 4fd8c85
Showing 1 changed file with 9 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public final class ByteSourceJsonBootstrapper
public final static byte UTF8_BOM_2 = (byte) 0xBB;
public final static byte UTF8_BOM_3 = (byte) 0xBF;

// [jackson-core#488] Limit in bytes for input byte array length to use StringReader instead of InputStreamReader
private static final int STRING_READER_BYTE_ARRAY_LENGTH_LIMIT = 8192;

/*
/**********************************************************
/* Configuration
Expand Down Expand Up @@ -230,6 +233,12 @@ public Reader constructReader() throws IOException
InputStream in = _in;

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

0 comments on commit 4fd8c85

Please sign in to comment.