Class CBORUtil

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

public class CBORUtil extends Object
Utility methods.
  • Method Details

    • createDateTime

      public static CBORString createDateTime(Instant instant, boolean millis, 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 the instant object is not within the range "0000-01-01T00:00:00Z" to "9999-12-31T23:59:59Z", a CBORException is thrown .
      If millis is true the date/time string will feature milliseconds (.nnn) as well.
      Sample code:
      CBORString iso = CBORUtil.createDateTime(Instant.now(), true, false);
      System.out.println(iso.toString());
      "2025-12-05T13:55:42.418+01:00"
      Parameters:
      instant - Time source object.
      millis -
      If millis is true, the milliseconds of the instant object will be featured in the created time object. Note: if the millisecond part of the instant object is zero, millis is considered to be false.
      If millis is false, the millisecond part of the instant object will not be used, but may after rounding, add a second to the created time object. Rounding is performed for milliseconds ≥500, and always in the positive direction.
      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 millis)
      Create EpochTime object.
      This method creates an Epoch [TIME] time stamp.
      If the instant object is not within the range 0 ("1970-01-01T00:00:00Z") to 253402300799 ("9999-12-31T23:59:59Z"), a CBORException is thrown.
      If millis 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 epoch = CBORUtil.createEpochTime(Instant.now(), false);
      System.out.println(epoch.toString());
      1764939916
      Parameters:
      instant - Time source object.
      millis -
      If millis is true, the milliseconds of the instant object will be featured in the created time object. Note: if the millisecond part of the instant object is zero, millis is considered to be false.
      If millis is false, the millisecond part of the instant object will not be used, but may after rounding, add a second to the created time object. Rounding is performed for milliseconds ≥500, and always in the positive direction.
      Returns:
      CBORObject
      Throws:
      CBORException
      See Also:
    • concatByteArrays

      public static byte[] concatByteArrays(byte[]... listOfArrays)
    • unsignedLongToByteArray

      public static byte[] unsignedLongToByteArray(long value)
    • reverseBits

      public static long reverseBits(long bits, int fieldWidth)