Package cl.obcom.desktopfx.util
Class Convert
java.lang.Object
cl.obcom.desktopfx.util.Convert
Utility methods that perform general conversions.
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
bytesToHex
(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 String
compressedBytesToStr
(byte[] bytes) Uncompress supplied compressed UTF-8 bytes into a string.static XMLGregorianCalendar
dateToXcal
(LocalDateTime date) Converts aLocalDateTime
to anXMLGregorianCalendar
.static XMLGregorianCalendar
dateToXcal
(Date date) Converts aDate
to anXMLGregorianCalendar
.static XMLGregorianCalendar
gcalToXcal
(GregorianCalendar gcal) Converts aGregorianCalendar
to anXMLGregorianCalendar
.static byte[]
hexToBytes
(String str) Returns the byte array represented by supplied hexadecimal string.static int
hexToNibble
(char hex) Returns the nibble (4-bits) of the supplied hexadecimal char.static String
Returns the supplied text filled to its left to complete size.static LocalDateTime
msecToLocalDateTime
(long msec) Converts UTC milliseconds to aLocalDateTime
.static XMLGregorianCalendar
msecToXcal
(long msec) Converts UTC milliseconds to anXMLGregorianCalendar
.static <T> T
objectFromBytes
(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 String
Returns 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 Object
unserializeObject
(byte[] bytes) Unserialize the supplied byte array to an object instance.static Object
unserializeObject
(InputStream input) Unserialize the supplied input stream to an object instance.static Date
Converts aXMLGregorianCalendar
to aDate
.static GregorianCalendar
Converts anXMLGregorianCalendar
to aGregorianCalendar
.static LocalDateTime
Converts anXMLGregorianCalendar
to aLocalDateTime
.static long
Converts anXMLGregorianCalendar
to 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
null
ifstr
isnull
.
-
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
- iftext
isnull
.IllegalArgumentException
- ifsize
is 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
null
ifchars
isnull
.
-
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
null
ifchars
isnull
.
-
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
- iftext
isnull
.IllegalArgumentException
- ifsize
is 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
- ifbytes
isnull
.
-
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
- ifstr
isnull
.IllegalArgumentException
- ifstr
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
- ifhex
is invalid.
-
gcalToXcal
Converts aGregorianCalendar
to anXMLGregorianCalendar
.- Parameters:
gcal
- the Gregorian Calendar to be converted.- Returns:
- an
XMLGregorianCalendar
created fromgcal
. - Throws:
NullPointerException
- ifgcal
isnull
.
-
dateToXcal
Converts aDate
to anXMLGregorianCalendar
.- Parameters:
date
- the date instance to be converted.- Returns:
- an
XMLGregorianCalendar
created fromdate
. - Throws:
NullPointerException
- ifdate
isnull
.
-
dateToXcal
Converts aLocalDateTime
to anXMLGregorianCalendar
.- Parameters:
date
- the local date time instance to be converted.- Returns:
- an
XMLGregorianCalendar
created fromdate
. - Throws:
NullPointerException
- ifdate
isnull
.
-
msecToXcal
Converts UTC milliseconds to anXMLGregorianCalendar
.- Parameters:
msec
- the UTC milliseconds from the Epoch.- Returns:
- an
XMLGregorianCalendar
created frommsec
.
-
msecToLocalDateTime
Converts UTC milliseconds to aLocalDateTime
.- Parameters:
msec
- the UTC milliseconds from the Epoch.- Returns:
- a
LocalDateTime
created frommsec
.
-
xcalToGcal
Converts anXMLGregorianCalendar
to aGregorianCalendar
.- Parameters:
xcal
- the XML Schema Date/Time to be converted.- Returns:
- a
GregorianCalendar
created fromxcal
. - Throws:
NullPointerException
- ifxcal
isnull
.
-
xcalToDate
Converts aXMLGregorianCalendar
to aDate
.- Parameters:
xcal
- the XML Schema Date/Time to be converted.- Returns:
- a
Date
created fromxcal
. - Throws:
NullPointerException
- ifxcal
isnull
.
-
xcalToLocalDateTime
Converts anXMLGregorianCalendar
to aLocalDateTime
.- Parameters:
xcal
- the XML Schema Date/Time to be converted.- Returns:
- a
LocalDateTime
created fromxcal
. - Throws:
NullPointerException
- ifxcal
isnull
.
-
xcalToMsec
Converts anXMLGregorianCalendar
to UTC milliseconds.- Parameters:
xcal
- the XML Schema Date/Time to be converted.- Returns:
- an UTC milliseconds created from
xcal
. - Throws:
NullPointerException
- ifxcal
isnull
.
-
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
null
ifstr
isnull
. - 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
null
ifbytes
isnull
. - 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
null
ifbytes
isnull
. - 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
null
ifbytes
isnull
. - 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
null
ifobject
isnull
. - 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
null
ifbytes
isnull
. - 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
- ifinput
isnull
.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
- ifobject
isnull
.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.
-