Class Convert

java.lang.Object
cl.obcom.desktopfx.util.Convert

public final class Convert extends Object
Utility methods that perform general conversions.
  • Method Details

    • strToChars

      public static char[] strToChars(String str)
      Converts a string to a character array or null.
      Parameters:
      str - the string to be converted to a character array.
      Returns:
      the array or null if str is null.
    • leftFill

      public static String leftFill(char chr, String text, int size)
      Returns the supplied text filled to its left to complete size.
      Parameters:
      chr - the character used to left-fill the text.
      text - the string text to be left-filled with chr.
      size - the minimum length of the resulting string.
      Returns:
      a copy of text filled to its left to complete size.
      Throws:
      NullPointerException - if text is null.
      IllegalArgumentException - if size is negative.
    • charsToBytes

      public static byte[] charsToBytes(char[] chars)
      Converts a character sequence to an UTF-8 byte array or null.
      Parameters:
      chars - the character sequence to convert to UTF-8 byte array.
      Returns:
      a byte array or null if chars is null.
    • charsToArray

      public static char[] charsToArray(CharSequence chars)
      Converts a character sequence to a character array or null.
      Parameters:
      chars - the character sequence to convert to character array.
      Returns:
      a character array or null if chars is null.
    • rightFill

      public static String rightFill(String text, char chr, int size)
      Returns the supplied text filled to its right to complete size.
      Parameters:
      text - the string text to be right-filled with chr.
      chr - the character used to right-fill the text.
      size - the minimum length of the resulting string.
      Returns:
      a copy of text filled to its right to complete size.
      Throws:
      NullPointerException - if text is null.
      IllegalArgumentException - if size is negative.
    • bytesToHex

      public static String bytesToHex(byte[] bytes)
      Converts a byte array to a Hexadecimal string representation.
      Parameters:
      bytes - the bytes to convert to a Hexadecimal representation.
      Returns:
      an hexadecimal string representation of the byte array.
      Throws:
      NullPointerException - if bytes is null.
    • hexToBytes

      public static byte[] hexToBytes(String str)
      Returns the byte array represented by supplied hexadecimal string.
      Parameters:
      str - the hexadecimal string to be converted.
      Returns:
      the byte array represented by supplied hexadecimal string.
      Throws:
      NullPointerException - if str is null.
      IllegalArgumentException - if str is invalid.
    • hexToNibble

      public static int hexToNibble(char hex)
      Returns the nibble (4-bits) of the supplied hexadecimal char.
      Parameters:
      hex - the hexadecimal character.
      Returns:
      the nibble (4-bits) of the supplied hex char.
      Throws:
      IllegalArgumentException - if hex is invalid.
    • gcalToXcal

      public static XMLGregorianCalendar gcalToXcal(GregorianCalendar gcal)
      Converts a GregorianCalendar to an XMLGregorianCalendar.
      Parameters:
      gcal - the Gregorian Calendar to be converted.
      Returns:
      an XMLGregorianCalendar created from gcal.
      Throws:
      NullPointerException - if gcal is null.
    • dateToXcal

      public static XMLGregorianCalendar dateToXcal(Date date)
      Converts a Date to an XMLGregorianCalendar.
      Parameters:
      date - the date instance to be converted.
      Returns:
      an XMLGregorianCalendar created from date.
      Throws:
      NullPointerException - if date is null.
    • dateToXcal

      public static XMLGregorianCalendar dateToXcal(LocalDateTime date)
      Converts a LocalDateTime to an XMLGregorianCalendar.
      Parameters:
      date - the local date time instance to be converted.
      Returns:
      an XMLGregorianCalendar created from date.
      Throws:
      NullPointerException - if date is null.
    • msecToXcal

      public static XMLGregorianCalendar msecToXcal(long msec)
      Converts UTC milliseconds to an XMLGregorianCalendar.
      Parameters:
      msec - the UTC milliseconds from the Epoch.
      Returns:
      an XMLGregorianCalendar created from msec.
    • msecToLocalDateTime

      public static LocalDateTime msecToLocalDateTime(long msec)
      Converts UTC milliseconds to a LocalDateTime.
      Parameters:
      msec - the UTC milliseconds from the Epoch.
      Returns:
      a LocalDateTime created from msec.
    • xcalToGcal

      public static GregorianCalendar xcalToGcal(XMLGregorianCalendar xcal)
      Converts an XMLGregorianCalendar to a GregorianCalendar.
      Parameters:
      xcal - the XML Schema Date/Time to be converted.
      Returns:
      a GregorianCalendar created from xcal.
      Throws:
      NullPointerException - if xcal is null.
    • xcalToDate

      public static Date xcalToDate(XMLGregorianCalendar xcal)
      Converts a XMLGregorianCalendar to a Date.
      Parameters:
      xcal - the XML Schema Date/Time to be converted.
      Returns:
      a Date created from xcal.
      Throws:
      NullPointerException - if xcal is null.
    • xcalToLocalDateTime

      public static LocalDateTime xcalToLocalDateTime(XMLGregorianCalendar xcal)
      Converts an XMLGregorianCalendar to a LocalDateTime.
      Parameters:
      xcal - the XML Schema Date/Time to be converted.
      Returns:
      a LocalDateTime created from xcal.
      Throws:
      NullPointerException - if xcal is null.
    • xcalToMsec

      public static long xcalToMsec(XMLGregorianCalendar xcal)
      Converts an XMLGregorianCalendar to UTC milliseconds.
      Parameters:
      xcal - the XML Schema Date/Time to be converted.
      Returns:
      an UTC milliseconds created from xcal.
      Throws:
      NullPointerException - if xcal is null.
    • strToCompressedBytes

      public static byte[] strToCompressedBytes(String str) throws IOException
      Compress the UTF-8 bytes of the supplied string.
      Parameters:
      str - the string to be compressed (can be null).
      Returns:
      the compressed bytes or null if str is null.
      Throws:
      IOException - if an I/O error occurs.
    • compressedBytesToStr

      public static String compressedBytesToStr(byte[] bytes) throws IOException
      Uncompress supplied compressed UTF-8 bytes into a string.
      Parameters:
      bytes - the compressed UTF-8 bytes (can be null).
      Returns:
      a string or null if bytes is null.
      Throws:
      IOException - if an I/O error occurs.
    • compressBytes

      public static byte[] compressBytes(byte[] bytes) throws IOException
      Compress the supplied bytes using the ZLIB algorithm.
      Parameters:
      bytes - the bytes to compress with ZLIB algorithm (can be null).
      Returns:
      the compressed bytes or null if bytes is null.
      Throws:
      IOException - if an I/O error occurs.
    • uncompressBytes

      public static byte[] uncompressBytes(byte[] bytes) throws IOException
      Uncompress the supplied bytes using the ZLIB algorithm.
      Parameters:
      bytes - the bytes to uncompress with ZLIB algorithm (can be null).
      Returns:
      the uncompress bytes or null if bytes is null.
      Throws:
      IOException - if an I/O error occurs.
    • serializeObject

      public static byte[] serializeObject(Object object) throws IOException
      Serialize the supplied object instance to a byte array.
      Parameters:
      object - the object to be serialized into a byte array.
      Returns:
      a byte array or null if object is null.
      Throws:
      IOException - if an I/O error occurs.
    • unserializeObject

      public static Object unserializeObject(byte[] bytes) throws IOException, ClassNotFoundException
      Unserialize the supplied byte array to an object instance.
      Parameters:
      bytes - the byte array to be unserialized into an object.
      Returns:
      an object or null if bytes is null.
      Throws:
      ClassNotFoundException - the class of the object was not found.
      IOException - if an I/O error occurs.
    • unserializeObject

      public static Object unserializeObject(InputStream input) throws IOException, ClassNotFoundException
      Unserialize the supplied input stream to an object instance.
      Parameters:
      input - the input stream containing the bytes to unserialize.
      Returns:
      the object unserialized from supplied input stream.
      Throws:
      NullPointerException - if input is null.
      ClassNotFoundException - the class of the object was not found.
      IOException - if an I/O error occurs.
    • objectToBytes

      public static byte[] objectToBytes(Serializable object) throws Exception
      Returns a byte array with the serialization of the supplied object.
      Parameters:
      object - the object instance to be converted to bytes.
      Returns:
      a byte array with the serialization of object.
      Throws:
      NullPointerException - if object is null.
      Exception - if an error occurs converting to byte array.
    • objectFromBytes

      public static <T> T objectFromBytes(byte[] bytes, Class<T> type) throws Exception
      Converts the supplied byte array to an object of specified type.
      Type Parameters:
      T - the type of the returned object.
      Parameters:
      bytes - the byte array to be unserialized.
      type - the class type of the returned object.
      Returns:
      an object instance of the specified type.
      Throws:
      NullPointerException - if an argument is null.
      Exception - if an error occurs converting bytes to object.