Class Utils


  • public class Utils
    extends java.lang.Object
    File utilities.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void copyFiles​(java.io.File source, java.io.File dest)
      Copies a File into File in binary mode.
      static int copyStreams​(java.io.InputStream is, java.io.OutputStream os)
      Copies an InputStream into on OutputStream byte-to-byte.
      static int copyStreams​(java.io.InputStream is, java.io.OutputStream os, int bufferSize)
      Copies an InputStream into on OutputStream byte-to-byte.
      static TypeAwareByteSource getFile​(java.io.File file)
      Locates and reads a file either by a direct path or by a relative path in classpath.
      static TypeAwareByteSource getFile​(java.net.URI uri)
      Locates and reads a file either by a direct path or by a relative path in classpath.
      static ByteSource getFileAsByteSource​(java.io.File file)
      Locates and reads a file either by a direct path or by a relative path in classpath.
      static java.net.URL getResource​(java.lang.String path)
      Finds a resource with a given name.
      static java.io.InputStream getResourceAsStream​(java.lang.String path)
      Returns an input stream for reading the specified resource.
      static byte[] readFileToBytes​(java.io.File file)
      Reads a File into an array of bytes.
      static byte[] readInputStreamToBytes​(java.io.InputStream is)
      Reads an InputStream into an array of bytes.
      static byte[] readInputStreamToBytes​(java.io.InputStream is, int bufferSize)
      Reads an InputStream into an array of bytes.
      static java.lang.String readInputStreamToString​(java.io.InputStream is)
      Reads an InputStream into a String.
      • Methods inherited from class java.lang.Object

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

      • readInputStreamToString

        @Nonnull
        public static java.lang.String readInputStreamToString​(@Nonnull
                                                               java.io.InputStream is)
                                                        throws java.io.IOException
        Reads an InputStream into a String. Uses UTF-8 encoding and 10 kilobytes buffer by default.
        Parameters:
        is - a stream to read from
        Returns:
        the result
        Throws:
        java.io.IOException - in case of a read error
      • readInputStreamToBytes

        public static byte[] readInputStreamToBytes​(@Nonnull
                                                    java.io.InputStream is)
                                             throws java.io.IOException
        Reads an InputStream into an array of bytes. Uses 10 kilobytes buffer by default.
        Parameters:
        is - a stream to read from
        Returns:
        the result
        Throws:
        java.io.IOException - in case of a read error
      • copyStreams

        public static int copyStreams​(@Nonnull
                                      java.io.InputStream is,
                                      @Nonnull
                                      java.io.OutputStream os,
                                      int bufferSize)
                               throws java.io.IOException
        Copies an InputStream into on OutputStream byte-to-byte.
        Parameters:
        is - a stream to read from
        os - a stream to write to
        bufferSize - size of read buffer in bytes
        Returns:
        bytes copied
        Throws:
        java.io.IOException - in case of a read error
      • copyStreams

        public static int copyStreams​(@Nonnull
                                      java.io.InputStream is,
                                      @Nonnull
                                      java.io.OutputStream os)
                               throws java.io.IOException
        Copies an InputStream into on OutputStream byte-to-byte.
        Parameters:
        is - a stream to read from
        os - a stream to write to
        Returns:
        bytes copied
        Throws:
        java.io.IOException - in case of a read error
      • readInputStreamToBytes

        public static byte[] readInputStreamToBytes​(@Nonnull
                                                    java.io.InputStream is,
                                                    int bufferSize)
                                             throws java.io.IOException
        Reads an InputStream into an array of bytes.
        Parameters:
        is - a stream to read from
        bufferSize - size of read buffer in bytes
        Returns:
        the result
        Throws:
        java.io.IOException - in case of a read error
      • readFileToBytes

        public static byte[] readFileToBytes​(@Nonnull
                                             java.io.File file)
                                      throws java.io.IOException
        Reads a File into an array of bytes.
        Parameters:
        file - a file to read
        Returns:
        the result
        Throws:
        java.io.IOException - in case of a read error, or a file not found
      • getResourceAsStream

        @Nonnull
        public static java.io.InputStream getResourceAsStream​(@Nonnull
                                                              java.lang.String path)
                                                       throws java.io.FileNotFoundException
        Returns an input stream for reading the specified resource.
        Parameters:
        path - resource name or path to the resource
        Returns:
        readable stream for the resource
        Throws:
        java.io.FileNotFoundException - if no resource found
      • getResource

        @Nonnull
        public static java.net.URL getResource​(@Nonnull
                                               java.lang.String path)
                                        throws java.io.FileNotFoundException
        Finds a resource with a given name.
        Parameters:
        path - resource name or path to the resource
        Returns:
        location reference
        Throws:
        java.io.FileNotFoundException - if no resource found
      • getFileAsByteSource

        public static ByteSource getFileAsByteSource​(@Nonnull
                                                     java.io.File file)
                                              throws java.io.IOException
        Locates and reads a file either by a direct path or by a relative path in classpath.
        Parameters:
        file - a file to locate and read
        Returns:
        file data
        Throws:
        java.io.IOException - in case of a read error, or a file not found
      • getFile

        public static TypeAwareByteSource getFile​(@Nonnull
                                                  java.io.File file)
                                           throws java.io.IOException
        Locates and reads a file either by a direct path or by a relative path in classpath.
        Parameters:
        file - a file to locate and read
        Returns:
        file data and type
        Throws:
        java.io.IOException - in case of a read error, or a file not found
      • getFile

        public static TypeAwareByteSource getFile​(@Nonnull
                                                  java.net.URI uri)
                                           throws java.io.IOException
        Locates and reads a file either by a direct path or by a relative path in classpath.
        Parameters:
        uri - a file to locate and read
        Returns:
        file data and type
        Throws:
        java.io.IOException - in case of a read error, or a file not found
      • copyFiles

        public static void copyFiles​(@Nonnull
                                     java.io.File source,
                                     @Nonnull
                                     java.io.File dest)
                              throws java.io.IOException
        Copies a File into File in binary mode.
        Parameters:
        source - a stream to read from
        dest - a stream to write to
        Throws:
        java.io.IOException - in case of a read/write error