Package org.javalite.common
Class Util
java.lang.Object
org.javalite.common.Util
- Author:
- Igor Polevoy, Eric Nielsen
-
Method Summary
Modifier and TypeMethodDescriptionString[]
Convenience method to create literal String arrays.static boolean
Returns true if value is either null or it's String representation is blank.static byte[]
bytes(InputStream in)
Reads contents of the input stream fully and returns it as byte array.static void
closeQuietly(AutoCloseable autoCloseable)
Closes a resource and swallows exception if thrown during a close.static void
closeQuietly(AutoCloseable... autoCloseable)
Close multiple resources.static <T extends AutoCloseable>
voidcloseQuietly(List<T> autoCloseable)
Close multiple resources.static boolean
createTree(Path path)
Creates directories recursively.static boolean
Returns true if array is either null or empty.static boolean
empty(Collection<?> collection)
Returns true if collection is either null or empty.static byte[]
fromBase64(String input)
Will decode Base64-encoded string back into byte array.static String
getCauseMessage(Throwable throwable)
This is stolen...ehrr...static Field
Gets a field from a class.getResourceLines(String resourceName)
Returns lines of text of a resource as list.static String
getStackTraceString(Throwable throwable)
Converts stack trace to string.static String
Joins the items in array with a delimiter.static void
join(StringBuilder sb, Object[] array, String delimiter)
Joins the items in array with a delimiter, and appends the result to StringBuilder.static void
join(StringBuilder sb, Collection<?> collection, String delimiter)
Joins the items in collection with a delimiter, and appends the result to StringBuilder.static String
join(Collection<?> collection, String delimiter)
Joins the items in collection with a delimiter.static void
joinAndRepeat(StringBuilder sb, String str, String delimiter, int count)
Repeats string of characters a defined number of times with a delimiter, and appends result to StringBuilder.static byte[]
Reads file into a byte array.static String
read(InputStream in)
Reads contents of the input stream fully and returns it as String.static String
read(InputStream in, String charset)
Reads contents of the input stream fully and returns it as String.static String
Reads contents of file fully and returns as string.static String
Reads contents of file fully and returns as string.static String
Reads contents of file fully and returns as string.static String
Reads contents of file fully and returns as string.static Properties
readProperties(String fileOrResource)
Reads a property file from classpath or from a file system to a properties object.static String
readResource(String resourceName)
Reads contents of resource fully into a string.static String
readResource(String resourceName, String charset)
Reads contents of resource fully into a string.static byte[]
readResourceBytes(String resourceName)
Reads contents of resource fully into a byte array.static void
recursiveDelete(Path directory)
Deletes a directory recursively with all its contents.static void
repeat(StringBuilder sb, String str, int count)
Repeats string of characters a defined number of times, and appends result to StringBuilder.static void
Saves content of byte array to file.static void
saveTo(String path, InputStream in)
Saves content read from input stream into a file.static String[]
Splits a string into an array using provided delimiter.static String[]
Splits a string into an array using provided delimiters.static String
toBase64(byte[] input)
Will encode byte array using Base64 encoding.
-
Method Details
-
readResourceBytes
Reads contents of resource fully into a byte array.- Parameters:
resourceName
- resource name.- Returns:
- entire contents of resource as byte array.
-
readResource
Reads contents of resource fully into a string. Sets UTF-8 encoding internally.- Parameters:
resourceName
- resource name.- Returns:
- entire contents of resource as string.
-
readResource
Reads contents of resource fully into a string.- Parameters:
resourceName
- resource name.charset
- name of supported charset- Returns:
- entire contents of resource as string.
-
readFile
Reads contents of file fully and returns as string.- Parameters:
fileName
- file name.- Returns:
- contents of entire file.
-
readFile
Reads contents of file fully and returns as string.- Parameters:
file
- file to read fully- Returns:
- contents of entire file.
-
readFile
Reads contents of file fully and returns as string.- Parameters:
fileName
- file name.charset
- name of supported charset.- Returns:
- contents of entire file.
-
readFile
Reads contents of file fully and returns as string.- Parameters:
file
- file to ready fully.charset
- name of supported charset.- Returns:
- contents of entire file.
-
closeQuietly
Closes a resource and swallows exception if thrown during a close.- Parameters:
autoCloseable
- resource to close
-
closeQuietly
Close multiple resources. CallscloseQuietly(AutoCloseable)
under the hood.- Parameters:
autoCloseable
- array of instances ofjava.lang.AutoCloseable
.
-
closeQuietly
Close multiple resources. CallscloseQuietly(AutoCloseable)
under the hood.- Parameters:
autoCloseable
- array of instances ofjava.lang.AutoCloseable
.
-
read
Reads contents of the input stream fully and returns it as String. Sets UTF-8 encoding internally.- Parameters:
in
- InputStream to read from.- Returns:
- contents of the input stream fully as String.
- Throws:
IOException
- in case of IO error
-
read
Reads contents of the input stream fully and returns it as String.- Parameters:
in
- InputStream to read from.charset
- name of supported charset to use- Returns:
- contents of the input stream fully as String.
- Throws:
IOException
- in case of IO error
-
bytes
Reads contents of the input stream fully and returns it as byte array.- Parameters:
in
- InputStream to read from.- Returns:
- contents of the input stream fully as byte array
- Throws:
IOException
- in case of IO error
-
read
Reads file into a byte array.- Parameters:
file
- file to read.- Returns:
- content of file.
- Throws:
IOException
-
getResourceLines
Returns lines of text of a resource as list.- Parameters:
resourceName
- name of resource- Returns:
- list of text lines
- Throws:
IOException
- in case of IO error
-
blank
Returns true if value is either null or it's String representation is blank.- Parameters:
value
- object to check.- Returns:
- true if value is either null or it's String representation is blank, otherwise returns false.
-
empty
Returns true if array is either null or empty.- Parameters:
array
- array to check- Returns:
- true if array is either null or empty, false otherwise
-
empty
Returns true if collection is either null or empty.- Parameters:
collection
- collection to check- Returns:
- true if collection is either null or empty, false otherwise
-
join
Joins the items in array with a delimiter.- Parameters:
array
- array of items to join.delimiter
- delimiter to insert between elements of array.- Returns:
- string with array elements separated by delimiter. There is no trailing delimiter in the string.
-
split
Splits a string into an array using provided delimiters. Empty (but not blank) split chunks are omitted. The split chunks are trimmed.- Parameters:
input
- string to split.delimiters
- delimiters- Returns:
- a string split into an array using provided delimiters
-
split
Splits a string into an array using provided delimiter. Empty (but not blank) split chunks are omitted. The split chunks are trimmed.- Parameters:
input
- string to split.delimiter
- delimiter- Returns:
- a string split into an array using a provided delimiter
-
join
Joins the items in collection with a delimiter.- Parameters:
collection
- collection of items to join.delimiter
- delimiter to insert between elements of collection.- Returns:
- string with collection elements separated by delimiter. There is no trailing delimiter in the string.
-
join
Joins the items in collection with a delimiter, and appends the result to StringBuilder.- Parameters:
sb
- StringBuilder to append result tocollection
- collection of items to join.delimiter
- delimiter to insert between elements of collection.
-
join
Joins the items in array with a delimiter, and appends the result to StringBuilder.- Parameters:
sb
- StringBuilder to append result toarray
- array of items to join.delimiter
- delimiter to insert between elements of array.
-
repeat
Repeats string of characters a defined number of times, and appends result to StringBuilder.- Parameters:
sb
- StringBuilder to append result tostr
- string of characters to be repeated.count
- number of times to repeat, zero or a negative number produces no result
-
joinAndRepeat
Repeats string of characters a defined number of times with a delimiter, and appends result to StringBuilder.For example, joinAndRepeat(sb, "?", ",", 3) will append "?,?,?" to sb.
- Parameters:
sb
- StringBuilder to append result tostr
- string of characters to be repeated.delimiter
- delimiter to insert between repeated items.count
- number of times to repeat, zero or a negative number produces no result
-
saveTo
Saves content read from input stream into a file.- Parameters:
path
- path to file.in
- input stream to read content from.
-
getCauseMessage
This is stolen...ehrr... borrowed from Apache ExceptionUtils- Parameters:
throwable
-- Returns:
-
getStackTraceString
Converts stack trace to string.- Parameters:
throwable
- - throwable to convert.- Returns:
- message and stack trace converted to string.
-
saveTo
Saves content of byte array to file.- Parameters:
path
- path to file - can be absolute or relative to current.content
- bytes to save.
-
toBase64
Will encode byte array using Base64 encoding.- Parameters:
input
- bytes to encode- Returns:
- encoded string
-
fromBase64
Will decode Base64-encoded string back into byte array.- Parameters:
input
- Base64-encoded string.- Returns:
- byte array decoded from string.
-
arr
Convenience method to create literal String arrays. Helps to replace code like this:String[] t = new String[]{"one", "two"}
String[] t = arr("one", "two");
- Parameters:
params
- strings to create array- Returns:
- array of strings
-
readProperties
Reads a property file from classpath or from a file system to a properties object. The path can look like:/opt/database.properties
. If this is found on classpath, it is loaded first. If not found on classpath, it will look for the file on te file system using the same path.- Parameters:
fileOrResource
- full path to a property file on classpath or a path to a file on file system. Classpath is searched first.- Returns:
java.util.Properties
object initialized from the file.- Throws:
IOException
-
createTree
Creates directories recursively. If a directory already exists, it will be silently ignored.- Returns:
- true if created all dirs, false if did not.
-
recursiveDelete
Deletes a directory recursively with all its contents. Will ignore files and directories if they disappear during the operation.- Parameters:
directory
- directory to delete.- Throws:
IOException
-
getField
Gets a field from a class. Will traverse to super classes for it.- Parameters:
fieldName
- name of a field to searchclazz
- - class to interrogate- Returns:
- fild if found, null if not.
-