Class CBORObject
- All Implemented Interfaces:
Cloneable,Comparable<CBORObject>
- Direct Known Subclasses:
CBORArray,CBORBoolean,CBORBytes,CBORFloat,CBORInt,CBORMap,CBORNonFinite,CBORNull,CBORSimple,CBORString,CBORTag
In this implementation "object" should be regarded as equivalent to the CBOR [RFC8949] term, "data item".
-
Method Summary
Modifier and TypeMethodDescriptionvoidCheck CBOR object for unread data.clone()Create deep copy of CBOR object.intcompareTo(CBORObject object) Compare CBOR objects for magnitude.byte[]encode()Encode (aka "serialize") CBOR object.encode(OutputStream outputStream) Encode (aka "serialize") CBOR object to a stream.booleanCompare CBOR objects for equality.getArray()Get handle to CBOR[](array) object.Get CBORintegerobject.booleanGet CBORboolobject.byte[]getBytes()Get CBORbstrobject.GetDateTimeobject.GetEpochTimeobject.doubleGet "extended" CBORfloatobject.floatGet CBORfloat16object.floatGet CBORfloat32object.doubleGet CBORfloatobject.Get CBORint128object.intgetInt16()Get CBORint16object.intgetInt32()Get CBORint32object.longgetInt53()Get CBORint53object.longgetInt64()Get CBORint64object.intgetInt8()Get CBORint8object.getMap()Get handle to CBOR{}(map) object.intGet CBOR#7.n(simple) object.Get CBORtstrobject.getTag()Get handle to CBOR#6.n(tag) object.Get CBORuint128object.intGet CBORuint16object.longGet CBORuint32object.longGet CBORuint64object.intgetUint8()Get CBORuint8object.inthashCode()Calculate hash code of CBOR object.booleanisNull()Check for CBORnull.scan()Scan CBOR object and mark it as read.toDiagnostic(boolean prettyPrint) Render CBOR object in Diagnostic Notation.toString()Render CBOR object in a pretty-printed form.
-
Method Details
-
encode
Encode (aka "serialize") CBOR object to a stream.Note: this method always produce data using Deterministic Encoding.
Note:
outputStreamis not closed after the encoding has been performed.- Parameters:
outputStream- Where to write data- Returns:
- The original
outputStream - See Also:
-
encode
public byte[] encode()Encode (aka "serialize") CBOR object.Note: this method always produce data using Deterministic Encoding.
- Returns:
- CBOR encoded
byte-array - See Also:
-
getBigInteger
Get CBORintegerobject.Get CBOR integers of any size.
If current object is not a
CBORInt, aCBORExceptionis thrown.- Returns:
BigInteger- Throws:
CBORException- See Also:
-
getInt128
Get CBORint128object.If current object is not a
CBORInt, or holds a value outside the range-0x80000000000000000000000000000000to0x7fffffffffffffffffffffffffffffff, aCBORExceptionis thrown.- Returns:
- 128-bit signed integer.
- Throws:
CBORException- See Also:
-
getUint128
Get CBORuint128object.If current object is not a
CBORInt, or holds a value outside the range0to0xffffffffffffffffffffffffffffffff, aCBORExceptionis thrown.- Returns:
- 128-bit unsigned integer.
- Throws:
CBORException- See Also:
-
getInt64
public long getInt64()Get CBORint64object.If current object is not a
CBORInt, or holds a value outside the range-0x8000000000000000to0x7fffffffffffffff, aCBORExceptionis thrown.- Returns:
long- Throws:
CBORException
-
getUint64
public long getUint64()Get CBORuint64object.If current object is not a
CBORInt, or holds a value outside the range0to0xffffffffffffffff, aCBORExceptionis thrown.- Returns:
long- Throws:
CBORException
-
getInt53
public long getInt53()Get CBORint53object.If current object is not a
CBORInt, or holds a value outside the JavaScript limitsNumber.MIN_SAFE_INTEGER(-9007199254740991) toNumber.MAX_SAFE_INTEGER(9007199254740991), aCBORExceptionis thrown.Since 53-bit integers are specific to JavaScript,
int53objects should be used with caution in cross-platform scenarios.- Returns:
long- Throws:
CBORException- See Also:
-
getInt32
public int getInt32()Get CBORint32object.If current object is not a
CBORInt, or holds a value outside the range-0x80000000to0x7fffffff, aCBORExceptionis thrown.- Returns:
int- Throws:
CBORException- See Also:
-
getUint32
public long getUint32()Get CBORuint32object.If current object is not a
CBORInt, or holds a value outside the range0to0xffffffff, aCBORExceptionis thrown.- Returns:
long- Throws:
CBORException- See Also:
-
getInt16
public int getInt16()Get CBORint16object.If current object is not a
CBORInt, or holds a value outside the range-0x8000to0x7fff, aCBORExceptionis thrown.- Returns:
int- Throws:
CBORException- See Also:
-
getUint16
public int getUint16()Get CBORuint16object.If current object is not a
CBORInt, or holds a value outside the range0to0xffff, aCBORExceptionis thrown.- Returns:
int- Throws:
CBORException- See Also:
-
getInt8
public int getInt8()Get CBORint8object.If current object is not a
CBORInt, or holds a value outside the range-0x80to0x7f, aCBORExceptionis thrown.- Returns:
int- Throws:
CBORException- See Also:
-
getUint8
public int getUint8()Get CBORuint8object.If current object is not a
CBORInt, or holds a value outside the range0to0xff, aCBORExceptionis thrown.- Returns:
int- Throws:
CBORException- See Also:
-
getExtendedFloat64
public double getExtendedFloat64()Get "extended" CBORfloatobject.If current object is not a
CBORFloatholding a 64, 32, or 16-bitIEEE754number, aCBORExceptionis thrown.Note that unlike
getFloat64(), this method also supports theDouble.NaN,Double.POSITIVE_INFINITY, andDouble.NEGATIVE_INFINITYnon-finite variants.- Returns:
double- Throws:
CBORException- See Also:
-
getFloat64
public double getFloat64()Get CBORfloatobject.If current object is not a
CBORFloatholding a 64, 32, or 16-bitIEEE754number, aCBORExceptionis thrown.Unlike
getExtendedFloat64(), this method only accepts "regular" floating-point numbers. This makes it adapted for CBOR protocols that do not considerNaNorInfinityas valid items. That is, the latter cause aCBORExceptionto be thrown.- Returns:
double- Throws:
CBORException
-
getFloat32
public float getFloat32()Get CBORfloat32object.If current object is not a
CBORFloatholding a 32-bit or 16-bitIEEE754number, aCBORExceptionis thrown.- Returns:
float- Throws:
CBORException- See Also:
-
getFloat16
public float getFloat16()Get CBORfloat16object.If current object is not a
CBORFloatholding a 16-bitIEEE754number, aCBORExceptionis thrown.- Returns:
float- Throws:
CBORException- See Also:
-
getSimple
public int getSimple()Get CBOR#7.n(simple) object.If current object is not a
CBORSimple, aCBORExceptionis thrown.- Returns:
int- Throws:
CBORException
-
getBoolean
public boolean getBoolean()Get CBORboolobject.If current object is not a
CBORBoolean, aCBORExceptionis thrown.- Returns:
boolean- Throws:
CBORException
-
isNull
public boolean isNull()Check for CBORnull.If current object is a
CBORNullthe call will returntrue, else it will returnfalse.Note that the object will only be considered as "read" (
checkForUnread()) if the object is aCBORNull.- Returns:
boolean
-
getString
Get CBORtstrobject.If current object is not a
CBORString, aCBORExceptionis thrown.- Returns:
String- Throws:
CBORException
-
getEpochTime
GetEpochTimeobject.Depending on the type of the current object, this method performs agetInt64()or agetFloat64(). The returned number is subsequently used for initiating anInstantobject.If not all of the following conditions are met, aCBORExceptionis thrown:- Returns:
Instant- Throws:
CBORException- See Also:
-
getDateTime
GetDateTimeobject.This method performs agetString(). The returned string is subsequently used for initiating anInstantobject.If not all of the following conditions are met, aCBORExceptionis thrown:- The underlying object is a
CBORString. - The string matches the ISO date/time format described in section 5.6 of [RFC3339].
- The optional sub-second field (
.nnn) features less than ten digits. - The date/time object is within the range:
"0000-01-01T00:00:00Z"to"9999-12-31T23:59:59Z".
- Returns:
Instant- Throws:
CBORException- See Also:
- The underlying object is a
-
getBytes
public byte[] getBytes()Get CBORbstrobject.If current object is not a
CBORBytes, aCBORExceptionis thrown.- Returns:
byteArray- Throws:
CBORException
-
getMap
Get handle to CBOR{}(map) object.If current object is not a
CBORMap, aCBORExceptionis thrown.Note: do not replace this method with a cast!
- Returns:
- CBOR
{}(map) object - Throws:
CBORException
-
getArray
Get handle to CBOR[](array) object.If current object is not a
CBORArray, aCBORExceptionis thrown.Note: do not replace this method with a cast!
- Returns:
- CBOR
[](array) object - Throws:
CBORException
-
getTag
Get handle to CBOR#6.n(tag) object.If current object is not a
CBORTag, aCBORExceptionis thrown.Note: do not replace this method with a cast!
- Returns:
- CBOR
#6.n(tag) object - Throws:
CBORException
-
scan
Scan CBOR object and mark it as read.This method sets the status of the current object as well as to possible child objects to "read".
- Returns:
this- See Also:
-
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 aCBORExceptionif this is not the case.- Throws:
CBORException- See Also:
-
equals
Compare CBOR objects for equality.The result is
trueif and only if the argument is notnulland is aCBORObject, and the associated binary encodings (as provided byencode()) are equivalent. -
compareTo
Compare CBOR objects for magnitude.The comparison is based on the associated binary encodings as provided by
encode().- Specified by:
compareToin interfaceComparable<CBORObject>- Parameters:
object- Argument to compare with
-
hashCode
public int hashCode()Calculate hash code of CBOR object.The hash is calculated in the same way as for
String.hashCode(), using the output fromencode()as"s". -
toDiagnostic
Render CBOR object in Diagnostic Notation.If current object (as well as possible child objects), conforms to the subset of data types supported by JSON, this method can also be used to generate JSON data.
- Parameters:
prettyPrint- Iftruewhite space is added to make the result easier to read. Iffalseelements are output without additional white space (=single line).
-
toString
Render CBOR object in a pretty-printed form.Equivalent to
toDiagnostic(boolean)with the argument set totrue. -
clone
Create deep copy of CBOR object.Note that the copy is assumed to be "unread" (
checkForUnread()).
-