Class CborEncoder

  • Direct Known Subclasses:
    ByteArrayCborEncoder

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

      Fields 
      Modifier and Type Field Description
      protected java.io.OutputStream m_os  
    • Constructor Summary

      Constructors 
      Constructor Description
      CborEncoder​(java.io.OutputStream os)
      Creates a new CborEncoder instance.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void writeArrayStart()
      Writes the start of an indefinite-length array.
      void writeArrayStart​(int length)
      Writes the start of a definite-length array.
      void writeBigInt​(java.math.BigInteger value)  
      void writeBigInts​(java.math.BigInteger[] value)  
      void writeBoolean​(boolean value)
      Writes a boolean value in canonical CBOR format.
      void writeBooleanObj​(java.lang.Boolean value)  
      void writeBreak()
      Writes a "break" stop-value in canonical CBOR format.
      void writeByteString​(byte[] bytes)
      Writes a byte string in canonical CBOR-format.
      void writeByteString​(byte[] bytes, int off, int len)  
      void writeByteStrings​(byte[][] value)  
      void writeByteStringStart()
      Writes the start of an indefinite-length byte string.
      void writeDouble​(double value)
      Writes a double-precision float value in canonical CBOR format.
      void writeEnumObj​(java.lang.Enum value)  
      void writeFloat​(float value)
      Writes a single-precision float value in canonical CBOR format.
      void writeHalfPrecisionFloat​(float value)
      Writes a half-precision float value in canonical CBOR format.
      void writeInstant​(java.time.Instant value)  
      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 writeInt16​(int value)
      Writes a signed or unsigned 16-bit integer value in CBOR format.
      void writeInt32​(long value)
      Writes a signed or unsigned 32-bit integer value in CBOR format.
      void writeInt64​(long value)
      Writes a signed or unsigned 64-bit integer value in CBOR format.
      void writeInt8​(int value)
      Writes a signed or unsigned 8-bit integer value in CBOR format.
      void writeIntObj​(java.lang.Integer value)  
      void writeIntObj​(java.lang.Long value)  
      void writeLongs​(long[] value)  
      void writeLongs​(java.util.List<java.lang.Long> value)  
      void writeMapStart()
      Writes the start of an indefinite-length map.
      void writeMapStart​(int length)
      Writes the start of a finite-length map.
      void writeNull()
      Writes a null value in canonical CBOR format.
      void writeObject​(CborEncodable object)  
      void writeObjects​(CborEncodable[] objects)  
      protected void writeSimpleType​(int majorType, int value)
      Encodes and writes the major type and value as a simple type.
      void writeSimpleValue​(byte simpleValue)
      Writes a simple value, i.e., an "atom" or "constant" value in canonical CBOR format.
      void writeSmallInt​(int value)
      Writes a signed or unsigned small (<= 23) integer value in CBOR format.
      protected void writeString​(int majorType, byte[] bytes)
      Writes a byte string in canonical CBOR-format.
      protected void writeString​(int majorType, byte[] bytes, int off, int len)  
      void writeTag​(long tag)
      Writes a semantic tag in canonical CBOR format.
      void writeTextString​(java.lang.String value)
      Writes an UTF-8 string in canonical CBOR-format.
      void writeTextStrings​(java.lang.String[] value)  
      void writeTextStringStart()
      Writes the start of an indefinite-length UTF-8 string.
      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
      void writeUndefined()
      Writes an "undefined" value in canonical CBOR format.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • m_os

        protected final java.io.OutputStream m_os
    • Constructor Detail

      • CborEncoder

        public CborEncoder​(java.io.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()
                             throws java.io.IOException
        Writes the start of an indefinite-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.

        After all array elements are written, one should write a single break value to end the array, see writeBreak().

        Throws:
        java.io.IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • writeArrayStart

        public void writeArrayStart​(int length)
                             throws java.io.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:
        java.lang.IllegalArgumentException - in case the given length was negative;
        java.io.IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • writeBoolean

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

        public void writeBreak()
                        throws java.io.IOException
        Writes a "break" stop-value in canonical CBOR format.
        Throws:
        java.io.IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • writeByteString

        public void writeByteString​(byte[] bytes)
                             throws java.io.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:
        java.io.IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • writeByteString

        public void writeByteString​(byte[] bytes,
                                    int off,
                                    int len)
                             throws java.io.IOException
        Throws:
        java.io.IOException
      • writeByteStringStart

        public void writeByteStringStart()
                                  throws java.io.IOException
        Writes the start of an indefinite-length byte string.

        After calling this method, one is expected to write the given number of string parts. No length checks are performed.

        After all string parts are written, one should write a single break value to end the string, see writeBreak().

        Throws:
        java.io.IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • writeDouble

        public void writeDouble​(double value)
                         throws java.io.IOException
        Writes a double-precision float value in canonical CBOR format.
        Parameters:
        value - the value to write, values from Double.MIN_VALUE to Double.MAX_VALUE are supported.
        Throws:
        java.io.IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • writeFloat

        public void writeFloat​(float value)
                        throws java.io.IOException
        Writes a single-precision float value in canonical CBOR format.
        Parameters:
        value - the value to write, values from Float.MIN_VALUE to Float.MAX_VALUE are supported.
        Throws:
        java.io.IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • writeHalfPrecisionFloat

        public void writeHalfPrecisionFloat​(float value)
                                     throws java.io.IOException
        Writes a half-precision float value in canonical CBOR format.
        Parameters:
        value - the value to write, values from Float.MIN_VALUE to Float.MAX_VALUE are supported.
        Throws:
        java.io.IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • writeInt

        public void writeInt​(long value)
                      throws java.io.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:
        java.io.IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • writeInt16

        public void writeInt16​(int value)
                        throws java.io.IOException
        Writes a signed or unsigned 16-bit integer value in CBOR format.
        Parameters:
        value - the value to write, values from [-65536..65535] are supported.
        Throws:
        java.io.IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • writeInt32

        public void writeInt32​(long value)
                        throws java.io.IOException
        Writes a signed or unsigned 32-bit integer value in CBOR format.
        Parameters:
        value - the value to write, values in the range [-4294967296..4294967295] are supported.
        Throws:
        java.io.IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • writeInt64

        public void writeInt64​(long value)
                        throws java.io.IOException
        Writes a signed or unsigned 64-bit integer value in CBOR format.
        Parameters:
        value - the value to write, values from Long.MIN_VALUE to Long.MAX_VALUE are supported.
        Throws:
        java.io.IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • writeInt8

        public void writeInt8​(int value)
                       throws java.io.IOException
        Writes a signed or unsigned 8-bit integer value in CBOR format.
        Parameters:
        value - the value to write, values in the range [-256..255] are supported.
        Throws:
        java.io.IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • writeMapStart

        public void writeMapStart()
                           throws java.io.IOException
        Writes the start of an indefinite-length map.

        After calling this method, one is expected to write any number of map entries, as separate key and value. Keys and values can both be of any type. No length checks are performed.

        After all map entries are written, one should write a single break value to end the map, see writeBreak().

        Throws:
        java.io.IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • writeMapStart

        public void writeMapStart​(int length)
                           throws java.io.IOException
        Writes the start of a finite-length map.

        After calling this method, one is expected to write any number of map entries, as separate key and value. Keys and values can both be of any type. No length checks are performed.

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

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

        public void writeSimpleValue​(byte simpleValue)
                              throws java.io.IOException
        Writes a simple value, i.e., an "atom" or "constant" value in canonical CBOR format.
        Parameters:
        simpleValue - the (unsigned byte) value to write, values from 32 to 255 are supported (though not enforced).
        Throws:
        java.io.IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • writeSmallInt

        public void writeSmallInt​(int value)
                           throws java.io.IOException
        Writes a signed or unsigned small (<= 23) integer value in CBOR format.
        Parameters:
        value - the value to write, values in the range [-24..23] are supported.
        Throws:
        java.io.IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • writeTag

        public void writeTag​(long tag)
                      throws java.io.IOException
        Writes a semantic tag in canonical CBOR format.
        Parameters:
        tag - the tag to write, should >= 0.
        Throws:
        java.lang.IllegalArgumentException - in case the given tag was negative;
        java.io.IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • writeTextString

        public void writeTextString​(java.lang.String value)
                             throws java.io.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 a null is written.
        Throws:
        java.io.IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • writeTextStringStart

        public void writeTextStringStart()
                                  throws java.io.IOException
        Writes the start of an indefinite-length UTF-8 string.

        After calling this method, one is expected to write the given number of string parts. No length checks are performed.

        After all string parts are written, one should write a single break value to end the string, see writeBreak().

        Throws:
        java.io.IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • writeUndefined

        public void writeUndefined()
                            throws java.io.IOException
        Writes an "undefined" value in canonical CBOR format.
        Throws:
        java.io.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 java.io.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:
        java.io.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 java.io.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 null is written.
        Throws:
        java.io.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,
                                   int off,
                                   int len)
                            throws java.io.IOException
        Throws:
        java.io.IOException
      • writeType

        protected void writeType​(int majorType,
                                 long value)
                          throws java.io.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:
        java.io.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 java.io.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:
        java.io.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 java.io.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:
        java.io.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 java.io.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:
        java.io.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 java.io.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:
        java.io.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 java.io.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:
        java.io.IOException - in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
      • writeTextStrings

        public void writeTextStrings​(java.lang.String[] value)
                              throws java.io.IOException
        Throws:
        java.io.IOException
      • writeByteStrings

        public void writeByteStrings​(byte[][] value)
                              throws java.io.IOException
        Throws:
        java.io.IOException
      • writeInstant

        public void writeInstant​(java.time.Instant value)
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • writeBigInt

        public void writeBigInt​(java.math.BigInteger value)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • writeBigInts

        public void writeBigInts​(java.math.BigInteger[] value)
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • writeIntObj

        public void writeIntObj​(java.lang.Long value)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • writeIntObj

        public void writeIntObj​(java.lang.Integer value)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • writeLongs

        public void writeLongs​(long[] value)
                        throws java.io.IOException
        Throws:
        java.io.IOException
      • writeLongs

        public void writeLongs​(java.util.List<java.lang.Long> value)
                        throws java.io.IOException
        Throws:
        java.io.IOException
      • writeBooleanObj

        public void writeBooleanObj​(java.lang.Boolean value)
                             throws java.io.IOException
        Throws:
        java.io.IOException
      • writeEnumObj

        public void writeEnumObj​(java.lang.Enum value)
                          throws java.io.IOException
        Throws:
        java.io.IOException