org.javalite.common
Class Util

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

public class Util
extends Object

Author:
Igor Polevoy

Constructor Summary
Util()
           
 
Method Summary
static boolean blank(Object value)
          Returns true if value is either null or it's String representation is empty.
static byte[] bytes(InputStream in)
          Reads contents of the input stream fully and returns it as byte array.
static void close(Closeable c)
           
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[] collection, String delimiter)
          Joins the items in collection with a delimiter.
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 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 a provided delimiter.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Util

public Util()
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.

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.
Returns:
contents of entire file.

close

public static void close(Closeable c)

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

Parameters:
value - object to check.
Returns:
true if value is either null or it's String representation is empty, otherwise returns false.

join

public static String join(String[] 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.

split

public static String[] split(String input,
                             char delimiter)
Splits a string into an array using a provided delimiter. The split chunks are also trimmed.

Parameters:
input - string to split.
delimiter - delimiter
Returns:
a string 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.

saveTo

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

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

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.


Copyright © 2013. All Rights Reserved.