Class CBORObject

java.lang.Object
org.webpki.cbor.CBORObject
All Implemented Interfaces:
Cloneable
Direct Known Subclasses:
CBORArray, CBORBigInt, CBORBoolean, CBORBytes, CBORFloat, CBORInt, CBORMap, CBORNull, CBORString, CBORTag

public abstract class CBORObject extends Object implements Cloneable
Base class for all CBOR objects.

In this implementation "object" should be regarded as equivalent to the RFC 8949 "data item".

  • Method Details

    • getType

      public CBORTypes getType()
      Returns core CBOR type.
      Returns:
      CBOR core type
    • encode

      public byte[] encode()
      Encodes CBOR object.

      Note: this method always return CBOR data using Deterministic Encoding.

      Returns:
      CBOR encoded byteArray
    • getBigInteger

      public BigInteger getBigInteger()
      Returns BigInteger value.

      This method requires that the object is a CBORBigInt or CBORInt, otherwise a CBORException is thrown.

      Returns:
      BigInteger
    • getLong

      public long getLong()
      Returns long value.

      This method requires that the object is a CBORInt and fits a Java long (Long.MIN_VALUE to Long.MAX_VALUE), otherwise a CBORException is thrown.

      Also see getBigInteger().
      Returns:
      long
    • getUnsignedLong

      public long getUnsignedLong()
      Returns unsigned long value.

      This method requires that the object is an unsigned CBORInt, otherwise a CBORException is thrown.

      Returns:
      long
    • getInt

      public int getInt()
      Returns int value.

      This method requires that the object is a CBORInt and fits a Java int (Integer.MIN_VALUE to Integer.MAX_VALUE), otherwise a CBORException is thrown.

      Also see getBigInteger().
      Returns:
      int
    • getUnsignedInt

      public long getUnsignedInt()
      Returns unsigned int value.

      This method requires that the object is a CBORInt and fits a Java int (0 to 0xffffffff), otherwise a CBORException is thrown.

      Also see getBigInteger().
      Returns:
      long
    • getShort

      public int getShort()
      Returns short value.

      This method requires that the object is a CBORInt and fits a Java short (Short.MIN_VALUE to Short.MAX_VALUE), otherwise a CBORException is thrown.

      Also see getBigInteger().
      Returns:
      int
    • getUnsignedShort

      public int getUnsignedShort()
      Returns unsigned short value.

      This method requires that the object is a CBORInt and fits a Java short (0 to 0xffff), otherwise a CBORException is thrown.

      Also see getBigInteger().
      Returns:
      int
    • getByte

      public int getByte()
      Returns byte value.

      This method requires that the object is a CBORInt and fits a Java byte (Byte.MIN_VALUE to Byte.MAX_VALUE), otherwise a CBORException is thrown.

      Also see getBigInteger().
      Returns:
      int
    • getUnsignedByte

      public int getUnsignedByte()
      Returns unsigned byte value.

      This method requires that the object is a CBORInt and fits a Java byte (0 to 0xff), otherwise a CBORException is thrown.

      Also see getBigInteger().
      Returns:
      int
    • getDouble

      public double getDouble()
      Returns double value.

      This method requires that the object is a CBORFloat, otherwise a CBORException is thrown.

      Returns:
      double
    • getFloat

      public float getFloat()
      Returns float value.

      This method requires that the object is a CBORFloat holding a 16 or 32-bit IEEE 754 value, otherwise a CBORException is thrown.

      Returns:
      float
    • getBoolean

      public boolean getBoolean()
      Returns boolean value.

      This method requires that the object is a CBORBoolean, otherwise a CBORException is thrown.

      Returns:
      boolean
    • isNull

      public boolean isNull()
      Checks for null.

      If the object is a CBORNull the call will return true, else it will return false.

      Note that the object will only be considered as "read" (checkForUnread()) if the object is a CBORNull.

      Returns:
      boolean
    • getString

      public String getString()
      Returns text string value.

      This method requires that the object is a CBORString, otherwise a CBORException is thrown.

      Returns:
      String
    • getBytes

      public byte[] getBytes()
      Returns byte string value.

      This method requires that the object is a CBORBytes, otherwise a CBORException is thrown.

      Returns:
      byteArray
    • getMap

      public CBORMap getMap()
      Returns map object.

      This method requires that the object is a CBORMap, otherwise a CBORException is thrown.

      Returns:
      CBOR map object
    • getArray

      public CBORArray getArray()
      Returns array object.

      This method requires that the object is a CBORArray, otherwise a CBORException is thrown.

      Returns:
      CBOR array object
    • getTag

      public CBORTag getTag()
      Returns tag object.

      This method requires that the object is a CBORTag, otherwise a CBORException is thrown.

      Returns:
      CBOR tag object
    • scan

      public CBORObject scan()
      Scans object node and marks as read.

      This method sets the status of this object as well as to possible child objects to "read".

      Also see checkForUnread().
      Returns:
      this
    • checkForUnread

      public void checkForUnread()
      Checks for unread CBOR data.

      Verifies that all data from the current object including possible child objects have been read (through calling getBytes() etc.), and throws a CBORException if this is not the case.

      Also see scan().
    • decode

      public static CBORObject decode(InputStream inputStream, boolean sequenceFlag, boolean nonDeterministic, Integer maxLength)
      Decodes CBOR data with options.

      Also see CBORSequenceBuilder.

      Decoding errors throw CBORException.

      Parameters:
      inputStream - Stream holding CBOR data
      sequenceFlag - If true stop reading after decoding a CBOR object (no object returns null).
      nonDeterministic - If true disable Deterministic Encoding checks for number serialization and map sorting. See also CBORMap.setSortingMode(boolean).
      maxLength - Holds maximum input size in bytes or null (Integer.MAX_VALUE is assumed). Since malformed CBOR objects can request arbitrary amounts of memory, it is highly recommended setting maxLength to a value that is adapted to the actual application.
      Returns:
      CBORObject
    • decode

      public static CBORObject decode(byte[] cborData)
      Decodes CBOR data.

      This method is identical to:

        decode(new ByteArrayInputStream(cborData),
               false, 
               false,
               cborData.length);
      

      Decoding errors throw CBORException.

      Parameters:
      cborData - CBOR in its binary form
      Returns:
      CBORObject
    • equals

      public boolean equals(Object object)
      Checks CBOR objects for equality.
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toDiagnosticNotation

      public String toDiagnosticNotation(boolean prettyPrint)
      Returns the CBOR object in Diagnostic Notation.

      Parameters:
      prettyPrint - If true white space is added to make the result easier to read. If false elements are output without additional white space (=single line).

    • toString

      public String toString()
      Returns the CBOR object in a pretty-printed form.

      Equivalent to toDiagnosticNotation(boolean) with the argument set to true.

      Overrides:
      toString in class Object
    • clone

      public CBORObject clone()
      Deep copy of CBORObject.

      Note that the copy is assumed to be "unread" (checkForUnread()).

      Overrides:
      clone in class Object