Class Lib


  • public class Lib
    extends java.lang.Object
    Miscellaneous helper functions.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String readFile​(java.io.File file)
      Read the content of the given file into a string, using UTF8 as default encoding
      static java.lang.String readFile​(java.io.File file, java.lang.String encoding)
      Read the content of the given file into a string.
      static java.lang.String readFile​(java.lang.String filename)
      Read the content of the given file into a string.
      static java.lang.String readReader​(java.io.Reader reader)
      Read the content of the given reader into a string.
      static java.lang.String readResource​(java.lang.String resource)
      Read a resource from classpath using the context classloader, using UTF8 as default encoding
      static java.lang.String readResource​(java.lang.String resource, java.lang.String encoding)
      Read a resource from classpath using the current thread classloader
      static java.lang.String readResource​(java.lang.String resource, java.lang.String encoding, java.lang.Class<?> clazz)
      Read a resource from classpath.
      static java.lang.String readStream​(java.io.InputStream stream)
      Read the content of the given stream into a string.
      static java.lang.String readStream​(java.io.InputStream stream, java.lang.String enconding)
      Read the content of the given stream into a string.
      static java.lang.String readStream​(java.io.InputStream stream, java.nio.charset.Charset charset)
      Reads the content of the given input stream into a string using the specified encoding.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • readFile

        public static java.lang.String readFile​(java.lang.String filename)
                                         throws java.io.IOException
        Read the content of the given file into a string.
        Parameters:
        filename - the name of the file to be read
        Throws:
        java.io.IOException - if an error occurs during read
        Since:
        7.6
        See Also:
        readFile(File)
      • readFile

        public static java.lang.String readFile​(java.io.File file)
                                         throws java.io.IOException
        Read the content of the given file into a string, using UTF8 as default encoding
        Parameters:
        file - the file to read
        Returns:
        file contents
        Throws:
        java.io.IOException - if an error occurs during read
        See Also:
        readFile(File, String)
      • readFile

        public static java.lang.String readFile​(java.io.File file,
                                                java.lang.String encoding)
                                         throws java.io.IOException
        Read the content of the given file into a string.
        Parameters:
        file - the file to be read
        encoding - encoding to use, may be null in which case UTF-8 is used as default
        Returns:
        the file contents or null if file is null or does not exist, or can't be read, or is not a file
        Throws:
        java.io.IOException - if an error occurs during read
      • readResource

        public static java.lang.String readResource​(java.lang.String resource)
                                             throws java.io.IOException
        Read a resource from classpath using the context classloader, using UTF8 as default encoding
        Parameters:
        resource - the resource name to read, must not be null
        Throws:
        java.io.IOException
        Since:
        7.10.0
        See Also:
        readResource(String, String)
      • readResource

        public static java.lang.String readResource​(java.lang.String resource,
                                                    java.lang.String encoding)
                                             throws java.io.IOException
        Read a resource from classpath using the current thread classloader
        Parameters:
        resource - the resource name to read, must not be null
        encoding - optional, may be null in which case UTF-8 is used as default
        Returns:
        read content or empty string if resource cannot be loaded
        Throws:
        java.io.IOException - if the resource stream cannot be read
        Since:
        7.7
        See Also:
        readResource(String, String, Class)
      • readResource

        public static java.lang.String readResource​(java.lang.String resource,
                                                    java.lang.String encoding,
                                                    java.lang.Class<?> clazz)
                                             throws java.io.IOException
        Read a resource from classpath.

        The implementation will attempt first to use the current thread classloader, and if the resource is not found it will use the classloader from the parameter class. This class parameter should be the class that is calling this method.

        Parameters:
        resource - the resource name to read, must not be null
        encoding - optional, may be null in which case UTF-8 is used as default
        clazz - optional parameter with the loading object class, for the fallback attempt
        Returns:
        read content or empty string if resource cannot be loaded
        Throws:
        java.io.IOException - if the resource stream cannot be read
        Since:
        8.0.3
      • readStream

        public static java.lang.String readStream​(java.io.InputStream stream)
                                           throws java.io.IOException
        Read the content of the given stream into a string.
        Parameters:
        stream - the contents to read
        Returns:
        the read content
        Throws:
        java.io.IOException - if the resource stream cannot be read
        Since:
        7.7
        See Also:
        readStream(InputStream, String)
      • readStream

        public static java.lang.String readStream​(java.io.InputStream stream,
                                                  java.lang.String enconding)
                                           throws java.io.IOException
        Read the content of the given stream into a string.
        Parameters:
        stream - the contents to read
        enconding - encoding to use, , may be null in which case UTF-8 is used as default
        Returns:
        the read content
        Throws:
        java.io.IOException - if the resource stream cannot be read
        Since:
        7.7
      • readStream

        public static java.lang.String readStream​(java.io.InputStream stream,
                                                  java.nio.charset.Charset charset)
                                           throws java.io.IOException
        Reads the content of the given input stream into a string using the specified encoding.
        Parameters:
        stream - the contents to read
        charset - the encoding to use, defaults to UTF-8 if null
        Returns:
        the read content
        Throws:
        java.io.IOException - if the resource stream cannot be read
        Since:
        9.2.12
      • readReader

        public static java.lang.String readReader​(java.io.Reader reader)
                                           throws java.io.IOException
        Read the content of the given reader into a string.
        Parameters:
        reader - the contents to read
        Returns:
        the read content
        Throws:
        java.io.IOException - if the resource stream cannot be read
        Since:
        7.7