Class CborEncoder


  • public class CborEncoder
    extends Object
    Provides an encoder capable of encoding data into CBOR format to a given OutputStream.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void writeArrayStart​(int length)
      Writes the start of a definite-length array.
      void writeBoolean​(boolean value)
      Writes a boolean value in canonical CBOR format.
      void writeByteString​(byte[] bytes)
      Writes a byte string in canonical CBOR-format.
      void writeInt​(long value)
      Writes a signed or unsigned integer value in canonical CBOR format, that is, tries to encode it in a little bytes as possible.
      void writeNull()
      Writes a null value in canonical CBOR format.
      protected void writeSimpleType​(int majorType, int value)
      Encodes and writes the major type and value as a simple type.
      protected void writeString​(int majorType, byte[] bytes)
      Writes a byte string in canonical CBOR-format.
      void writeTextString​(String value)
      Writes an UTF-8 string in canonical CBOR-format.
      protected void writeType​(int majorType, long value)
      Encodes and writes the major type indicator with a given payload (length).
      protected void writeUInt​(int mt, long value)
      Encodes and writes an unsigned integer value, that is, tries to encode it in a little bytes as possible.
      protected void writeUInt16​(int mt, int value)
      Encodes and writes an unsigned 16-bit integer value
      protected void writeUInt32​(int mt, int value)
      Encodes and writes an unsigned 32-bit integer value
      protected void writeUInt64​(int mt, long value)
      Encodes and writes an unsigned 64-bit integer value
      protected void writeUInt8​(int mt, int value)
      Encodes and writes an unsigned 8-bit integer value
    • Constructor Detail

      • CborEncoder

        public CborEncoder​(OutputStream os)
        Creates a new CborEncoder instance.
        Parameters:
        os - the actual output stream to write the CBOR-encoded data to, cannot be null.
    • Method Detail

      • writeArrayStart

        public void writeArrayStart​(int length)
                             throws IOException
        Writes the start of a definite-length array.

        After calling this method, one is expected to write the given number of array elements, which can be of any type. No length checks are performed.

        Parameters:
        length - the number of array elements to write, should >= 0.
        Throws:
        IllegalArgumentException - in case the given length was negative;
        IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • writeBoolean

        public void writeBoolean​(boolean value)
                          throws IOException
        Writes a boolean value in canonical CBOR format.
        Parameters:
        value - the boolean to write.
        Throws:
        IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • writeByteString

        public void writeByteString​(byte[] bytes)
                             throws IOException
        Writes a byte string in canonical CBOR-format.
        Parameters:
        bytes - the byte string to write, can be null in which case a byte-string of length 0 is written.
        Throws:
        IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • writeInt

        public void writeInt​(long value)
                      throws IOException
        Writes a signed or unsigned integer value in canonical CBOR format, that is, tries to encode it in a little bytes as possible.
        Parameters:
        value - the value to write, 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.
      • writeNull

        public void writeNull()
                       throws IOException
        Writes a null value in canonical CBOR format.
        Throws:
        IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • writeTextString

        public void writeTextString​(String value)
                             throws IOException
        Writes an UTF-8 string in canonical CBOR-format.

        Note that this method is platform specific, as the given string value will be encoded in a byte array using the platform encoding! This means that the encoding must be standardized and known.

        Parameters:
        value - the UTF-8 string to write, can be null in which case an UTF-8 string of length 0 is written.
        Throws:
        IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • writeSimpleType

        protected void writeSimpleType​(int majorType,
                                       int value)
                                throws IOException
        Encodes and writes the major type and value as a simple type.
        Parameters:
        majorType - the major type of the value to write, denotes what semantics the written value has;
        value - the value to write, values from [0..31] are supported.
        Throws:
        IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • writeString

        protected void writeString​(int majorType,
                                   byte[] bytes)
                            throws IOException
        Writes a byte string in canonical CBOR-format.
        Parameters:
        majorType - the major type of the string, should be either 0x40 or 0x60;
        bytes - the byte string to write, can be null in which case a byte-string of length 0 is written.
        Throws:
        IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • writeType

        protected void writeType​(int majorType,
                                 long value)
                          throws IOException
        Encodes and writes the major type indicator with a given payload (length).
        Parameters:
        majorType - the major type of the value to write, denotes what semantics the written value has;
        value - the value to write, 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.
      • writeUInt

        protected void writeUInt​(int mt,
                                 long value)
                          throws IOException
        Encodes and writes an unsigned integer value, that is, tries to encode it in a little bytes as possible.
        Parameters:
        mt - the major type of the value to write, denotes what semantics the written value has;
        value - the value to write, 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.
      • writeUInt16

        protected void writeUInt16​(int mt,
                                   int value)
                            throws IOException
        Encodes and writes an unsigned 16-bit integer value
        Parameters:
        mt - the major type of the value to write, denotes what semantics the written value has;
        value - the value to write, 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.
      • writeUInt32

        protected void writeUInt32​(int mt,
                                   int value)
                            throws IOException
        Encodes and writes an unsigned 32-bit integer value
        Parameters:
        mt - the major type of the value to write, denotes what semantics the written value has;
        value - the value to write, 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.
      • writeUInt64

        protected void writeUInt64​(int mt,
                                   long value)
                            throws IOException
        Encodes and writes an unsigned 64-bit integer value
        Parameters:
        mt - the major type of the value to write, denotes what semantics the written value has;
        value - the value to write, 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.
      • writeUInt8

        protected void writeUInt8​(int mt,
                                  int value)
                           throws IOException
        Encodes and writes an unsigned 8-bit integer value
        Parameters:
        mt - the major type of the value to write, denotes what semantics the written value has;
        value - the value to write, 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.