Class CBORDecoder

java.lang.Object
org.webpki.cbor.CBORDecoder

public class CBORDecoder extends Object
CBOR decoder class.
  • Constructor Details

  • Method Details

    • decodeWithOptions

      public CBORObject decodeWithOptions()
      Decode CBOR data with options.
      Returns:
      CBORObject or null (for EOF sequences only).
      Throws:
      CBORException - For decoding errors.
    • getByteCount

      public int getByteCount()
      Get CBOR decoder byte count.

      This is equivalent to the position of the next item to be read.

      Returns:
      The number of bytes read so far.
    • setInputLength

      public CBORDecoder setInputLength(int maxLength)
      Set CBOR decoder input max length.

      By default the CBOR decoder accepts up to (Integer.MAX_VALUE bytes of input data. Since malformed CBOR objects can request arbitrary amounts of memory, it is highly recommended to select a value that is adapted to the actual application. This method enables overriding the default.

      Parameters:
      maxLength - Maximum length of CBOR input in bytes. Exceeding this limit will cause a CBORException to be thrown.
      Returns:
      this (updated CBORDecoder object)
    • setNaNSupport

      public CBORDecoder setNaNSupport(boolean accept)
      Set CBOR decoder NaN/Infinity support.

      By default the decoder supports NaN, Infinity, and -Infinity. In case these variants are not applicable for the application in question, this method enables overriding the default.

      Parameters:
      accept - If the accept flag is set to false, the mentioned exceptional floating point values will (if encountered), cause a CBORException to be thrown.
      Returns:
      this (updated CBORDecoder object)
    • setDeterministicMode

      public CBORDecoder setDeterministicMode(boolean enforce)
      Set CBOR decoder deterministic mode.

      By default the decoder assumes that CBOR data conforms to the Deterministic Encoding rules. This method enables overriding the default.

      Parameters:
      enforce - If the enforce flag is set to false, the decoder will accept CBOR data that does not adhere to the map sorting and preferred number serialization requirements. This option may be needed for dealing with "legacy" CBOR implementations. Note: duplicate keys and other invalid (or not supported) CBOR constructs will still cause a CBORException to be thrown.
      Returns:
      this (updated CBORDecoder object)
    • setSequenceMode

      public CBORDecoder setSequenceMode(boolean sequence)
      Set CBOR decoder sequence mode.

      By default the decoder assumes that CBOR data constitutes of a single CBOR object. This method enables overriding the default.

      Also see CBORSequenceBuilder.

      Parameters:
      sequence - If the sequence flag is set to true, the following apply:
      • Immediately return after decoding a CBOR object, while preparing the decoder for the next item. See also getByteCount().
      • If no data is found (EOF), null is returned (empty sequences are permitted).
      • Note that data succeeding a just decoded CBOR object is not verified for correctness.
      Returns:
      this (updated CBORDecoder object)
    • decode

      public static CBORObject decode(byte[] cbor)
      Decode CBOR data.

      This conveniance method is identical to:

        new CBORDecoder(new ByteArrayInputStream(cbor))
            .setInputLength(cbor.length)
            .decodeWithOptions();
       

      Parameters:
      cbor - CBOR binary data holding exactly one CBOR object.
      Returns:
      CBORObject
      Throws:
      CBORException - For decoding errors.