Class CBORUtil

java.lang.Object
org.webpki.cbor.CBORUtil

public class CBORUtil extends Object
Utility methods.
  • Method Details

    • createDateTime

      public static CBORString createDateTime(OffsetDateTime offsetDateTime, boolean subsecond, boolean utc)
      Create DateTime object.
      This method creates a date/time string in the ISO format described in section 5.6 of [RFC3339]. The string is subsequently wrapped in a CBORString object.
      If offsetDateTime is not within the range "1970-01-01T00:00:00Z" to "9999-12-31T23:59:59Z", a CBORException is thrown .
      If subsecond is true the date/time string will feature up to 9 digits of subsecond data (.nnnnnnnnn) as well.
      Sample code:
      CBORString dateTime = CBORUtil.createDateTime(OffsetDateTime.now(), true, false);
      System.out.println(dateTime.toString());  // Diagnostic notation
      "2025-12-05T13:55:42.418+01:00"
      Parameters:
      offsetDateTime - DateTime source object.
      subsecond -
      If subsecond is true, the subsecond data of the offsetDateTime object will be featured in the created time object. If subsecond is false roundimg is performed.
      utc -
      If utc is true, the UTC time zone (denoted by a terminating Z) will be used, else the local time followed by the UTC offset (±hh:mm) will be used.
      Returns:
      CBORString
      Throws:
      CBORException
      See Also:
    • createEpochTime

      public static CBORObject createEpochTime(Instant instant, boolean subsecond)
      Create EpochTime object.
      This method creates an Epoch [TIME] time stamp.
      If instant is not within the range 0 ("1970-01-01T00:00:00Z") to 253402300799 ("9999-12-31T23:59:59Z"), a CBORException is thrown.
      If subsecond is true a CBORFloat object holding seconds with a milliseconds fraction will be created, else a CBORInt object holding seconds will be created.
      Sample code:
      CBORObject epochTime = CBORUtil.createEpochTime(Instant.now(), false);
      System.out.println(epochTime.toString());  // Diagnostic notation
      1764939916
      Parameters:
      instant - Time source object.
      subsecond -
      If subsecond is true, the milliseconds of instant will be featured in the created time object. Note: if the millisecond part of instant is zero, subsecond is considered to be false.
      If subsecond is false, the millisecond part of instant will not be used, but may after rounding, add a second to the created time object. Rounding is performed for milliseconds ≥500.
      Returns:
      CBORObject
      Throws:
      CBORException
      See Also:
    • concatByteArrays

      public static byte[] concatByteArrays(byte[]... listOfArrays)
      Concatenate a set of byte-arrays.
      Parameters:
      listOfArrays - Sort of self-explanatory.
      Returns:
      byte[]
    • unsignedLongToByteArray

      public static byte[] unsignedLongToByteArray(long value)
      Convert long to byte[].
      Parameters:
      value - Value to be converted.
      Returns:
      byte[]