Class CBORDecoder
The decoder is deliberately strict: indefinite-length items, non-shortest-form
arguments, floating-point values, simple values other than false, true, null and
undefined, malformed UTF-8 in text strings, and maps whose keys are not in bytewise
lexicographic order are all rejected with an IOException. Accepting any of
these would allow the same data item to have more than one encoding, which for
natively signed structures (where the signature is computed over the CBOR bytes)
would make signatures malleable.
All read methods honour a throws IOException contract for malformed
input; no RuntimeException escapes for any input byte sequence. Declared
lengths are validated against the number of bytes actually remaining before any
allocation is made, so a short crafted header cannot drive an over-allocation.
-
Constructor Summary
ConstructorsConstructorDescriptionCBORDecoder(byte[] encoding) Base constructor.CBORDecoder(byte[] encoding, int off, int len) Constructor for reading a slice of a larger array. -
Method Summary
Modifier and TypeMethodDescriptionvoidThrow an exception unless all input has been consumed.booleanhasNext()Return true if at least one more data item (or part of one) is available.booleanReturn true if the next data item is the simple value null (0xf6).booleanReturn true if the next data item is the simple value undefined (0xf7).intReturn the major type (0 to 7) of the next data item without consuming it.intRead an array header (major type 4), returning the number of elements that follow.Read an integer (major type 0 or 1) of any magnitude CBOR supports (-2^64 to 2^64-1).booleanRead a boolean simple value (0xf4 or 0xf5).byte[]Read a byte string (major type 2).byte[]Read one complete data item, of whatever type, returning its encoding verbatim.intreadInt()Read an integer (major type 0 or 1) in the range of a Java int.longRead an integer (major type 0 or 1) in the range of a Java long.intRead a map header (major type 5), returning the number of key/value pairs that follow.voidreadNull()Read the simple value null (0xf6).longreadTag()Read a tag (major type 6), returning the tag number.Read a text string (major type 3), validating that it is well-formed UTF-8.voidRead the simple value undefined (0xf7).longRead an unsigned integer (major type 0) in the range 0 to 2^63-1.
-
Constructor Details
-
CBORDecoder
public CBORDecoder(byte[] encoding) Base constructor.- Parameters:
encoding- the encoded CBOR data to read from.
-
CBORDecoder
public CBORDecoder(byte[] encoding, int off, int len) Constructor for reading a slice of a larger array.- Parameters:
encoding- the array containing the encoded CBOR data.off- the offset of the first byte of CBOR data.len- the number of bytes of CBOR data.
-
-
Method Details
-
hasNext
public boolean hasNext()Return true if at least one more data item (or part of one) is available. -
expectEnd
Throw an exception unless all input has been consumed.- Throws:
IOException
-
peekMajorType
Return the major type (0 to 7) of the next data item without consuming it.- Throws:
IOException
-
nextIsNull
Return true if the next data item is the simple value null (0xf6).- Throws:
IOException
-
nextIsUndefined
Return true if the next data item is the simple value undefined (0xf7).- Throws:
IOException
-
readUnsignedInteger
Read an unsigned integer (major type 0) in the range 0 to 2^63-1.- Throws:
IOException- if the next item is not an unsigned integer in range.
-
readInteger
Read an integer (major type 0 or 1) in the range of a Java long.- Throws:
IOException- if the next item is not an integer, or is outside the long range.
-
readInt
Read an integer (major type 0 or 1) in the range of a Java int.- Throws:
IOException- if the next item is not an integer, or is outside the int range.
-
readBigInteger
Read an integer (major type 0 or 1) of any magnitude CBOR supports (-2^64 to 2^64-1).- Throws:
IOException- if the next item is not an integer.
-
readByteString
Read a byte string (major type 2).- Throws:
IOException
-
readTextString
Read a text string (major type 3), validating that it is well-formed UTF-8.- Throws:
IOException
-
readArrayHeader
Read an array header (major type 4), returning the number of elements that follow. The count is validated against the bytes remaining (every element occupies at least one byte), so a crafted count cannot drive an oversized allocation in the caller.- Throws:
IOException
-
readMapHeader
Read a map header (major type 5), returning the number of key/value pairs that follow.- Throws:
IOException
-
readTag
Read a tag (major type 6), returning the tag number. The tag content follows as the next data item.- Throws:
IOException
-
readBoolean
Read a boolean simple value (0xf4 or 0xf5).- Throws:
IOException
-
readNull
-
readUndefined
Read the simple value undefined (0xf7).- Throws:
IOException
-
readEncodedItem
Read one complete data item, of whatever type, returning its encoding verbatim. The item is fully validated as it is skipped: all nested items must be deterministically encoded, nesting is bounded, and map keys must be sorted in bytewise lexicographic order of their encodings (RFC 8949 Section 4.2.1).- Throws:
IOException
-