Class CBORDecoder

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

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

  • Constructor Details

    • CBORDecoder

      public CBORDecoder(InputStream inputStream, int options, int maxInputLength)
      Create a customized CBOR decoder.

      This constructor presumes that the actual decoding is performed by one or more (for sequences only) calls to decodeWithOptions().

      Customization is provided through an options parameter. Multiple options can be combined using the binary OR-operator ("|"). A zero (0) sets the decoder default mode. The options are defined by the following constants:

      If the SEQUENCE_MODE option is defined, the following apply:
      • The decoder returns after having decoded a single CBOR object, while preparing for the next object.
      • If no data is found (EOF), null is returned (empty sequences are permitted).
      Note that data that has not yet been decoded, is not verified for correctness.
      By default, the decoder requires that CBOR maps conform to the Deterministic Encoding rules. The LENIENT_MAP_DECODING option forces the decoder to accept CBOR maps with arbitrary key ordering. Note that duplicate keys still cause a CBORException to be thrown.
      By default, the decoder requires that CBOR numbers conform to the Deterministic Encoding rules. The LENIENT_NUMBER_DECODING option forces the decoder to accept different representations of CBOR int, bigint, and float items, only limited by RFC 8949.
      By default, the decoder supports NaN and Infinity values. In case these variants are not applicable for the application in question, the REJECT_INVALID_FLOATS option causes such numbers to throw a CBORException.

      Exceeding maxInputLength throws a CBORException. It is recommendable setting this as low as possible, since malformed CBOR objects may request any amount of memory.

      Parameters:
      inputStream - Stream holding CBOR data.
      options - The decoder options.
      maxInputLength - Upper limit in bytes.
      See Also:
  • 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.
    • decode

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

      This conveniance method is identical to:

        new CBORDecoder(new ByteArrayInputStream(cbor), 0, cbor.length)
            .decodeWithOptions();
       

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