Class Util


  • public final class Util
    extends Object
    Author:
    Igor Polevoy, Eric Nielsen
    • 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.
      • closeQuietly

        public static void closeQuietly​(AutoCloseable autoCloseable)
        Closes a resource and swallows exception if thrown during a close.
        Parameters:
        autoCloseable - resource to close
      • closeQuietly

        public static void closeQuietly​(AutoCloseable... autoCloseable)
        Close multiple resources. Calls closeQuietly(AutoCloseable) under the hood.
        Parameters:
        autoCloseable - array of instances of java.lang.AutoCloseable.
      • closeQuietly

        public static <T extends AutoCloseable> void closeQuietly​(List<T> autoCloseable)
        Close multiple resources. Calls closeQuietly(AutoCloseable) under the hood.
        Parameters:
        autoCloseable - array of instances of java.lang.AutoCloseable.
      • 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.
      • getCauseMessage

        public static String getCauseMessage​(Throwable throwable)
        This is stolen...ehrr... borrowed from Apache ExceptionUtils
        Parameters:
        throwable -
        Returns:
      • 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.
      • arr

        public String[] arr​(String... params)
        Convenience method to create literal String arrays. Helps to replace code like this:

        String[] t = new String[]{"one", "two"}

        with:

        String[] t = arr("one", "two");

        Parameters:
        params - strings to create array
        Returns:
        array of strings
      • readProperties

        public static Properties readProperties​(String fileOrResource)
                                         throws IOException
        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

        public static boolean createTree​(Path path)
        Creates directories recursively. If a directory already exists, it will be silently ignored.
        Returns:
        true if created all dirs, false if did not.
      • recursiveDelete

        public static void recursiveDelete​(Path directory)
                                    throws IOException
        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

        public static Field getField​(String fieldName,
                                     Class clazz)
        Gets a field from a class. Will traverse to super classes for it.
        Parameters:
        fieldName - name of a field to search
        clazz - - class to interrogate
        Returns:
        fild if found, null if not.