Class IOUtils


  • public class IOUtils
    extends java.lang.Object
    The IOUtils provide some IO related helper methods.
    Author:
    Thomas Morgner.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void copyStreams​(java.io.InputStream in, java.io.OutputStream out)
      Copies the InputStream into the OutputStream, until the end of the stream has been reached.
      void copyStreams​(java.io.InputStream in, java.io.OutputStream out, int buffersize)
      Copies the InputStream into the OutputStream, until the end of the stream has been reached.
      void copyWriter​(java.io.Reader in, java.io.Writer out)
      Copies the contents of the Reader into the Writer, until the end of the stream has been reached.
      void copyWriter​(java.io.Reader in, java.io.Writer out, int buffersize)
      Copies the contents of the Reader into the Writer, until the end of the stream has been reached.
      java.lang.String createRelativePath​(java.lang.String targetFile, java.lang.String baseFile)
      Creates a relative url by stripping the common parts of the the url.
      java.lang.String createRelativeURL​(java.net.URL url, java.net.URL baseURL)
      Creates a relative url by stripping the common parts of the the url.
      java.io.File findFileOnClassPath​(java.lang.String name)
      Returns a reference to a file with the specified name that is located somewhere on the classpath.
      java.lang.String getAbsolutePath​(java.lang.String targetFile, java.lang.String baseFile)
      Computes the absolute filename for the target file using the baseFile as root directory.
      java.lang.String getFileExtension​(java.lang.String file)
      Returns the file extension of the given file name.
      java.lang.String getFileName​(java.lang.String path)
      Extracts the last file name from the given pathname.
      java.lang.String getFileName​(java.net.URL url)
      Extracts the file name from the URL.
      static IOUtils getInstance()
      Gets the singleton instance of the utility package.
      java.lang.String getPath​(java.lang.String path)
      Returns the path-portion of the given path (anything before the last slash or backslash) or an empty string.
      boolean isSubDirectory​(java.io.File base, java.io.File child)
      Checks, whether the child directory is a subdirectory of the base directory.
      void readFully​(java.io.InputStream in, byte[] data, int offset, int length)
      Reads the given number of bytes into the target array.
      java.lang.String stripFileExtension​(java.lang.String file)
      Removes the file extension from the given file name.
      • Methods inherited from class java.lang.Object

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

      • getInstance

        public static IOUtils getInstance()
        Gets the singleton instance of the utility package.
        Returns:
        the singleton instance.
      • createRelativePath

        public java.lang.String createRelativePath​(java.lang.String targetFile,
                                                   java.lang.String baseFile)
        Creates a relative url by stripping the common parts of the the url. If the baseFile denotes a directory, it must end with a slash.
        Parameters:
        targetFile - the to be stripped url
        baseFile - the base url, to which the url is relative to.
        Returns:
        the relative url, or the url unchanged, if there is no relation beween both URLs.
      • createRelativeURL

        public java.lang.String createRelativeURL​(java.net.URL url,
                                                  java.net.URL baseURL)
        Creates a relative url by stripping the common parts of the the url. If the base-URL denotes a directory, it must end with a slash.
        Parameters:
        url - the to be stripped url
        baseURL - the base url, to which the url is relative to.
        Returns:
        the relative url, or the url unchanged, if there is no relation beween both URLs.
      • copyStreams

        public void copyStreams​(java.io.InputStream in,
                                java.io.OutputStream out)
                         throws java.io.IOException
        Copies the InputStream into the OutputStream, until the end of the stream has been reached. This method uses a buffer of 4096 kbyte.
        Parameters:
        in - the inputstream from which to read.
        out - the outputstream where the data is written to.
        Throws:
        java.io.IOException - if a IOError occurs.
      • copyStreams

        public void copyStreams​(java.io.InputStream in,
                                java.io.OutputStream out,
                                int buffersize)
                         throws java.io.IOException
        Copies the InputStream into the OutputStream, until the end of the stream has been reached.
        Parameters:
        in - the inputstream from which to read.
        out - the outputstream where the data is written to.
        buffersize - the buffer size.
        Throws:
        java.io.IOException - if a IOError occurs.
      • copyWriter

        public void copyWriter​(java.io.Reader in,
                               java.io.Writer out)
                        throws java.io.IOException
        Copies the contents of the Reader into the Writer, until the end of the stream has been reached. This method uses a buffer of 4096 kbyte.
        Parameters:
        in - the reader from which to read.
        out - the writer where the data is written to.
        Throws:
        java.io.IOException - if a IOError occurs.
      • copyWriter

        public void copyWriter​(java.io.Reader in,
                               java.io.Writer out,
                               int buffersize)
                        throws java.io.IOException
        Copies the contents of the Reader into the Writer, until the end of the stream has been reached.
        Parameters:
        in - the reader from which to read.
        out - the writer where the data is written to.
        buffersize - the buffer size.
        Throws:
        java.io.IOException - if a IOError occurs.
      • readFully

        public void readFully​(java.io.InputStream in,
                              byte[] data,
                              int offset,
                              int length)
                       throws java.io.IOException
        Reads the given number of bytes into the target array. This method does not return until all bytes are read. In case a end-of-stream is reached, the method throws an Exception.
        Parameters:
        in - the inputstream from where to read.
        data - the array where to store the data.
        offset - the offset in the array where to store the data.
        length - the number of bytes to be read.
        Throws:
        java.io.IOException - if an IO error occured or the End of the stream has been reached.
      • getFileName

        public java.lang.String getFileName​(java.net.URL url)
        Extracts the file name from the URL.
        Parameters:
        url - the url.
        Returns:
        the extracted filename.
      • getFileName

        public java.lang.String getFileName​(java.lang.String path)
        Extracts the last file name from the given pathname.
        Parameters:
        path - the path name.
        Returns:
        the extracted filename.
      • stripFileExtension

        public java.lang.String stripFileExtension​(java.lang.String file)
        Removes the file extension from the given file name.
        Parameters:
        file - the file name.
        Returns:
        the file name without the file extension.
      • getFileExtension

        public java.lang.String getFileExtension​(java.lang.String file)
        Returns the file extension of the given file name. The returned value will contain the dot.
        Parameters:
        file - the file name.
        Returns:
        the file extension.
      • isSubDirectory

        public boolean isSubDirectory​(java.io.File base,
                                      java.io.File child)
                               throws java.io.IOException
        Checks, whether the child directory is a subdirectory of the base directory.
        Parameters:
        base - the base directory.
        child - the suspected child directory.
        Returns:
        true, if the child is a subdirectory of the base directory.
        Throws:
        java.io.IOException - if an IOError occured during the test.
      • findFileOnClassPath

        public java.io.File findFileOnClassPath​(java.lang.String name)
                                         throws java.lang.SecurityException
        Returns a reference to a file with the specified name that is located somewhere on the classpath. The code for this method is an adaptation of code supplied by Dave Postill.
        Parameters:
        name - the filename.
        Returns:
        a reference to a file or null if no file could be found.
        Throws:
        java.lang.SecurityException - if access to the system properties or filesystem is forbidden.
      • getAbsolutePath

        public java.lang.String getAbsolutePath​(java.lang.String targetFile,
                                                java.lang.String baseFile)
        Computes the absolute filename for the target file using the baseFile as root directory. If the baseFile is null or empty, the target file will be normalized (all navigation elements like ".." are removed).
        Parameters:
        targetFile - the target file name.
        baseFile - the base file (can be null).
        Returns:
        the absolute path.
      • getPath

        public java.lang.String getPath​(java.lang.String path)
        Returns the path-portion of the given path (anything before the last slash or backslash) or an empty string.
        Parameters:
        path - the path or filename from where to extract the path name.
        Returns:
        the extracted path or a empty string.