Class IO


  • public class IO
    extends java.lang.Object
    IO Utilities. Provides stream handling utilities in singleton Threadpool implementation accessed by static members.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int bufferSize  
      static java.lang.String CRLF  
      static byte[] CRLF_BYTES  
    • Constructor Summary

      Constructors 
      Constructor Description
      IO()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void close​(java.io.Closeable closeable)
      Closes an arbitrary closable, and logs exceptions at ignore level
      static void close​(java.io.InputStream is)
      closes an input stream, and logs exceptions
      static void close​(java.io.OutputStream os)
      closes an output stream, and logs exceptions
      static void close​(java.io.Reader reader)
      closes a reader, and logs exceptions
      static void close​(java.io.Writer writer)
      closes a writer, and logs exceptions
      static void copy​(java.io.File from, java.io.File to)
      Copy files or directories
      static void copy​(java.io.InputStream in, java.io.OutputStream out)
      Copy Stream in to Stream out until EOF or exception.
      static void copy​(java.io.InputStream in, java.io.OutputStream out, long byteCount)
      Copy Stream in to Stream for byteCount bytes or until EOF or exception.
      static void copy​(java.io.Reader in, java.io.Writer out)
      Copy Reader to Writer out until EOF or exception.
      static void copy​(java.io.Reader in, java.io.Writer out, long byteCount)
      Copy Reader to Writer for byteCount bytes or until EOF or exception.
      static void copyDir​(java.io.File from, java.io.File to)  
      static void copyFile​(java.io.File from, java.io.File to)  
      static boolean delete​(java.io.File file)
      Delete File.
      static java.io.InputStream getClosedStream()  
      static java.io.PrintWriter getNullPrintWriter()  
      static java.io.OutputStream getNullStream()  
      static java.io.Writer getNullWriter()  
      static boolean isEmptyDir​(java.io.File dir)
      Test if directory is empty.
      static byte[] readBytes​(java.io.InputStream in)  
      static java.lang.String toString​(java.io.InputStream in)
      Read input stream to string.
      static java.lang.String toString​(java.io.InputStream in, java.lang.String encoding)
      Read input stream to string.
      static java.lang.String toString​(java.io.InputStream in, java.nio.charset.Charset encoding)
      Read input stream to string.
      static java.lang.String toString​(java.io.Reader in)
      Read input stream to string.
      static java.lang.String toString​(java.nio.file.Path path, java.nio.charset.Charset charset)
      Read Path to string.
      static long write​(java.nio.channels.GatheringByteChannel out, java.nio.ByteBuffer[] buffers, int offset, int length)
      A gathering write utility wrapper.
      • Methods inherited from class java.lang.Object

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

      • IO

        public IO()
    • Method Detail

      • copy

        public static void copy​(java.io.InputStream in,
                                java.io.OutputStream out)
                         throws java.io.IOException
        Copy Stream in to Stream out until EOF or exception.
        Parameters:
        in - the input stream to read from (until EOF)
        out - the output stream to write to
        Throws:
        java.io.IOException - if unable to copy streams
      • copy

        public static void copy​(java.io.Reader in,
                                java.io.Writer out)
                         throws java.io.IOException
        Copy Reader to Writer out until EOF or exception.
        Parameters:
        in - the read to read from (until EOF)
        out - the writer to write to
        Throws:
        java.io.IOException - if unable to copy the streams
      • copy

        public static void copy​(java.io.InputStream in,
                                java.io.OutputStream out,
                                long byteCount)
                         throws java.io.IOException
        Copy Stream in to Stream for byteCount bytes or until EOF or exception.
        Parameters:
        in - the stream to read from
        out - the stream to write to
        byteCount - the number of bytes to copy
        Throws:
        java.io.IOException - if unable to copy the streams
      • copy

        public static void copy​(java.io.Reader in,
                                java.io.Writer out,
                                long byteCount)
                         throws java.io.IOException
        Copy Reader to Writer for byteCount bytes or until EOF or exception.
        Parameters:
        in - the Reader to read from
        out - the Writer to write to
        byteCount - the number of bytes to copy
        Throws:
        java.io.IOException - if unable to copy streams
      • copy

        public static void copy​(java.io.File from,
                                java.io.File to)
                         throws java.io.IOException
        Copy files or directories
        Parameters:
        from - the file to copy
        to - the destination to copy to
        Throws:
        java.io.IOException - if unable to copy
      • copyDir

        public static void copyDir​(java.io.File from,
                                   java.io.File to)
                            throws java.io.IOException
        Throws:
        java.io.IOException
      • copyFile

        public static void copyFile​(java.io.File from,
                                    java.io.File to)
                             throws java.io.IOException
        Throws:
        java.io.IOException
      • toString

        public static java.lang.String toString​(java.nio.file.Path path,
                                                java.nio.charset.Charset charset)
                                         throws java.io.IOException
        Read Path to string.
        Parameters:
        path - the path to read from (until EOF)
        charset - the charset to read with
        Returns:
        the String parsed from path (default Charset)
        Throws:
        java.io.IOException - if unable to read the path (or handle the charset)
      • toString

        public static java.lang.String toString​(java.io.InputStream in)
                                         throws java.io.IOException
        Read input stream to string.
        Parameters:
        in - the stream to read from (until EOF)
        Returns:
        the String parsed from stream (default Charset)
        Throws:
        java.io.IOException - if unable to read the stream (or handle the charset)
      • toString

        public static java.lang.String toString​(java.io.InputStream in,
                                                java.lang.String encoding)
                                         throws java.io.IOException
        Read input stream to string.
        Parameters:
        in - the stream to read from (until EOF)
        encoding - the encoding to use (can be null to use default Charset)
        Returns:
        the String parsed from the stream
        Throws:
        java.io.IOException - if unable to read the stream (or handle the charset)
      • toString

        public static java.lang.String toString​(java.io.InputStream in,
                                                java.nio.charset.Charset encoding)
                                         throws java.io.IOException
        Read input stream to string.
        Parameters:
        in - the stream to read from (until EOF)
        encoding - the Charset to use (can be null to use default Charset)
        Returns:
        the String parsed from the stream
        Throws:
        java.io.IOException - if unable to read the stream (or handle the charset)
      • toString

        public static java.lang.String toString​(java.io.Reader in)
                                         throws java.io.IOException
        Read input stream to string.
        Parameters:
        in - the reader to read from (until EOF)
        Returns:
        the String parsed from the reader
        Throws:
        java.io.IOException - if unable to read the stream (or handle the charset)
      • delete

        public static boolean delete​(java.io.File file)
        Delete File. This delete will recursively delete directories - BE CAREFUL
        Parameters:
        file - The file (or directory) to be deleted.
        Returns:
        true if file was deleted, or directory referenced was deleted. false if file doesn't exist, or was null.
      • isEmptyDir

        public static boolean isEmptyDir​(java.io.File dir)
        Test if directory is empty.
        Parameters:
        dir - the directory
        Returns:
        true if directory is null, doesn't exist, or has no content. false if not a directory, or has contents
      • close

        public static void close​(java.io.Closeable closeable)
        Closes an arbitrary closable, and logs exceptions at ignore level
        Parameters:
        closeable - the closeable to close
      • close

        public static void close​(java.io.InputStream is)
        closes an input stream, and logs exceptions
        Parameters:
        is - the input stream to close
      • close

        public static void close​(java.io.OutputStream os)
        closes an output stream, and logs exceptions
        Parameters:
        os - the output stream to close
      • close

        public static void close​(java.io.Reader reader)
        closes a reader, and logs exceptions
        Parameters:
        reader - the reader to close
      • close

        public static void close​(java.io.Writer writer)
        closes a writer, and logs exceptions
        Parameters:
        writer - the writer to close
      • readBytes

        public static byte[] readBytes​(java.io.InputStream in)
                                throws java.io.IOException
        Throws:
        java.io.IOException
      • write

        public static long write​(java.nio.channels.GatheringByteChannel out,
                                 java.nio.ByteBuffer[] buffers,
                                 int offset,
                                 int length)
                          throws java.io.IOException
        A gathering write utility wrapper.

        This method wraps a gather write with a loop that handles the limitations of some operating systems that have a limit on the number of buffers written. The method loops on the write until either all the content is written or no progress is made.

        Parameters:
        out - The GatheringByteChannel to write to
        buffers - The buffers to write
        offset - The offset into the buffers array
        length - The length in buffers to write
        Returns:
        The total bytes written
        Throws:
        java.io.IOException - if unable write to the GatheringByteChannel
      • getNullStream

        public static java.io.OutputStream getNullStream()
        Returns:
        An outputstream to nowhere
      • getClosedStream

        public static java.io.InputStream getClosedStream()
        Returns:
        An outputstream to nowhere
      • getNullWriter

        public static java.io.Writer getNullWriter()
        Returns:
        An writer to nowhere
      • getNullPrintWriter

        public static java.io.PrintWriter getNullPrintWriter()
        Returns:
        An writer to nowhere