Class Convert
java.lang.Object
cl.obcom.desktopfx.util.Convert
Utility methods that perform general conversions.
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringbytesToHex(byte[] bytes) Converts a byte array to a Hexadecimal string representation.static char[]charsToArray(CharSequence chars) Converts a character sequence to a character array ornull.static byte[]charsToBytes(char[] chars) Converts a character sequence to an UTF-8 byte array ornull.static byte[]compressBytes(byte[] bytes) Compress the supplied bytes using the ZLIB algorithm.static StringcompressedBytesToStr(byte[] bytes) Uncompress supplied compressed UTF-8 bytes into a string.static XMLGregorianCalendardateToXcal(LocalDateTime date) Converts aLocalDateTimeto anXMLGregorianCalendar.static XMLGregorianCalendardateToXcal(Date date) Converts aDateto anXMLGregorianCalendar.static XMLGregorianCalendargcalToXcal(GregorianCalendar gcal) Converts aGregorianCalendarto anXMLGregorianCalendar.static byte[]hexToBytes(String str) Returns the byte array represented by supplied hexadecimal string.static inthexToNibble(char hex) Returns the nibble (4-bits) of the supplied hexadecimal char.static StringReturns the supplied text filled to its left to complete size.static LocalDateTimemsecToLocalDateTime(long msec) Converts UTC milliseconds to aLocalDateTime.static XMLGregorianCalendarmsecToXcal(long msec) Converts UTC milliseconds to anXMLGregorianCalendar.static <T> TobjectFromBytes(byte[] bytes, Class<T> type) Converts the supplied byte array to an object of specified type.static byte[]objectToBytes(Serializable object) Returns a byte array with the serialization of the supplied object.static StringReturns the supplied text filled to its right to complete size.static byte[]serializeObject(Object object) Serialize the supplied object instance to a byte array.static char[]strToChars(String str) Converts a string to a character array ornull.static byte[]Compress the UTF-8 bytes of the supplied string.static byte[]uncompressBytes(byte[] bytes) Uncompress the supplied bytes using the ZLIB algorithm.static ObjectunserializeObject(byte[] bytes) Unserialize the supplied byte array to an object instance.static ObjectunserializeObject(InputStream input) Unserialize the supplied input stream to an object instance.static DateConverts aXMLGregorianCalendarto aDate.static GregorianCalendarConverts anXMLGregorianCalendarto aGregorianCalendar.static LocalDateTimeConverts anXMLGregorianCalendarto aLocalDateTime.static longConverts anXMLGregorianCalendarto UTC milliseconds.
-
Method Details
-
strToChars
Converts a string to a character array ornull.- Parameters:
str- the string to be converted to a character array.- Returns:
- the array or
nullifstrisnull.
-
leftFill
Returns the supplied text filled to its left to complete size.- Parameters:
chr- the character used to left-fill thetext.text- the string text to be left-filled withchr.size- the minimum length of the resulting string.- Returns:
- a copy of text filled to its left to complete size.
- Throws:
NullPointerException- iftextisnull.IllegalArgumentException- ifsizeis negative.
-
charsToBytes
public static byte[] charsToBytes(char[] chars) Converts a character sequence to an UTF-8 byte array ornull.- Parameters:
chars- the character sequence to convert to UTF-8 byte array.- Returns:
- a byte array or
nullifcharsisnull.
-
charsToArray
Converts a character sequence to a character array ornull.- Parameters:
chars- the character sequence to convert to character array.- Returns:
- a character array or
nullifcharsisnull.
-
rightFill
Returns the supplied text filled to its right to complete size.- Parameters:
text- the string text to be right-filled withchr.chr- the character used to right-fill thetext.size- the minimum length of the resulting string.- Returns:
- a copy of text filled to its right to complete size.
- Throws:
NullPointerException- iftextisnull.IllegalArgumentException- ifsizeis negative.
-
bytesToHex
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- ifbytesisnull.
-
hexToBytes
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- ifstrisnull.IllegalArgumentException- ifstris 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
hexchar. - Throws:
IllegalArgumentException- ifhexis invalid.
-
gcalToXcal
Converts aGregorianCalendarto anXMLGregorianCalendar.- Parameters:
gcal- the Gregorian Calendar to be converted.- Returns:
- an
XMLGregorianCalendarcreated fromgcal. - Throws:
NullPointerException- ifgcalisnull.
-
dateToXcal
Converts aDateto anXMLGregorianCalendar.- Parameters:
date- the date instance to be converted.- Returns:
- an
XMLGregorianCalendarcreated fromdate. - Throws:
NullPointerException- ifdateisnull.
-
dateToXcal
Converts aLocalDateTimeto anXMLGregorianCalendar.- Parameters:
date- the local date time instance to be converted.- Returns:
- an
XMLGregorianCalendarcreated fromdate. - Throws:
NullPointerException- ifdateisnull.
-
msecToXcal
Converts UTC milliseconds to anXMLGregorianCalendar.- Parameters:
msec- the UTC milliseconds from the Epoch.- Returns:
- an
XMLGregorianCalendarcreated frommsec.
-
msecToLocalDateTime
Converts UTC milliseconds to aLocalDateTime.- Parameters:
msec- the UTC milliseconds from the Epoch.- Returns:
- a
LocalDateTimecreated frommsec.
-
xcalToGcal
Converts anXMLGregorianCalendarto aGregorianCalendar.- Parameters:
xcal- the XML Schema Date/Time to be converted.- Returns:
- a
GregorianCalendarcreated fromxcal. - Throws:
NullPointerException- ifxcalisnull.
-
xcalToDate
Converts aXMLGregorianCalendarto aDate.- Parameters:
xcal- the XML Schema Date/Time to be converted.- Returns:
- a
Datecreated fromxcal. - Throws:
NullPointerException- ifxcalisnull.
-
xcalToLocalDateTime
Converts anXMLGregorianCalendarto aLocalDateTime.- Parameters:
xcal- the XML Schema Date/Time to be converted.- Returns:
- a
LocalDateTimecreated fromxcal. - Throws:
NullPointerException- ifxcalisnull.
-
xcalToMsec
Converts anXMLGregorianCalendarto UTC milliseconds.- Parameters:
xcal- the XML Schema Date/Time to be converted.- Returns:
- an UTC milliseconds created from
xcal. - Throws:
NullPointerException- ifxcalisnull.
-
strToCompressedBytes
Compress the UTF-8 bytes of the supplied string.- Parameters:
str- the string to be compressed (can be null).- Returns:
- the compressed bytes or
nullifstrisnull. - Throws:
IOException- if an I/O error occurs.
-
compressedBytesToStr
Uncompress supplied compressed UTF-8 bytes into a string.- Parameters:
bytes- the compressed UTF-8 bytes (can be null).- Returns:
- a string or
nullifbytesisnull. - Throws:
IOException- if an I/O error occurs.
-
compressBytes
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
nullifbytesisnull. - Throws:
IOException- if an I/O error occurs.
-
uncompressBytes
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
nullifbytesisnull. - Throws:
IOException- if an I/O error occurs.
-
serializeObject
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
nullifobjectisnull. - Throws:
IOException- if an I/O error occurs.
-
unserializeObject
Unserialize the supplied byte array to an object instance.- Parameters:
bytes- the byte array to be unserialized into an object.- Returns:
- an object or
nullifbytesisnull. - 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- ifinputisnull.ClassNotFoundException- the class of the object was not found.IOException- if an I/O error occurs.
-
objectToBytes
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- ifobjectisnull.Exception- if an error occurs converting to byte array.
-
objectFromBytes
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 isnull.Exception- if an error occurs converting bytes to object.
-