org.javalite.common
Class Util

java.lang.Object
  extended by org.javalite.common.Util

public final class Util
extends Object

Author:
Igor Polevoy, Eric Nielsen

Method Summary
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(java.io.Closeable) 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(Closeable c)
           
static void closeQuietly(ResultSet rs)
           
static void closeQuietly(Statement st)
           
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

readResourceBytes

public static byte[] readResourceBytes(String resourceName)
Reads contents of resource fully into a byte array.

Parameters:
resourceName - resource name.
Returns:
entire contents of resource as byte array.

readResource

public static String readResource(String resourceName)
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

public static String readResource(String resourceName,
                                  String charset)
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

public static String readFile(String fileName)
Reads contents of file fully and returns as string.

Parameters:
fileName - file name.
Returns:
contents of entire file.

readFile

public static String readFile(String fileName,
                              String charset)
Reads contents of file fully and returns as string.

Parameters:
fileName - file name.
charset - name of supported charset.
Returns:
contents of entire file.

close

@Deprecated
public static void close(Closeable c)
Deprecated. use closeQuietly(java.io.Closeable) 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.


closeQuietly

public static void closeQuietly(Closeable c)

closeQuietly

public static void closeQuietly(ResultSet rs)

closeQuietly

public static void closeQuietly(Statement st)

read

public static String read(InputStream in)
                   throws IOException
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

public static String read(InputStream in,
                          String charset)
                   throws IOException
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

public static byte[] bytes(InputStream in)
                    throws IOException
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

public static byte[] read(File file)
                   throws IOException
Reads file into a byte array.

Parameters:
file - file to read.
Returns:
content of file.
Throws:
IOException

getResourceLines

public static List<String> getResourceLines(String resourceName)
                                     throws IOException
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

public static boolean blank(Object value)
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

public static boolean empty(Object[] array)
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

public static boolean empty(Collection<?> collection)
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

public static String join(String[] array,
                          String delimiter)
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

public static String[] split(String input,
                             String delimiters)
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

public static String[] split(String input,
                             char delimiter)
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

public static String join(Collection<?> collection,
                          String delimiter)
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

public static void join(StringBuilder sb,
                        Collection<?> collection,
                        String delimiter)
Joins the items in collection with a delimiter, and appends the result to StringBuilder.

Parameters:
sb - StringBuilder to append result to
collection - collection of items to join.
delimiter - delimiter to insert between elements of collection.

join

public static void join(StringBuilder sb,
                        Object[] array,
                        String delimiter)
Joins the items in array with a delimiter, and appends the result to StringBuilder.

Parameters:
sb - StringBuilder to append result to
array - array of items to join.
delimiter - delimiter to insert between elements of array.

repeat

public static void repeat(StringBuilder sb,
                          String str,
                          int count)
Repeats string of characters a defined number of times, and appends result to StringBuilder.

Parameters:
sb - StringBuilder to append result to
str - string of characters to be repeated.
count - number of times to repeat, zero or a negative number produces no result

joinAndRepeat

public 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.

For example, joinAndRepeat(sb, "?", ",", 3) will append "?,?,?" to sb.

Parameters:
sb - StringBuilder to append result to
str - 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

public static void saveTo(String path,
                          InputStream in)
Saves content read from input stream into a file.

Parameters:
path - path to file.
in - input stream to read content from.

getStackTraceString

public static String getStackTraceString(Throwable throwable)
Converts stack trace to string.

Parameters:
throwable - - throwable to convert.
Returns:
message and stack trace converted to string.

saveTo

public static void saveTo(String path,
                          byte[] content)
Saves content of byte array to file.

Parameters:
path - path to file - can be absolute or relative to current.
content - bytes to save.

toBase64

public static String toBase64(byte[] input)
Will encode byte array using Base64 encoding.

Parameters:
input - bytes to encode
Returns:
encoded string

fromBase64

public static byte[] fromBase64(String input)
Will decode Base64-encoded string back into byte array.

Parameters:
input - Base64-encoded string.
Returns:
byte array decoded from string.


Copyright © 2015 JavaLite. All rights reserved.