Package org.xipki.util.cbor
Class CborDecoder
- java.lang.Object
-
- org.xipki.util.cbor.CborDecoder
-
- All Implemented Interfaces:
java.lang.AutoCloseable
- Direct Known Subclasses:
ByteArrayCborDecoder
public class CborDecoder extends java.lang.Object implements java.lang.AutoCloseableProvides a decoder capable of handling CBOR encoded data from aInputStream.
-
-
Field Summary
Fields Modifier and Type Field Description protected java.io.PushbackInputStreamm_is
-
Constructor Summary
Constructors Constructor Description CborDecoder(java.io.InputStream is)Creates a newCborDecoderinstance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()protected longexpectIntegerType(int ib)Reads the next major type from the underlying input stream, and verifies whether it matches the given expectation.static booleanisNull(CborType type)CborTypepeekType()Peeks in the input stream for the upcoming type.intread1Byte()read one byelongreadArrayLength()Prolog to reading an array value in CBOR format.java.math.BigIntegerreadBigInt()java.math.BigInteger[]readBigInts()booleanreadBoolean()Reads a boolean value in CBOR format.java.lang.BooleanreadBooleanObj()voidreadBreak()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.byte[][]readByteStrings()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.java.time.InstantreadInstant()intreadInt()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.java.util.List<java.lang.Integer>readIntList()java.lang.IntegerreadIntObj()int[]readInts()longreadLong()Reads a signed or unsigned integer value in CBOR format.java.util.List<java.lang.Long>readLongList()java.lang.LongreadLongObj()long[]readLongs()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.voidreadNull()Reads anull-value in CBOR format.java.lang.IntegerreadNullOrArrayLength()booleanreadNullOrArrayLength(int expectedLen)true if it is null, or false it is an array of the specified length.java.lang.IntegerreadNullOrArrayLength(java.lang.Class clazz)java.lang.IntegerreadNullOrMapLength()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.LongreadTagObj()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.java.lang.String[]readTextStrings()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 valueprotected longreadUIntExact(int expectedLength, int length)Reads an unsigned integer with a given length-indicator.voidreadUndefined()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
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.
-
read1Byte
public int read1Byte() throws java.io.IOExceptionread one bye- Returns:
- the read byte.
- Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
-
readArrayLength
public long readArrayLength() throws java.io.IOException, DecodeExceptionProlog 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:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.DecodeException- in case of CBOR decoding problem.
-
readBoolean
public boolean readBoolean() throws java.io.IOException, DecodeExceptionReads 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.DecodeException- in case of CBOR decoding problem.
-
readBreak
public void readBreak() throws java.io.IOException, DecodeExceptionReads a "break"/stop value in CBOR format.- Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.DecodeException- in case of CBOR decoding problem.
-
readByteString
public byte[] readByteString() throws java.io.IOException, DecodeExceptionReads a byte string value in CBOR format.- Returns:
- the read byte string, or
null. - Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.DecodeException- in case of CBOR decoding problem.
-
readByteStringLength
public long readByteStringLength() throws java.io.IOException, DecodeExceptionProlog to reading a byte string value in CBOR format.- Returns:
- the number of bytes in the string to read, or -1 in 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.DecodeException- in case of CBOR decoding problem.
-
readDouble
public double readDouble() throws java.io.IOException, DecodeExceptionReads 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.DecodeException- in case of CBOR decoding problem.
-
readFloat
public float readFloat() throws java.io.IOException, DecodeExceptionReads 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.DecodeException- in case of CBOR decoding problem.
-
readHalfPrecisionFloat
public double readHalfPrecisionFloat() throws java.io.IOException, DecodeExceptionReads 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.DecodeException- in case of CBOR decoding problem.
-
readLong
public long readLong() throws java.io.IOException, DecodeExceptionReads 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.DecodeException- in case of CBOR decoding problem.
-
readLongs
public long[] readLongs() throws java.io.IOException, DecodeException- Throws:
java.io.IOExceptionDecodeException
-
readLongList
public java.util.List<java.lang.Long> readLongList() throws java.io.IOException, DecodeException- Throws:
java.io.IOExceptionDecodeException
-
readInt16
public int readInt16() throws java.io.IOException, DecodeExceptionReads a signed or unsigned 16-bit integer value in CBOR format.read the small integer value, values from [-65536..65535] are supported.
- Returns:
- the read 16-bit integerr.
- Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying output stream.DecodeException- in case of CBOR decoding problem.
-
readInt32
public long readInt32() throws java.io.IOException, DecodeExceptionReads a signed or unsigned 32-bit integer value in CBOR format.read the small integer value, values in the range [-4294967296..4294967295] are supported.
- Returns:
- the read 32-bit integer.
- Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying output stream.DecodeException- in case of CBOR decoding problem.
-
readInt64
public long readInt64() throws java.io.IOException, DecodeExceptionReads a signed or unsigned 64-bit integer value in CBOR format.read the small integer value, values from
Long.MIN_VALUEtoLong.MAX_VALUEare supported.- Returns:
- the read 64-bit integer (long).
- Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying output stream.DecodeException- in case of CBOR decoding problem.
-
readInt8
public int readInt8() throws java.io.IOException, DecodeExceptionReads a signed or unsigned 8-bit integer value in CBOR format.read the small integer value, values in the range [-256..255] are supported.
- Returns:
- the read 8-bit integer.
- Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying output stream.DecodeException- in case of CBOR decoding problem.
-
readMapLength
public long readMapLength() throws java.io.IOException, DecodeExceptionProlog 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.DecodeException- in case of CBOR decoding problem.
-
readNull
public void readNull() throws java.io.IOException, DecodeExceptionReads anull-value in CBOR format.- Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.DecodeException- in case of CBOR decoding problem.
-
readSimpleValue
public byte readSimpleValue() throws java.io.IOException, DecodeExceptionReads 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.DecodeException- in case of CBOR decoding problem.
-
readSmallInt
public int readSmallInt() throws java.io.IOException, DecodeExceptionReads a signed or unsigned small (<= 23) integer value in CBOR format.read the small integer value, values in the range [-24..23] are supported.
- Returns:
- the read small int.
- Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying output stream.DecodeException- in case of CBOR decoding problem.
-
readTag
public long readTag() throws java.io.IOException, DecodeExceptionReads 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.DecodeException- in case of CBOR decoding problem.
-
readTextString
public java.lang.String readTextString() throws java.io.IOException, DecodeExceptionReads an UTF-8 encoded string value in CBOR format.- Returns:
- the read UTF-8 encoded string, or
null. - Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.DecodeException- in case of CBOR decoding problem.
-
readTextStringLength
public long readTextStringLength() throws java.io.IOException, DecodeExceptionProlog to reading an UTF-8 encoded string value in CBOR format.- Returns:
- the length of the string to read, or -1 in 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.DecodeException- in case of CBOR decoding problem.
-
readUndefined
public void readUndefined() throws java.io.IOException, DecodeExceptionReads an undefined value in CBOR format.- Throws:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.DecodeException- in case of CBOR decoding problem.
-
expectIntegerType
protected long expectIntegerType(int ib) throws DecodeExceptionReads 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:
DecodeException- in case of CBOR decoding problem.
-
readMajorType
protected int readMajorType(int majorType) throws java.io.IOException, DecodeExceptionReads 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.DecodeException- in case of CBOR decoding problem.
-
readMajorTypeExact
protected void readMajorTypeExact(int majorType, int subtype) throws java.io.IOException, DecodeExceptionReads 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.DecodeException- in case of CBOR decoding problem.
-
readMajorTypeWithSize
protected long readMajorTypeWithSize(int majorType) throws java.io.IOException, DecodeExceptionReads 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:
java.io.IOException- in case of I/O problems reading the CBOR-encoded value from the underlying input stream.DecodeException- in case of CBOR decoding problem.
-
readUInt
protected long readUInt(int length, boolean breakAllowed) throws java.io.IOException, DecodeExceptionReads 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:
java.io.IOException- in case of I/O problems reading the unsigned integer from the underlying input stream.DecodeException- in case of CBOR decoding problem.
-
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.IOException, DecodeExceptionReads 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.DecodeException- in case of CBOR decoding problem.
-
readNullOrArrayLength
public boolean readNullOrArrayLength(int expectedLen) throws java.io.IOException, DecodeExceptiontrue if it is null, or false it is an array of the specified length.- Parameters:
expectedLen- the expected length of an array.- Returns:
- whether it is null.
- Throws:
java.io.IOException- if cannot decode the stream or is not an array with given length.DecodeException- in case of CBOR decoding problem.
-
readNullOrArrayLength
public java.lang.Integer readNullOrArrayLength() throws java.io.IOException, DecodeException- Throws:
java.io.IOExceptionDecodeException
-
readNullOrArrayLength
public java.lang.Integer readNullOrArrayLength(java.lang.Class clazz) throws DecodeException- Throws:
DecodeException
-
readNullOrMapLength
public java.lang.Integer readNullOrMapLength() throws java.io.IOException, DecodeException- Throws:
java.io.IOExceptionDecodeException
-
readTagObj
public java.lang.Long readTagObj() throws java.io.IOException, DecodeException- Throws:
java.io.IOExceptionDecodeException
-
isNull
public static boolean isNull(CborType type)
-
readBooleanObj
public java.lang.Boolean readBooleanObj() throws java.io.IOException, DecodeException- Throws:
java.io.IOExceptionDecodeException
-
readLongObj
public java.lang.Long readLongObj() throws java.io.IOException, DecodeException- Throws:
java.io.IOExceptionDecodeException
-
readIntObj
public java.lang.Integer readIntObj() throws java.io.IOException, DecodeException- Throws:
java.io.IOExceptionDecodeException
-
readBigInt
public java.math.BigInteger readBigInt() throws java.io.IOException, DecodeException- Throws:
java.io.IOExceptionDecodeException
-
readInstant
public java.time.Instant readInstant() throws java.io.IOException, DecodeException- Throws:
java.io.IOExceptionDecodeException
-
readTextStrings
public java.lang.String[] readTextStrings() throws java.io.IOException, DecodeException- Throws:
java.io.IOExceptionDecodeException
-
readByteStrings
public byte[][] readByteStrings() throws java.io.IOException, DecodeException- Throws:
java.io.IOExceptionDecodeException
-
readBigInts
public java.math.BigInteger[] readBigInts() throws java.io.IOException, DecodeException- Throws:
java.io.IOExceptionDecodeException
-
readInt
public int readInt() throws java.io.IOException, DecodeException- Throws:
java.io.IOExceptionDecodeException
-
readInts
public int[] readInts() throws java.io.IOException, DecodeException- Throws:
java.io.IOExceptionDecodeException
-
readIntList
public java.util.List<java.lang.Integer> readIntList() throws java.io.IOException, DecodeException- Throws:
java.io.IOExceptionDecodeException
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Throws:
java.io.IOException
-
-