Skip to content

Instantly share code, notes, and snippets.

@sugmanue
Created November 15, 2024 23:38
Show Gist options
  • Save sugmanue/0d763382c429dac2f8e94a08210622bc to your computer and use it in GitHub Desktop.
Save sugmanue/0d763382c429dac2f8e94a08210622bc to your computer and use it in GitHub Desktop.
CBOR create string directly from input buffer
diff --git a/cbor/src/main/java/com/fasterxml/jackson/dataformat/cbor/CBORParser.java b/cbor/src/main/java/com/fasterxml/jackson/dataformat/cbor/CBORParser.java
index a87cf82d..fa7cefd2 100644
--- a/cbor/src/main/java/com/fasterxml/jackson/dataformat/cbor/CBORParser.java
+++ b/cbor/src/main/java/com/fasterxml/jackson/dataformat/cbor/CBORParser.java
@@ -3,8 +3,7 @@ package com.fasterxml.jackson.dataformat.cbor;
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
-import java.nio.charset.Charset;
-import java.nio.charset.StandardCharsets;
+import java.nio.charset.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Stack;
@@ -2289,7 +2288,7 @@ public class CBORParser extends ParserMinimalBase
// if not, could we read? NOTE: we do not require it, just attempt to read
|| ((_inputBuffer.length >= len)
&& _tryToLoadToHaveAtLeast(len))) {
- _finishShortText(len);
+ _finishShortTextNew(len);
return;
}
// If not enough space, need handling similar to chunked
@@ -2334,12 +2333,32 @@ public class CBORParser extends ParserMinimalBase
// if not, could we read? NOTE: we do not require it, just attempt to read
|| ((_inputBuffer.length >= len)
&& _tryToLoadToHaveAtLeast(len))) {
- return _finishShortText(len);
+ return _finishShortTextNew(len);
}
// If not enough space, need handling similar to chunked
return _finishLongText(len);
}
+ private final String _finishShortTextNew(int len) throws IOException
+ {
+ StringRefList stringRefs = null;
+ if (!_stringRefs.empty() &&
+ shouldReferenceString(_stringRefs.peek().stringRefs.size(), len)) {
+ stringRefs = _stringRefs.peek();
+ }
+
+ int inPtr = _inputPtr;
+ _inputPtr += len;
+ final byte[] inputBuf = _inputBuffer;
+ String str = new String(inputBuf, inPtr, len, UTF8);
+ if (stringRefs != null) {
+ stringRefs.stringRefs.add(str);
+ _sharedString = str;
+ }
+ _textBuffer.resetWithString(str);
+ return str;
+ }
+
private final String _finishShortText(int len) throws IOException
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment