Class IOUtils


  • public final class IOUtils
    extends java.lang.Object
    Utility class for I/O operations.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void closeQuietly​(java.io.Closeable closeable)
      Null-safe method that calls Closeable.close() and chokes the IOException.
      static void copy​(java.io.InputStream source, java.io.File destination)
      Copies the content of an InputStream into a destination File.
      static long copy​(java.io.InputStream input, java.io.OutputStream output)
      Copies the content of a InputStream into an OutputStream.
      static long copy​(java.io.InputStream input, java.io.OutputStream output, int buffersize)
      Copies the entire content of the given InputStream into the given OutputStream.
      static java.io.File[] filesContainedIn​(java.io.File source)
      Given a source File, return its direct descendants if the File is a directory.
      static java.lang.String relativePath​(java.io.File root, java.io.File node)
      Computes the path name of a file node relative to a given root node.
      static void requireDirectory​(java.io.File destination)
      Makes sure that the given File is either a writable directory, or that it does not exist and a directory can be created at its path.
      • Methods inherited from class java.lang.Object

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

      • copy

        public static void copy​(java.io.InputStream source,
                                java.io.File destination)
                         throws java.io.IOException
        Copies the content of an InputStream into a destination File.
        Parameters:
        source - the InputStream to copy
        destination - the target File
        Throws:
        java.io.IOException - if an error occurs
      • copy

        public static long copy​(java.io.InputStream input,
                                java.io.OutputStream output)
                         throws java.io.IOException
        Copies the content of a InputStream into an OutputStream. Uses a default buffer size of 8024 bytes.
        Parameters:
        input - the InputStream to copy
        output - the target Stream
        Returns:
        the amount of bytes written
        Throws:
        java.io.IOException - if an error occurs
      • copy

        public static long copy​(java.io.InputStream input,
                                java.io.OutputStream output,
                                int buffersize)
                         throws java.io.IOException
        Copies the entire content of the given InputStream into the given OutputStream.
        Parameters:
        input - the InputStream to copy
        output - the target Stream
        buffersize - the buffer size to use
        Returns:
        the amount of bytes written
        Throws:
        java.io.IOException - if an error occurs
      • relativePath

        public static java.lang.String relativePath​(java.io.File root,
                                                    java.io.File node)
                                             throws java.io.IOException
        Computes the path name of a file node relative to a given root node.
        If the root is /home/cdlflex/custom-ahy and the given node is /home/cdlflex/custom-ahy/assembly/pom.xml, the returned path name will be assembly/pom.xml.
        Parameters:
        root - the parent node
        node - the file node to compute the relative path for
        Returns:
        the path of node relative to root
        Throws:
        java.io.IOException - when an I/O error occurs during resolving the canonical path of the files
      • requireDirectory

        public static void requireDirectory​(java.io.File destination)
                                     throws java.io.IOException,
                                            java.lang.IllegalArgumentException
        Makes sure that the given File is either a writable directory, or that it does not exist and a directory can be created at its path.
        Will throw an exception if the given File is actually an existing file, or the directory is not writable
        Parameters:
        destination - the directory which to ensure its existence for
        Throws:
        java.io.IOException - if an I/O error occurs e.g. when attempting to create the destination directory
        java.lang.IllegalArgumentException - if the destination is an existing file, or the directory is not writable
      • closeQuietly

        public static void closeQuietly​(java.io.Closeable closeable)
        Null-safe method that calls Closeable.close() and chokes the IOException.
        Parameters:
        closeable - the object to close
      • filesContainedIn

        public static java.io.File[] filesContainedIn​(java.io.File source)
        Given a source File, return its direct descendants if the File is a directory. Otherwise return the File itself.
        Parameters:
        source - File or folder to be examined
        Returns:
        a File[] array containing the files inside this folder, or a size-1 array containing the file itself.