public class CBORDecoder
extends java.lang.Object
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 and Description |
|---|
CBORDecoder(byte[] encoding)
Base constructor.
|
CBORDecoder(byte[] encoding,
int off,
int len)
Constructor for reading a slice of a larger array.
|
| Modifier and Type | Method and Description |
|---|---|
void |
expectEnd()
Throw an exception unless all input has been consumed.
|
boolean |
hasNext()
Return true if at least one more data item (or part of one) is available.
|
boolean |
nextIsNull()
Return true if the next data item is the simple value null (0xf6).
|
boolean |
nextIsUndefined()
Return true if the next data item is the simple value undefined (0xf7).
|
int |
peekMajorType()
Return the major type (0 to 7) of the next data item without consuming it.
|
int |
readArrayHeader()
Read an array header (major type 4), returning the number of elements that follow.
|
java.math.BigInteger |
readBigInteger()
Read an integer (major type 0 or 1) of any magnitude CBOR supports
(-2^64 to 2^64-1).
|
boolean |
readBoolean()
Read a boolean simple value (0xf4 or 0xf5).
|
byte[] |
readByteString()
Read a byte string (major type 2).
|
byte[] |
readEncodedItem()
Read one complete data item, of whatever type, returning its encoding verbatim.
|
int |
readInt()
Read an integer (major type 0 or 1) in the range of a Java int.
|
long |
readInteger()
Read an integer (major type 0 or 1) in the range of a Java long.
|
int |
readMapHeader()
Read a map header (major type 5), returning the number of key/value pairs that follow.
|
void |
readNull()
Read the simple value null (0xf6).
|
long |
readTag()
Read a tag (major type 6), returning the tag number.
|
java.lang.String |
readTextString()
Read a text string (major type 3), validating that it is well-formed UTF-8.
|
void |
readUndefined()
Read the simple value undefined (0xf7).
|
long |
readUnsignedInteger()
Read an unsigned integer (major type 0) in the range 0 to 2^63-1.
|
public CBORDecoder(byte[] encoding)
encoding - the encoded CBOR data to read from.public CBORDecoder(byte[] encoding,
int off,
int len)
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.public boolean hasNext()
public void expectEnd()
throws java.io.IOException
java.io.IOExceptionpublic int peekMajorType()
throws java.io.IOException
java.io.IOExceptionpublic boolean nextIsNull()
throws java.io.IOException
java.io.IOExceptionpublic boolean nextIsUndefined()
throws java.io.IOException
java.io.IOExceptionpublic long readUnsignedInteger()
throws java.io.IOException
java.io.IOException - if the next item is not an unsigned integer in range.public long readInteger()
throws java.io.IOException
java.io.IOException - if the next item is not an integer, or is outside the long range.public int readInt()
throws java.io.IOException
java.io.IOException - if the next item is not an integer, or is outside the int range.public java.math.BigInteger readBigInteger()
throws java.io.IOException
java.io.IOException - if the next item is not an integer.public byte[] readByteString()
throws java.io.IOException
java.io.IOExceptionpublic java.lang.String readTextString()
throws java.io.IOException
java.io.IOExceptionpublic int readArrayHeader()
throws java.io.IOException
java.io.IOExceptionpublic int readMapHeader()
throws java.io.IOException
java.io.IOExceptionpublic long readTag()
throws java.io.IOException
java.io.IOExceptionpublic boolean readBoolean()
throws java.io.IOException
java.io.IOExceptionpublic void readNull()
throws java.io.IOException
java.io.IOExceptionpublic void readUndefined()
throws java.io.IOException
java.io.IOExceptionpublic byte[] readEncodedItem()
throws java.io.IOException
java.io.IOException