Class CBOREncoder

java.lang.Object
org.bouncycastle.cbor.CBOREncoder

public class CBOREncoder extends Object
An encoder producing Deterministically Encoded CBOR (RFC 8949, Sections 4.2.1 and 4.2.2). Integer arguments, lengths and tag numbers are always emitted in their shortest form, and only definite-length items can be produced, so any output of this class is deterministically encoded by construction.

The encoder does not enforce map key ordering itself - callers writing maps are responsible for presenting keys in bytewise lexicographic order of their encodings. The structures defined for C509 (draft-ietf-cose-cbor-encoded-cert-20) use no maps.

  • Constructor Summary

    Constructors
    Constructor
    Description
    Base constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    writeArrayHeader(int count)
    Write an array header (major type 4) for an array of the given number of elements.
    void
    writeBoolean(boolean value)
    Write a boolean simple value (0xf4 or 0xf5).
    void
    writeByteString(byte[] bytes)
    Write a byte string (major type 2).
    void
    writeByteString(byte[] bytes, int off, int len)
    Write a byte string (major type 2) from a slice of an array.
    void
    writeEncoded(byte[] encodedItem)
    Write a complete data item that has already been encoded.
    void
    writeInteger(long value)
    Write an integer (major type 0 or 1 according to sign).
    void
    Write an integer (major type 0 or 1 according to sign) of any magnitude CBOR supports (-2^64 to 2^64-1).
    void
    writeMapHeader(int count)
    Write a map header (major type 5) for a map of the given number of key/value pairs.
    void
    Write the simple value null (0xf6).
    void
    writeTag(long tagNumber)
    Write a tag (major type 6).
    void
    Write a text string (major type 3), encoded as UTF-8.
    void
    Write the simple value undefined (0xf7).
    void
    Write an unsigned integer (major type 0).

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • CBOREncoder

      public CBOREncoder(OutputStream out)
      Base constructor.
      Parameters:
      out - the stream the CBOR encoding is written to.
  • Method Details

    • writeUnsignedInteger

      public void writeUnsignedInteger(long value) throws IOException
      Write an unsigned integer (major type 0).
      Parameters:
      value - the value, which must be non-negative.
      Throws:
      IOException
    • writeInteger

      public void writeInteger(long value) throws IOException
      Write an integer (major type 0 or 1 according to sign).
      Throws:
      IOException
    • writeInteger

      public void writeInteger(BigInteger value) throws IOException
      Write an integer (major type 0 or 1 according to sign) of any magnitude CBOR supports (-2^64 to 2^64-1).
      Throws:
      IOException
    • writeByteString

      public void writeByteString(byte[] bytes) throws IOException
      Write a byte string (major type 2).
      Throws:
      IOException
    • writeByteString

      public void writeByteString(byte[] bytes, int off, int len) throws IOException
      Write a byte string (major type 2) from a slice of an array.
      Throws:
      IOException
    • writeTextString

      public void writeTextString(String text) throws IOException
      Write a text string (major type 3), encoded as UTF-8.
      Throws:
      IOException
    • writeArrayHeader

      public void writeArrayHeader(int count) throws IOException
      Write an array header (major type 4) for an array of the given number of elements. The elements themselves are written by the following calls.
      Throws:
      IOException
    • writeMapHeader

      public void writeMapHeader(int count) throws IOException
      Write a map header (major type 5) for a map of the given number of key/value pairs. The keys and values themselves are written by the following calls, and must be presented in deterministic key order.
      Throws:
      IOException
    • writeTag

      public void writeTag(long tagNumber) throws IOException
      Write a tag (major type 6). The tag content is written by the following call.
      Throws:
      IOException
    • writeBoolean

      public void writeBoolean(boolean value) throws IOException
      Write a boolean simple value (0xf4 or 0xf5).
      Throws:
      IOException
    • writeNull

      public void writeNull() throws IOException
      Write the simple value null (0xf6).
      Throws:
      IOException
    • writeUndefined

      public void writeUndefined() throws IOException
      Write the simple value undefined (0xf7).
      Throws:
      IOException
    • writeEncoded

      public void writeEncoded(byte[] encodedItem) throws IOException
      Write a complete data item that has already been encoded. The caller is responsible for the item being deterministically encoded - typically it was produced by this class or validated by CBORDecoder.readEncodedItem().
      Throws:
      IOException