Class Util

java.lang.Object
org.moera.lib.util.Util

public class Util extends Object
A utility class that provides a collection of various helper methods.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static byte[]
    Decodes a Base64-encoded string into its corresponding byte array.
    static String
    base64encode(byte[] bytes)
    Encodes the given byte array into a Base64-encoded string.
    static String
    base64urlencode(byte[] bytes)
    Encodes the given byte array into a Base64 URL-safe encoded string without padding.
    static String
    dump(byte[] bytes)
    Converts a byte array into a space-separated string of hexadecimal values.
    static String
    dumpShort(byte[] bytes)
    Converts a byte array into a condensed string representation.
    static String
    formatTimestamp(long timestamp)
    Formats a given timestamp (in epoch seconds) into a string representation using the ISO-8601 date and time format.
    static String
    hexByte(byte b)
    Converts a single byte into a two-character uppercase hexadecimal string.
    static boolean
    Checks whether a given string is either null or empty.
    static Timestamp
    now()
    Returns the current timestamp based on the system's current time.
    static Long
    Converts a Timestamp object to epoch seconds.
    static Timestamp
    toTimestamp(Long epochSecond)
    Converts a given epoch second value into a Timestamp.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Util

      public Util()
  • Method Details

    • now

      public static Timestamp now()
      Returns the current timestamp based on the system's current time.
      Returns:
      a Timestamp object representing the current time
    • toEpochSecond

      public static Long toEpochSecond(Timestamp timestamp)
      Converts a Timestamp object to epoch seconds.
      Parameters:
      timestamp - the Timestamp to be converted; may be null
      Returns:
      the number of seconds since the epoch, or null if the input is null
    • toTimestamp

      public static Timestamp toTimestamp(Long epochSecond)
      Converts a given epoch second value into a Timestamp. If the input is null, this method returns null.
      Parameters:
      epochSecond - the epoch second value to be converted; may be null
      Returns:
      a Timestamp object representing the given epoch second, or null if the input is null
    • formatTimestamp

      public static String formatTimestamp(long timestamp)
      Formats a given timestamp (in epoch seconds) into a string representation using the ISO-8601 date and time format.
      Parameters:
      timestamp - the timestamp in epoch seconds to format
      Returns:
      a string representation of the given timestamp in ISO-8601 format
    • base64encode

      public static String base64encode(byte[] bytes)
      Encodes the given byte array into a Base64-encoded string.
      Parameters:
      bytes - the byte array to be encoded; must not be null
      Returns:
      a Base64-encoded string representation of the input byte array
    • base64decode

      public static byte[] base64decode(String s)
      Decodes a Base64-encoded string into its corresponding byte array.
      Parameters:
      s - the Base64-encoded string to be decoded; must not be null
      Returns:
      the decoded byte array corresponding to the input string
    • base64urlencode

      public static String base64urlencode(byte[] bytes)
      Encodes the given byte array into a Base64 URL-safe encoded string without padding. If the input byte array is null, this method returns null.
      Parameters:
      bytes - the byte array to be encoded; may be null
      Returns:
      a Base64 URL-safe encoded string representation of the input byte array, or null if the input is null
    • dump

      public static String dump(byte[] bytes)
      Converts a byte array into a space-separated string of hexadecimal values. Each byte is formatted as a two-character hexadecimal string.
      Parameters:
      bytes - the byte array to be converted; must not be null
      Returns:
      a string representation of the input byte array
    • dumpShort

      public static String dumpShort(byte[] bytes)
      Converts a byte array into a condensed string representation. If the byte array length is 16 or less, the entire array is represented in hexadecimal format. Otherwise, the first and last 8 bytes are represented in hexadecimal format, separated by "...".
      Parameters:
      bytes - the byte array to be converted; must not be null
      Returns:
      a condensed string representation of the input byte array
    • hexByte

      public static String hexByte(byte b)
      Converts a single byte into a two-character uppercase hexadecimal string. If the byte is negative, it is treated as an unsigned value.
      Parameters:
      b - the byte to be converted
      Returns:
      a two-character hexadecimal string representation of the given byte
    • isEmpty

      public static boolean isEmpty(String s)
      Checks whether a given string is either null or empty.
      Parameters:
      s - the string to be checked; may be null
      Returns:
      true if the string is null or empty, false otherwise