Package ej.cbor
Class CborDecoder
- java.lang.Object
-
- ej.cbor.CborDecoder
-
public class CborDecoder extends java.lang.ObjectProvides a decoder capable of handling CBOR encoded data from aInputStream.
-
-
Constructor Summary
Constructors Constructor Description CborDecoder(java.io.InputStream is)Creates a newCborDecoderinstance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected longexpectIntegerType(int ib)Reads the next major type from the underlying input stream, and verifies whether it matches the given expectation.CborTypepeekType()Peeks in the input stream for the upcoming type.longreadArrayLength()Prolog to reading an array value in CBOR format.booleanreadBoolean()Reads a boolean value in CBOR format.java.lang.ObjectreadBreak()Reads a "break"/stop value in CBOR format.byte[]readByteString()Reads a byte string value in CBOR format.longreadByteStringLength()Prolog to reading a byte string value in CBOR format.doublereadDouble()Reads a double-precision float value in CBOR format.floatreadFloat()Reads a single-precision float value in CBOR format.doublereadHalfPrecisionFloat()Reads a half-precision float value in CBOR format.longreadInt()Reads a signed or unsigned integer value in CBOR format.intreadInt16()Reads a signed or unsigned 16-bit integer value in CBOR format.longreadInt32()Reads a signed or unsigned 32-bit integer value in CBOR format.longreadInt64()Reads a signed or unsigned 64-bit integer value in CBOR format.intreadInt8()Reads a signed or unsigned 8-bit integer value in CBOR format.protected intreadMajorType(int majorType)Reads the next major type from the underlying input stream, and verifies whether it matches the given expectation.protected voidreadMajorTypeExact(int majorType, int subtype)Reads the next major type from the underlying input stream, and verifies whether it matches the given expectations.protected longreadMajorTypeWithSize(int majorType)Reads the next major type from the underlying input stream, verifies whether it matches the given expectation, and decodes the payload into a size.longreadMapLength()Prolog to reading a map of key-value pairs in CBOR format.java.lang.ObjectreadNull()Reads anull-value in CBOR format.bytereadSimpleValue()Reads a single byte value in CBOR format.intreadSmallInt()Reads a signed or unsigned small (<= 23) integer value in CBOR format.longreadTag()Reads a semantic tag value in CBOR format.java.lang.StringreadTextString()Reads an UTF-8 encoded string value in CBOR format.longreadTextStringLength()Prolog to reading an UTF-8 encoded string value in CBOR format.protected longreadUInt(int length, boolean breakAllowed)Reads an unsigned integer with a given length-indicator.protected intreadUInt16()Reads an unsigned 16-bit integer value.protected longreadUInt32()Reads an unsigned 32-bit integer value.protected longreadUInt64()Reads an unsigned 64-bit integer value.protected intreadUInt8()Reads an unsigned 8-bit integer value.protected longreadUIntExact(int expectedLength, int length)Reads an unsigned integer with a given length-indicator.java.lang.ObjectreadUndefined()Reads an undefined value in CBOR format.
-
-
-
Constructor Detail
-
CborDecoder
public CborDecoder(java.io.InputStream is)
Creates a newCborDecoderinstance.- Parameters:
is- the actual input stream to read the CBOR-encoded data from, cannot benull.
-
-
Method Detail
-
peekType
@Nullable public CborType peekType() throws java.io.IOException
Peeks in the input stream for the upcoming type.- Returns:
- the upcoming type in the stream, or
nullin case of an end-of-stream. - Throws:
java.io.IOException- in case of I/O problems reading the CBOR-type from the underlying input stream.
-
readArrayLength
public long readArrayLength() throws java.io.IOExceptionProlog to reading an array value in CBOR format.- Returns:
- the number of elements in the array to read, or
-1in case of infinite-length arrays. - Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
readBoolean
public boolean readBoolean() throws java.io.IOExceptionReads a boolean value in CBOR format.- Returns:
- the read boolean.
- Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
readBreak
@Nullable public java.lang.Object readBreak() throws java.io.IOExceptionReads a "break"/stop value in CBOR format.- Returns:
- always
null. - Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
readByteString
public byte[] readByteString() throws java.io.IOExceptionReads a byte string value in CBOR format.- Returns:
- the read byte string, never
null. In case the encoded string has a length of0, an empty string is returned. - Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
readByteStringLength
public long readByteStringLength() throws java.io.IOExceptionProlog to reading a byte string value in CBOR format.- Returns:
- the number of bytes in the string to read, or
-1in case of infinite-length strings. - Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
readDouble
public double readDouble() throws java.io.IOExceptionReads a double-precision float value in CBOR format.- Returns:
- the read double value, values from
Float.MIN_VALUEtoFloat.MAX_VALUEare supported. - Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
readFloat
public float readFloat() throws java.io.IOExceptionReads a single-precision float value in CBOR format.- Returns:
- the read float value, values from
Float.MIN_VALUEtoFloat.MAX_VALUEare supported. - Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
readHalfPrecisionFloat
public double readHalfPrecisionFloat() throws java.io.IOExceptionReads a half-precision float value in CBOR format.- Returns:
- the read half-precision float value, values from
Float.MIN_VALUEtoFloat.MAX_VALUEare supported. - Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
readInt
public long readInt() throws java.io.IOExceptionReads a signed or unsigned integer value in CBOR format.- Returns:
- the read integer value, values from
Long.MIN_VALUEtoLong.MAX_VALUEare supported. - Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
readInt16
public int readInt16() throws java.io.IOExceptionReads a signed or unsigned 16-bit integer value in CBOR format.- Returns:
- the small integer value, values from
[-65536..65535]are supported. - Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying output stream.
-
readInt32
public long readInt32() throws java.io.IOExceptionReads a signed or unsigned 32-bit integer value in CBOR format.- Returns:
- the small integer value, values in the range
[-4294967296..4294967295]are supported. - Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying output stream.
-
readInt64
public long readInt64() throws java.io.IOExceptionReads a signed or unsigned 64-bit integer value in CBOR format.- Returns:
- the small integer value, values from
Long.MIN_VALUEtoLong.MAX_VALUEare supported. - Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying output stream.
-
readInt8
public int readInt8() throws java.io.IOExceptionReads a signed or unsigned 8-bit integer value in CBOR format.- Returns:
- the small integer value, values in the range
[-256..255]are supported. - Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying output stream.
-
readMapLength
public long readMapLength() throws java.io.IOExceptionProlog to reading a map of key-value pairs in CBOR format.- Returns:
- the number of entries in the map, <= 0.
- Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
readNull
@Nullable public java.lang.Object readNull() throws java.io.IOExceptionReads anull-value in CBOR format.- Returns:
- always
null. - Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
readSimpleValue
public byte readSimpleValue() throws java.io.IOExceptionReads a single byte value in CBOR format.- Returns:
- the read byte value.
- Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
readSmallInt
public int readSmallInt() throws java.io.IOExceptionReads a signed or unsigned small (<= 23) integer value in CBOR format.- Returns:
- the small integer value, values in the range
[-24..23]are supported. - Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying output stream.
-
readTag
public long readTag() throws java.io.IOExceptionReads a semantic tag value in CBOR format.- Returns:
- the read tag value.
- Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
readTextString
public java.lang.String readTextString() throws java.io.IOExceptionReads an UTF-8 encoded string value in CBOR format.- Returns:
- the read UTF-8 encoded string, never
null. In case the encoded string has a length of0, an empty string is returned. - Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
readTextStringLength
public long readTextStringLength() throws java.io.IOExceptionProlog to reading an UTF-8 encoded string value in CBOR format.- Returns:
- the length of the string to read, or
-1in case of infinite-length strings. - Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
readUndefined
@Nullable public java.lang.Object readUndefined() throws java.io.IOExceptionReads an undefined value in CBOR format.- Returns:
- always
null. - Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
expectIntegerType
protected long expectIntegerType(int ib) throws java.io.IOExceptionReads the next major type from the underlying input stream, and verifies whether it matches the given expectation.- Parameters:
ib- the the expected type- Returns:
- either
-1if the major type was an signed integer, or0otherwise. - Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
readMajorType
protected int readMajorType(int majorType) throws java.io.IOExceptionReads the next major type from the underlying input stream, and verifies whether it matches the given expectation.- Parameters:
majorType- the expected major type, cannot benull(unchecked).- Returns:
- the read subtype, or payload, of the read major type.
- Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
readMajorTypeExact
protected void readMajorTypeExact(int majorType, int subtype) throws java.io.IOExceptionReads the next major type from the underlying input stream, and verifies whether it matches the given expectations.- Parameters:
majorType- the expected major type, cannot benull(unchecked);subtype- the expected subtype.- Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
readMajorTypeWithSize
protected long readMajorTypeWithSize(int majorType) throws java.io.IOExceptionReads the next major type from the underlying input stream, verifies whether it matches the given expectation, and decodes the payload into a size.- Parameters:
majorType- the expected major type, cannot benull(unchecked).- Returns:
- the number of succeeding bytes, >= 0, or
-1if an infinite-length type is read. - Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
readUInt
protected long readUInt(int length, boolean breakAllowed) throws java.io.IOExceptionReads an unsigned integer with a given length-indicator.- Parameters:
length- the length indicator to use;breakAllowed- if break is allowed.- Returns:
- the read unsigned integer, as long value.
- Throws:
java.io.IOException- in case of I/O problems reading the unsigned integer from the underlying input stream.
-
readUInt16
protected int readUInt16() throws java.io.IOExceptionReads an unsigned 16-bit integer value.- Returns:
- value the read value, values from
Long.MIN_VALUEtoLong.MAX_VALUEare supported. - Throws:
java.io.IOException- in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
-
readUInt32
protected long readUInt32() throws java.io.IOExceptionReads an unsigned 32-bit integer value.- Returns:
- value the read value, values from
Long.MIN_VALUEtoLong.MAX_VALUEare supported. - Throws:
java.io.IOException- in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
-
readUInt64
protected long readUInt64() throws java.io.IOExceptionReads an unsigned 64-bit integer value.- Returns:
- value the read value, values from
Long.MIN_VALUEtoLong.MAX_VALUEare supported. - Throws:
java.io.IOException- in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
-
readUInt8
protected int readUInt8() throws java.io.IOExceptionReads an unsigned 8-bit integer value.- Returns:
- value the read value, values from
Long.MIN_VALUEtoLong.MAX_VALUEare supported. - Throws:
java.io.IOException- in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
-
readUIntExact
protected long readUIntExact(int expectedLength, int length) throws java.io.IOExceptionReads an unsigned integer with a given length-indicator.- Parameters:
expectedLength- the expected length;length- the length indicator to use;- Returns:
- the read unsigned integer, as long value.
- Throws:
java.io.IOException- in case of I/O problems reading the unsigned integer from the underlying input stream.
-
-