public final class Util extends Object
Modifier and Type | Method and Description |
---|---|
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 |
close(Closeable c)
Deprecated.
use
closeQuietly(AutoCloseable) instead. Two problems can arise if resources are not
closed quietly in the finally block: (1) If there are multiple close() calls, and one of the first ones throws
an Exception, then the following ones will never be called. (2) If an Exception is thrown inside the
try { ... } catch block and another Exception is thrown by a close() call in the finally { ... } block, then the
second Exception will hide the first one. |
static void |
closeQuietly(AutoCloseable autoCloseable)
Closes a resource and swallows exception if thrown during a close.
|
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 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 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 |
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.@Deprecated public static void close(Closeable c)
closeQuietly(AutoCloseable)
instead. Two problems can arise if resources are not
closed quietly in the finally block: (1) If there are multiple close() calls, and one of the first ones throws
an Exception, then the following ones will never be called. (2) If an Exception is thrown inside the
try { ... } catch block and another Exception is thrown by a close() call in the finally { ... } block, then the
second Exception will hide the first one.public static void closeQuietly(AutoCloseable autoCloseable)
autoCloseable
- resource to closepublic 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 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.Copyright © 2016 JavaLite. All rights reserved.