Class CborTokenReader
- All Implemented Interfaces:
Closeable,AutoCloseable,RecordReader,TokenReader
TokenReader surface for the CBOR format
(RFC 8949).
Backed by the existing internal CborInputStream plumbing. CBOR's wire format is already
token-shaped (major-type encoded data items), so this cursor maps almost directly onto
CborInputStream.readDataType().
Notes:
- This class is not thread safe.
- The cursor is a true O(1)-memory streaming cursor (
isStreaming()==true ). - The cursor is purely structural; object swaps and
@Schemaannotations have no effect. Use the POJO databind path onCborParserfor those.
-
Field Summary
Fields inherited from interface org.apache.juneau.marshall.stream.TokenReader
MSG_READ_NON_VALUE_STATE -
Constructor Summary
ConstructorsConstructorDescriptionCborTokenReader(ParserPipe pipe) Constructor with default settings.CborTokenReader(ParserPipe pipe, CborParserSession session) Constructor used byCborParserSession.readTokens(Object)to plumb the calling session through so thatread(Class)can delegate to the CBOR databind path. -
Method Summary
Modifier and TypeMethodDescriptionbooleancanRead()Returnstrue ifTokenReader.read(Class)is currently callable on this cursor — i.e. the cursor is positioned at a value boundary.voidclose()Closes the underlying stream.byte[]Returns the current binary value.booleangetBool()Returns the current boolean value.Returns the current token type without advancing.intgetDepth()Returns the current depth of the cursor.intReturns the typed-binary type byte for the current token.Returns the field name when the current token isTokenType.FIELD_NAME.Returns the binary-native classification of the current token.Returns the current numeric value as a parsedNumber.Returns the original numeric lexeme for the current numeric token.intReturns the opaque simple value for the current token.Returns the current scalar value as a string.longgetTag(int index) Returns the semantic tag at the specified position in the tag stack.intReturns the number of semantic tags wrapping the current token.booleanStructural token cursors are always O(1)-memory streamers.next()Advances the cursor to the next token.<T> TConsumes the upcoming value (object, array, or scalar) and binds it to a POJO via the format's existing parser.<T> TSame asTokenReader.read(Class)but accepts a parameterizedTypeand optional type arguments (the same shape asParser.read(Object, Type, Type...)).<T> TSame asTokenReader.read(Class)but accepts a pre-resolvedClassMeta.setMaxLength(int value) Sets the maximum allowed wire-declared length (in bytes) for byte/text strings read by this cursor.setMaxTagNestingDepth(int value) Sets the maximum number of nested CBOR semantic tags this cursor will unwrap before throwing aParseException.setNativeMode(boolean value) Enables or disables binary-native opt-in mode.voidSkips over the children of the current structural token.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.juneau.marshall.stream.RecordReader
iterator, stream
-
Constructor Details
-
CborTokenReader
Constructor with default settings.- Parameters:
pipe- The parser input pipe. Must not benull .- Throws:
IOException- If a problem occurred opening the underlying stream.
-
CborTokenReader
Constructor used byCborParserSession.readTokens(Object)to plumb the calling session through so thatread(Class)can delegate to the CBOR databind path.- Parameters:
pipe- The parser input pipe. Must not benull .session- TheCborParserSessionforread(Class)delegation, ornull to disableread.- Throws:
IOException- If a problem occurred opening the underlying stream.
-
-
Method Details
-
setMaxTagNestingDepth
Sets the maximum number of nested CBOR semantic tags this cursor will unwrap before throwing aParseException.Defaults to
DEFAULT_MAX_TAG_NESTING_DEPTH(64). A chain of more than this many tags wrapping a single value causesnext()to throw rather than risk aStackOverflowError.- Parameters:
value- The new maximum nesting depth. Must be>= 0.- Returns:
- This object.
-
setNativeMode
Enables or disables binary-native opt-in mode.When enabled, CBOR semantic tags are captured on the tag stack rather than discarded (surface them via
getTagCount()/getTag(int)), and CBOR simple values surface their int viagetSimpleValue(). When disabled (default), tags are silently unwrapped and simple values collapse toTokenType.VALUE_NULLwith no metadata — identical to the pre-native-mode behavior.- Parameters:
value-true to enable native mode.- Returns:
- This object.
-
setMaxLength
Sets the maximum allowed wire-declared length (in bytes) for byte/text strings read by this cursor.Applies to the length caps enforced while decoding definite- and indefinite-length strings. The cursor is intentionally NOT bounded by an element-count cap for arrays/maps — it is an O(1)-memory streaming cursor and never materializes containers, so it accepts large definite-length containers. Values
<= 0disable the maximum-length cap (the negative-length check still applies).- Parameters:
value- The maximum length in bytes.- Returns:
- This object.
-
next
Description copied from interface:TokenReaderAdvances the cursor to the next token.- Specified by:
nextin interfaceTokenReader- Returns:
- The token type of the new current token, or
TokenType.END_OF_STREAMwhen the underlying input is exhausted. Never returnsTokenType.NOT_AVAILABLE— that value is reserved for the initial pre-next()state. - Throws:
IOException- If a problem occurred reading from the underlying stream.ParseException- If the input is malformed for the format.
-
getCurrentToken
Description copied from interface:TokenReaderReturns the current token type without advancing.- Specified by:
getCurrentTokenin interfaceTokenReader- Returns:
- The current token type. Returns
TokenType.NOT_AVAILABLEbefore the first call toTokenReader.next(), andTokenType.END_OF_STREAMonce the stream is exhausted.
-
getDepth
Description copied from interface:TokenReaderReturns the current depth of the cursor.Depth starts at
0 at the document root and increases by 1 for eachTokenType.START_OBJECT/TokenType.START_ARRAYencountered (and decreases on the matching end tokens).- Specified by:
getDepthin interfaceTokenReader- Returns:
- The current depth.
-
getFieldName
Description copied from interface:TokenReaderReturns the field name when the current token isTokenType.FIELD_NAME.- Specified by:
getFieldNamein interfaceTokenReader- Returns:
- The field name.
-
getString
Description copied from interface:TokenReaderReturns the current scalar value as a string.Defined for
TokenType.VALUE_STRING(the natural value),TokenType.FIELD_NAME(returns the field name),TokenType.VALUE_NUMBER(returns the lexeme), andTokenType.VALUE_BOOLEAN(returns"true" /"false" ). ForTokenType.VALUE_NULLreturnsnull .- Specified by:
getStringin interfaceTokenReader- Returns:
- The string view of the current value.
-
getNumber
Description copied from interface:TokenReaderReturns the current numeric value as a parsedNumber.Implementations may parse lazily; the returned
Numbersubtype is implementation- and format-defined (e.g.Long/Double/BigDecimal). UseTokenReader.getNumberLexeme()to recover the original textual form.- Specified by:
getNumberin interfaceTokenReader- Returns:
- The parsed numeric value.
- Throws:
ParseException- If the lexeme cannot be parsed.
-
getNumberLexeme
Description copied from interface:TokenReaderReturns the original numeric lexeme for the current numeric token.For text formats this is the exact substring that produced the number. For binary formats the implementation returns a canonical text rendering of the native numeric token.
- Specified by:
getNumberLexemein interfaceTokenReader- Returns:
- The numeric lexeme.
-
getBool
Description copied from interface:TokenReaderReturns the current boolean value.- Specified by:
getBoolin interfaceTokenReader- Returns:
- The boolean value.
-
getBinary
Description copied from interface:TokenReaderReturns the current binary value.Binary scalars come from binary formats only (cbor, msgpack, bson, ...). Text formats deliver embedded binary content as base64-encoded
TokenType.VALUE_STRING.- Specified by:
getBinaryin interfaceTokenReader- Returns:
- The binary value.
-
canRead
Description copied from interface:TokenReaderReturnstrue ifTokenReader.read(Class)is currently callable on this cursor — i.e. the cursor is positioned at a value boundary.This is the natural loop predicate when streaming records:
try (TokenReaderr = Json.DEFAULT .readTokens(input )) {r .next();// consume the opening [ while (r .canRead()) { Itemitem =r .read(Item.class ); process(item ); }r .next();// consume the closing ] }- Specified by:
canReadin interfaceRecordReader- Specified by:
canReadin interfaceTokenReader- Returns:
true if the next operation can beread;false if the cursor is positioned at a structural separator, field name, end-container, or end-of-stream.- Throws:
IOException- If a problem occurred peeking at the underlying stream.ParseException- If the input is malformed for the format.
-
read
Description copied from interface:TokenReaderConsumes the upcoming value (object, array, or scalar) and binds it to a POJO via the format's existing parser.Cursor positioning: the cursor must be in a state where the next call to
TokenReader.next()would emit a value-shaped token (i.e.TokenType.START_OBJECT,TokenType.START_ARRAY, or one of theVALUE_*tokens). Callingreadconsumes that upcoming value wholesale and advances the cursor's depth back to where it was before the value would have started — structurally equivalent to callingTokenReader.next()on a scalar, orTokenReader.next()+TokenReader.skipChildren()on a container.Do NOT call
TokenReader.next()beforeread. Once the structural opener (e.g.{ or[ ) has been consumed byTokenReader.next(), the underlying parser cannot re-bind the value. The contract is "instead-of-next, not after-next."Default behavior: the default implementation throws
UnsupportedOperationException. Format-specific token readers override this by delegating to their existing*ParserSessionat the cursor's current input position and resyncing the cursor's state machine afterward.- Specified by:
readin interfaceRecordReader- Specified by:
readin interfaceTokenReader- Type Parameters:
T- The expected return type.- Parameters:
type- The target Java type.- Returns:
- The bound POJO.
- Throws:
IOException- If a problem occurred reading from the underlying stream.ParseException- If the input is malformed for the format or cannot be bound to the type.
-
read
Description copied from interface:TokenReaderSame asTokenReader.read(Class)but accepts a pre-resolvedClassMeta.- Specified by:
readin interfaceRecordReader- Specified by:
readin interfaceTokenReader- Type Parameters:
T- The expected return type.- Parameters:
type- The target type.- Returns:
- The bound POJO.
- Throws:
IOException- If a problem occurred reading from the underlying stream.ParseException- If the input is malformed or cannot be bound to the type.
-
read
Description copied from interface:TokenReaderSame asTokenReader.read(Class)but accepts a parameterizedTypeand optional type arguments (the same shape asParser.read(Object, Type, Type...)).- Specified by:
readin interfaceRecordReader- Specified by:
readin interfaceTokenReader- Type Parameters:
T- The expected return type.- Parameters:
type- The target type.args- Optional type arguments for parameterized types.- Returns:
- The bound POJO.
- Throws:
IOException- If a problem occurred reading from the underlying stream.ParseException- If the input is malformed or cannot be bound to the type.
-
skipChildren
Description copied from interface:TokenReaderSkips over the children of the current structural token.If the current token is
TokenType.START_OBJECTorTokenType.START_ARRAY, the cursor is advanced to the matchingTokenType.END_OBJECTorTokenType.END_ARRAY(i.e. the entire sub-tree is consumed). For any other token this is a no-op.- Specified by:
skipChildrenin interfaceTokenReader- Throws:
IOException- If a problem occurred reading from the underlying stream.ParseException- If the input is malformed for the format.
-
isStreaming
Description copied from interface:TokenReaderStructural token cursors are always O(1)-memory streamers.- Specified by:
isStreamingin interfaceRecordReader- Specified by:
isStreamingin interfaceTokenReader- Returns:
- Always
true .
-
close
Description copied from interface:TokenReaderCloses the underlying stream.Implementations should be idempotent — closing a closed reader is a no-op.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceRecordReader- Specified by:
closein interfaceTokenReader- Throws:
IOException
-
getNativeKind
Description copied from interface:TokenReaderReturns the binary-native classification of the current token.- Specified by:
getNativeKindin interfaceTokenReader- Returns:
- The native kind, or
NONEwhen the current token carries no binary-native metadata. Nevernull.
-
getTagCount
Description copied from interface:TokenReaderReturns the number of semantic tags wrapping the current token.- Specified by:
getTagCountin interfaceTokenReader- Returns:
- The tag count (0 when
TokenReader.getNativeKind()is notCBOR_TAG).
-
getTag
Description copied from interface:TokenReaderReturns the semantic tag at the specified position in the tag stack.- Specified by:
getTagin interfaceTokenReader- Parameters:
index- The tag position; outermost tag is0.- Returns:
- The tag number.
-
getExtType
Description copied from interface:TokenReaderReturns the typed-binary type byte for the current token.- Specified by:
getExtTypein interfaceTokenReader- Returns:
- The signed type byte (range
-128..127).
-
getSimpleValue
Description copied from interface:TokenReaderReturns the opaque simple value for the current token.- Specified by:
getSimpleValuein interfaceTokenReader- Returns:
- The simple int.
-