Package org.xipki.apppackage.cbor
Class CborDecoder
- java.lang.Object
-
- org.xipki.apppackage.cbor.CborDecoder
-
public class CborDecoder extends Object
Provides a decoder capable of handling CBOR encoded data from aInputStream.
-
-
Field Summary
Fields Modifier and Type Field Description protected PushbackInputStreamm_is
-
Constructor Summary
Constructors Constructor Description CborDecoder(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.byte[]readByteString()Reads a byte string value in CBOR format.longreadInt()Reads a signed or unsigned 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.ObjectreadNull()Reads anull-value in CBOR format.StringreadTextString()Reads 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 valueprotected longreadUInt32()Reads an unsigned 32-bit integer valueprotected longreadUInt64()Reads an unsigned 64-bit integer valueprotected intreadUInt8()Reads an unsigned 8-bit integer value
-
-
-
Field Detail
-
m_is
protected final PushbackInputStream m_is
-
-
Constructor Detail
-
CborDecoder
public CborDecoder(InputStream is)
Creates a newCborDecoderinstance.- Parameters:
is- the actual input stream to read the CBOR-encoded data from, cannot benull.
-
-
Method Detail
-
peekType
public CborType peekType() throws 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:
IOException- in case of I/O problems reading the CBOR-type from the underlying input stream.
-
readArrayLength
public long readArrayLength() throws IOExceptionProlog to reading an array value in CBOR format.- Returns:
- the number of elements in the array to read, or -1 in case of infinite-length arrays.
- Throws:
IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
readBoolean
public boolean readBoolean() throws IOExceptionReads a boolean value in CBOR format.- Returns:
- the read boolean.
- Throws:
IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
readByteString
public byte[] readByteString() throws IOExceptionReads a byte string value in CBOR format.- Returns:
- the read byte string, never
null. In case the encoded string has a length of 0, an empty string is returned. - Throws:
IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
readInt
public long readInt() throws IOExceptionReads a signed or unsigned integer value in CBOR format.- Returns:
- the read integer value, values from
Long.MIN_VALUEtoLong.MAX_VALUEare supported. - Throws:
IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
readNull
public Object readNull() throws IOException
Reads anull-value in CBOR format.- Returns:
- always
null. - Throws:
IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
readTextString
public String readTextString() throws IOException
Reads 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 of 0, an empty string is returned. - Throws:
IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
expectIntegerType
protected long expectIntegerType(int ib) throws IOExceptionReads the next major type from the underlying input stream, and verifies whether it matches the given expectation.- Parameters:
ib- the expected major type, cannot benull(unchecked).- Returns:
- either -1 if the major type was an signed integer, or 0 otherwise.
- Throws:
IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
readMajorType
protected int readMajorType(int majorType) throws 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:
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 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:
IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
readMajorTypeWithSize
protected long readMajorTypeWithSize(int majorType) throws 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 -1 if an infinite-length type is read.
- Throws:
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 IOExceptionReads an unsigned integer with a given length-indicator.- Parameters:
length- the length indicator to use;breakAllowed- whether break is allowed.- Returns:
- the read unsigned integer, as long value.
- Throws:
IOException- in case of I/O problems reading the unsigned integer from the underlying input stream.
-
readUInt16
protected int readUInt16() throws IOExceptionReads an unsigned 16-bit integer value- Returns:
- value the read value, values from
Long.MIN_VALUEtoLong.MAX_VALUEare supported. - Throws:
IOException- in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
-
readUInt32
protected long readUInt32() throws IOExceptionReads an unsigned 32-bit integer value- Returns:
- value the read value, values from
Long.MIN_VALUEtoLong.MAX_VALUEare supported. - Throws:
IOException- in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
-
readUInt64
protected long readUInt64() throws IOExceptionReads an unsigned 64-bit integer value- Returns:
- value the read value, values from
Long.MIN_VALUEtoLong.MAX_VALUEare supported. - Throws:
IOException- in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
-
readUInt8
protected int readUInt8() throws IOExceptionReads an unsigned 8-bit integer value- Returns:
- value the read value, values from
Long.MIN_VALUEtoLong.MAX_VALUEare supported. - Throws:
IOException- in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
-
-