Class CBORObject

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

public abstract class CBORObject extends Object implements Cloneable, Comparable<CBORObject>
Base class for all CBOR objects.

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

  • Method Details

    • encode

      public byte[] encode()
      Encode CBOR object.

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

      Returns:
      CBOR encoded byteArray
    • getBigInteger

      public BigInteger getBigInteger()
      Get CBOR BigInteger value.

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

      Returns:
      BigInteger
      Throws:
      CBORException
    • getInt64

      public long getInt64()
      Get CBOR integer.

      This method requires that the object is a CBORInt and has a value ranging from -0x8000000000000000 to 0x7fffffffffffffff.

      Returns:
      long
      Throws:
      CBORException
    • getUint64

      public long getUint64()
      Get CBOR integer.

      This method requires that the object is a CBORInt and has a value ranging from 0 to 0xffffffffffffffff.

      Returns:
      long
      Throws:
      CBORException
    • getInt32

      public int getInt32()
      Get CBOR integer.

      This method requires that the object is a CBORInt and has a value ranging from -0x80000000 to 0x7fffffff.

      Returns:
      int
      Throws:
      CBORException
    • getUint32

      public long getUint32()
      Get CBOR integer.

      This method requires that the object is a CBORInt and has a value ranging from 0 to 0xffffffff.

      Returns:
      long
      Throws:
      CBORException
    • getInt16

      public int getInt16()
      Get CBOR integer.

      This method requires that the object is a CBORInt and has a value ranging from -0x8000 to 0x7fff.

      Returns:
      int
      Throws:
      CBORException
    • getUint16

      public int getUint16()
      Get CBOR integer.

      This method requires that the object is a CBORInt and has a value ranging from 0 to 0xffff.

      Returns:
      int
      Throws:
      CBORException
    • getInt8

      public int getInt8()
      Get CBOR integer.

      This method requires that the object is a CBORInt and has a value ranging from -0x80 to 0x7f.

      Returns:
      int
      Throws:
      CBORException
    • getUint8

      public int getUint8()
      Get CBOR integer.

      This method requires that the object is a CBORInt and has a value ranging from 0 to 0xff.

      Returns:
      int
      Throws:
      CBORException
    • getFloat64

      public double getFloat64()
      Get CBOR floating point value.

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

      Returns:
      double
      Throws:
      CBORException
    • getFloat32

      public float getFloat32()
      Get CBOR floating point 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
      Throws:
      CBORException
    • getFloat16

      public float getFloat16()
      Get CBOR floating point value.

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

      Returns:
      float
      Throws:
      CBORException
    • getBoolean

      public boolean getBoolean()
      Get CBOR boolean.

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

      Returns:
      boolean
      Throws:
      CBORException
    • isNull

      public boolean isNull()
      Check for CBOR 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()
      Get CBOR text string.

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

      Returns:
      String
      Throws:
      CBORException
    • getBytes

      public byte[] getBytes()
      Get CBOR byte string.

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

      Returns:
      byteArray
      Throws:
      CBORException
    • getMap

      public CBORMap getMap()
      Get CBOR map object.

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

      Returns:
      CBOR map object
      Throws:
      CBORException
    • getArray

      public CBORArray getArray()
      Get CBOR array object.

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

      Returns:
      CBOR array object
      Throws:
      CBORException
    • getTag

      public CBORTag getTag()
      Get CBOR tag object.

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

      Returns:
      CBOR tag object
      Throws:
      CBORException
    • scan

      public CBORObject scan()
      Scan CBOR object and mark it as read.

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

      See also checkForUnread().
      Returns:
      this
    • checkForUnread

      public void checkForUnread()
      Check CBOR object for unread data.

      Verifies that all objects 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.

      See also scan().
      Throws:
      CBORException
    • equals

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

      public int compareTo(CBORObject object)
      Compare CBOR objects for magnitude.
      Specified by:
      compareTo in interface Comparable<CBORObject>
    • hashCode

      public int hashCode()
      Calculate hash code of CBOR object.
      Overrides:
      hashCode in class Object
    • toDiagnosticNotation

      public String toDiagnosticNotation(boolean prettyPrint)
      Render 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()
      Render 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()
      Create deep copy of CBOR object.

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

      Overrides:
      clone in class Object