Class CborDecoder


  • public class CborDecoder
    extends Object
    Provides a decoder capable of handling CBOR encoded data from a InputStream.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected long expectIntegerType​(int ib)
      Reads the next major type from the underlying input stream, and verifies whether it matches the given expectation.
      CborType peekType()
      Peeks in the input stream for the upcoming type.
      long readArrayLength()
      Prolog to reading an array value in CBOR format.
      boolean readBoolean()
      Reads a boolean value in CBOR format.
      Object readBreak()
      Reads a "break"/stop value in CBOR format.
      byte[] readByteString()
      Reads a byte string value in CBOR format.
      long readByteStringLength()
      Prolog to reading a byte string value in CBOR format.
      double readDouble()
      Reads a double-precision float value in CBOR format.
      float readFloat()
      Reads a single-precision float value in CBOR format.
      double readHalfPrecisionFloat()
      Reads a half-precision float value in CBOR format.
      long readInt()
      Reads a signed or unsigned integer value in CBOR format.
      int readInt16()
      Reads a signed or unsigned 16-bit integer value in CBOR format.
      long readInt32()
      Reads a signed or unsigned 32-bit integer value in CBOR format.
      long readInt64()
      Reads a signed or unsigned 64-bit integer value in CBOR format.
      int readInt8()
      Reads a signed or unsigned 8-bit integer value in CBOR format.
      protected int readMajorType​(int majorType)
      Reads the next major type from the underlying input stream, and verifies whether it matches the given expectation.
      protected void readMajorTypeExact​(int majorType, int subtype)
      Reads the next major type from the underlying input stream, and verifies whether it matches the given expectations.
      protected long readMajorTypeWithSize​(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.
      long readMapLength()
      Prolog to reading a map of key-value pairs in CBOR format.
      Object readNull()
      Reads a null-value in CBOR format.
      byte readSimpleValue()
      Reads a single byte value in CBOR format.
      int readSmallInt()
      Reads a signed or unsigned small (<= 23) integer value in CBOR format.
      long readTag()
      Reads a semantic tag value in CBOR format.
      String readTextString()
      Reads an UTF-8 encoded string value in CBOR format.
      long readTextStringLength()
      Prolog to reading an UTF-8 encoded string value in CBOR format.
      protected long readUInt​(int length, boolean breakAllowed)
      Reads an unsigned integer with a given length-indicator.
      protected int readUInt16()
      Reads an unsigned 16-bit integer value
      protected long readUInt32()
      Reads an unsigned 32-bit integer value
      protected long readUInt64()
      Reads an unsigned 64-bit integer value
      protected int readUInt8()
      Reads an unsigned 8-bit integer value
      protected long readUIntExact​(int expectedLength, int length)
      Reads an unsigned integer with a given length-indicator.
      Object readUndefined()
      Reads an undefined value in CBOR format.
    • Constructor Detail

      • CborDecoder

        public CborDecoder​(InputStream is)
        Creates a new CborDecoder instance.
        Parameters:
        is - the actual input stream to read the CBOR-encoded data from, cannot be null.
    • 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 null in 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 IOException
        Prolog 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 IOException
        Reads 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.
      • readBreak

        public Object readBreak()
                         throws IOException
        Reads a "break"/stop 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.
      • readByteString

        public byte[] readByteString()
                              throws IOException
        Reads 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.
      • readByteStringLength

        public long readByteStringLength()
                                  throws IOException
        Prolog 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:
        IOException - in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
      • readDouble

        public double readDouble()
                          throws IOException
        Reads a double-precision float value in CBOR format.
        Returns:
        the read double value, values from Float.MIN_VALUE to Float.MAX_VALUE are supported.
        Throws:
        IOException - in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
      • readFloat

        public float readFloat()
                        throws IOException
        Reads a single-precision float value in CBOR format.
        Returns:
        the read float value, values from Float.MIN_VALUE to Float.MAX_VALUE are supported.
        Throws:
        IOException - in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
      • readHalfPrecisionFloat

        public double readHalfPrecisionFloat()
                                      throws IOException
        Reads a half-precision float value in CBOR format.
        Returns:
        the read half-precision float value, values from Float.MIN_VALUE to Float.MAX_VALUE are supported.
        Throws:
        IOException - in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
      • readInt

        public long readInt()
                     throws IOException
        Reads a signed or unsigned integer value in CBOR format.
        Returns:
        the read integer value, values from Long.MIN_VALUE to Long.MAX_VALUE are supported.
        Throws:
        IOException - in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
      • readInt16

        public int readInt16()
                      throws IOException
        Reads a signed or unsigned 16-bit integer value in CBOR format. read the small integer value, values from [-65536..65535] are supported.
        Throws:
        IOException - in case of I/O problems reading the CBOR-encoded value from the underlying output stream.
      • readInt32

        public long readInt32()
                       throws IOException
        Reads a signed or unsigned 32-bit integer value in CBOR format. read the small integer value, values in the range [-4294967296..4294967295] are supported.
        Throws:
        IOException - in case of I/O problems reading the CBOR-encoded value from the underlying output stream.
      • readInt64

        public long readInt64()
                       throws IOException
        Reads a signed or unsigned 64-bit integer value in CBOR format. read the small integer value, values from Long.MIN_VALUE to Long.MAX_VALUE are supported.
        Throws:
        IOException - in case of I/O problems reading the CBOR-encoded value from the underlying output stream.
      • readInt8

        public int readInt8()
                     throws IOException
        Reads a signed or unsigned 8-bit integer value in CBOR format. read the small integer value, values in the range [-256..255] are supported.
        Throws:
        IOException - in case of I/O problems reading the CBOR-encoded value from the underlying output stream.
      • readMapLength

        public long readMapLength()
                           throws IOException
        Prolog to reading a map of key-value pairs in CBOR format.
        Returns:
        the number of entries in the map, >= 0.
        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 a null-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.
      • readSimpleValue

        public byte readSimpleValue()
                             throws IOException
        Reads a single byte value in CBOR format.
        Returns:
        the read byte value.
        Throws:
        IOException - in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
      • readSmallInt

        public int readSmallInt()
                         throws IOException
        Reads a signed or unsigned small (<= 23) integer value in CBOR format. read the small integer value, values in the range [-24..23] are supported.
        Throws:
        IOException - in case of I/O problems reading the CBOR-encoded value from the underlying output stream.
      • readTag

        public long readTag()
                     throws IOException
        Reads a semantic tag value in CBOR format.
        Returns:
        the read tag value.
        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.
      • readTextStringLength

        public long readTextStringLength()
                                  throws IOException
        Prolog 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:
        IOException - in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
      • readUndefined

        public Object readUndefined()
                             throws IOException
        Reads an undefined 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.
      • expectIntegerType

        protected long expectIntegerType​(int ib)
                                  throws IOException
        Reads the next major type from the underlying input stream, and verifies whether it matches the given expectation.
        Parameters:
        ib - the expected major type, cannot be null (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 IOException
        Reads the next major type from the underlying input stream, and verifies whether it matches the given expectation.
        Parameters:
        majorType - the expected major type, cannot be null (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 IOException
        Reads the next major type from the underlying input stream, and verifies whether it matches the given expectations.
        Parameters:
        majorType - the expected major type, cannot be null (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 IOException
        Reads 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 be null (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 IOException
        Reads an unsigned integer with a given length-indicator.
        Parameters:
        length - the length indicator to use;
        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 IOException
        Reads an unsigned 16-bit integer value
        Returns:
        value the read value, values from Long.MIN_VALUE to Long.MAX_VALUE are supported.
        Throws:
        IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • readUInt32

        protected long readUInt32()
                           throws IOException
        Reads an unsigned 32-bit integer value
        Returns:
        value the read value, values from Long.MIN_VALUE to Long.MAX_VALUE are supported.
        Throws:
        IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • readUInt64

        protected long readUInt64()
                           throws IOException
        Reads an unsigned 64-bit integer value
        Returns:
        value the read value, values from Long.MIN_VALUE to Long.MAX_VALUE are supported.
        Throws:
        IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • readUInt8

        protected int readUInt8()
                         throws IOException
        Reads an unsigned 8-bit integer value
        Returns:
        value the read value, values from Long.MIN_VALUE to Long.MAX_VALUE are supported.
        Throws:
        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 IOException
        Reads an unsigned integer with a given length-indicator.
        Parameters:
        length - the length indicator to use;
        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.