public final class Util extends Object
Modifier and Type | Method and Description |
---|---|
String[] |
arr(String... params)
Convenience method to create literal String arrays.
|
static boolean |
blank(Object value)
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)
Close multiple resources.
|
static void |
closeQuietly(AutoCloseable autoCloseable)
Closes a resource and swallows exception if thrown during a close.
|
static <T extends AutoCloseable> |
closeQuietly(List<T> autoCloseable)
Close multiple resources.
|
static boolean |
createTree(Path path)
Creates directories recursively.
|
static boolean |
empty(Collection<?> collection)
Returns true if collection is either null or empty.
|
static boolean |
empty(Object[] array)
Returns true if array 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 List<String> |
getResourceLines(String resourceName)
Returns lines of text of a resource as list.
|
static String |
getStackTraceString(Throwable throwable)
Converts stack trace to string.
|
static String |
join(Collection<?> collection,
String delimiter)
Joins the items in collection with a delimiter.
|
static String |
join(String[] array,
String delimiter)
Joins the items in array with a delimiter.
|
static void |
join(StringBuilder sb,
Collection<?> collection,
String delimiter)
Joins the items in collection with a delimiter, and appends the result to StringBuilder.
|
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 |
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[] |
read(File file)
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 |
readFile(String fileName)
Reads contents of file fully and returns as string.
|
static String |
readFile(String fileName,
String charset)
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 |
saveTo(String path,
byte[] content)
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[] |
split(String input,
char delimiter)
Splits a string into an array using provided delimiter.
|
static String[] |
split(String input,
String delimiters)
Splits a string into an array using provided delimiters.
|
static String |
toBase64(byte[] input)
Will encode byte array using Base64 encoding.
|
public static byte[] readResourceBytes(String resourceName)
resourceName
- resource name.public static String readResource(String resourceName)
resourceName
- resource name.public static String readResource(String resourceName, String charset)
resourceName
- resource name.charset
- name of supported charsetpublic static String readFile(String fileName)
fileName
- file name.public static String readFile(String fileName, String charset)
fileName
- file name.charset
- name of supported charset.public static void closeQuietly(AutoCloseable autoCloseable)
autoCloseable
- resource to closepublic static void closeQuietly(AutoCloseable... autoCloseable)
closeQuietly(AutoCloseable)
under the hood.autoCloseable
- array of instances of java.lang.AutoCloseable
.public static <T extends AutoCloseable> void closeQuietly(List<T> autoCloseable)
closeQuietly(AutoCloseable)
under the hood.autoCloseable
- array of instances of java.lang.AutoCloseable
.public static String read(InputStream in) throws IOException
in
- InputStream to read from.IOException
- in case of IO errorpublic static String read(InputStream in, String charset) throws IOException
in
- InputStream to read from.charset
- name of supported charset to useIOException
- in case of IO errorpublic static byte[] bytes(InputStream in) throws IOException
in
- InputStream to read from.IOException
- in case of IO errorpublic static byte[] read(File file) throws IOException
file
- file to read.IOException
public static List<String> getResourceLines(String resourceName) throws IOException
resourceName
- name of resourceIOException
- in case of IO errorpublic static boolean blank(Object value)
value
- object to check.public static boolean empty(Object[] array)
array
- array to checkpublic static boolean empty(Collection<?> collection)
collection
- collection to checkpublic static String join(String[] array, String delimiter)
array
- array of items to join.delimiter
- delimiter to insert between elements of array.public static String[] split(String input, String delimiters)
input
- string to split.delimiters
- delimiterspublic static String[] split(String input, char delimiter)
input
- string to split.delimiter
- delimiterpublic static String join(Collection<?> collection, String delimiter)
collection
- collection of items to join.delimiter
- delimiter to insert between elements of collection.public static void join(StringBuilder sb, Collection<?> collection, String delimiter)
sb
- StringBuilder to append result tocollection
- collection of items to join.delimiter
- delimiter to insert between elements of collection.public static void join(StringBuilder sb, Object[] array, String delimiter)
sb
- StringBuilder to append result toarray
- array of items to join.delimiter
- delimiter to insert between elements of array.public static void repeat(StringBuilder sb, String str, int count)
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 resultpublic static void joinAndRepeat(StringBuilder sb, String str, String delimiter, int count)
For example, joinAndRepeat(sb, "?", ",", 3) will append "?,?,?" to sb.
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 resultpublic static void saveTo(String path, InputStream in)
path
- path to file.in
- input stream to read content from.public static String getCauseMessage(Throwable throwable)
throwable
- public static String getStackTraceString(Throwable throwable)
throwable
- - throwable to convert.public static void saveTo(String path, byte[] content)
path
- path to file - can be absolute or relative to current.content
- bytes to save.public static String toBase64(byte[] input)
input
- bytes to encodepublic static byte[] fromBase64(String input)
input
- Base64-encoded string.public String[] arr(String... params)
String[] t = new String[]{"one", "two"}
String[] t = arr("one", "two");
params
- strings to create arraypublic static Properties readProperties(String fileOrResource) throws IOException
/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.fileOrResource
- full path to a property file on classpath or a path to a file on file system. Classpath
is searched first.java.util.Properties
object initialized from the file.IOException
public static boolean createTree(Path path)
public static void recursiveDelete(Path directory) throws IOException
directory
- directory to delete.IOException
Copyright © 2020 JavaLite. All rights reserved.