Skip to content

Commit

Permalink
Merge branch '2.16'
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Aug 19, 2023
2 parents 279c89c + e33efb3 commit b57fb9e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
4 changes: 4 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,7 @@ Joo Hyuk Kim (JooHyukKim@github)
* Contributed #1067: Add `ErrorReportConfiguration`
(2.16.0)

David Schlosnagle (@schlosna)
* Contributed #1081: Make `ByteSourceJsonBootstrapper` use `StringReader` for < 8KiB
byte[] inputs
(2.16.0)
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ a pure JSON library.
(contributed by @digulla)
#1066: Add configurable error report behavior via `ErrorReportConfiguration`
(contributed by Joo-Hyuk K)
#1081: Make `ByteSourceJsonBootstrapper` use `StringReader` for < 8KiB byte[] inputs
(contributed by @schlosna)

2.15.2 (30-May-2023)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;

import tools.jackson.core.*;
import tools.jackson.core.exc.WrappedIOException;
import tools.jackson.core.io.*;
import tools.jackson.core.sym.ByteQuadsCanonicalizer;
import tools.jackson.core.sym.CharsToNameCanonicalizer;
import tools.jackson.core.util.VersionUtil;

/**
* This class is used to determine the encoding of byte stream
Expand All @@ -26,6 +28,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#1081] 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 @@ -177,7 +182,8 @@ public JsonEncoding detectEncoding() throws JacksonException
break;
case 4: enc = _bigEndian ? JsonEncoding.UTF32_BE : JsonEncoding.UTF32_LE;
break;
default: throw new RuntimeException("Internal error"); // should never get here
default:
return VersionUtil.throwInternalReturnAny();
}
}
_context.setEncoding(enc);
Expand Down Expand Up @@ -237,6 +243,16 @@ public Reader constructReader() throws JacksonException
InputStream in = _in;

if (in == null) {
int length = _inputEnd - _inputPtr;
if (length <= STRING_READER_BYTE_ARRAY_LENGTH_LIMIT) {
// [jackson-core#1081] Avoid overhead of heap ByteBuffer allocated by InputStreamReader
// when processing small inputs up to 8KiB.
try {
return new StringReader(new String(_inputBuffer, _inputPtr, length, enc.getJavaName()));
} catch (IOException e) {
throw _wrapIOFailure(e);
}
}
in = new ByteArrayInputStream(_inputBuffer, _inputPtr, _inputEnd);
} else {
// Also, if we have any read but unused input (usually true),
Expand All @@ -262,7 +278,7 @@ public Reader constructReader() throws JacksonException
_context.getEncoding().isBigEndian());
}
}
throw new RuntimeException("Internal error"); // should never get here
return VersionUtil.throwInternalReturnAny();
}

public JsonParser constructParser(ObjectReadContext readCtxt,
Expand Down Expand Up @@ -403,9 +419,8 @@ private void _reportWeirdUCS4(String type) throws JacksonException {
*/

protected boolean ensureLoaded(int minimum) throws JacksonException {
/* Let's assume here buffer has enough room -- this will always
* be true for the limited used this method gets
*/
// Let's assume here buffer has enough room -- this will always
// be true for the limited used this method gets
int gotten = (_inputEnd - _inputPtr);
while (gotten < minimum) {
int count;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/tools/jackson/core/util/VersionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,8 @@ protected static int parseVersionPart(String s) {
public final static void throwInternal() {
throw new RuntimeException("Internal error: this code path should never get executed");
}

public final static <T> T throwInternalReturnAny() {
throw new RuntimeException("Internal error: this code path should never get executed");
}
}

0 comments on commit b57fb9e

Please sign in to comment.