Package jodd.io

Class IOUtil


  • public class IOUtil
    extends java.lang.Object
    Optimized byte and character stream utilities.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int ioBufferSize
      Buffer size for various I/O operations.
    • Constructor Summary

      Constructors 
      Constructor Description
      IOUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void close​(java.io.Closeable closeable)
      Closes silently the closable object.
      static boolean compare​(java.io.InputStream input1, java.io.InputStream input2)
      Compares the content of two byte streams (InputStreams).
      static boolean compare​(java.io.Reader input1, java.io.Reader input2)
      Compares the content of two character streams (Readers).
      static java.io.CharArrayWriter copy​(java.io.InputStream input)  
      static java.io.CharArrayWriter copy​(java.io.InputStream input, int count)  
      static int copy​(java.io.InputStream input, java.io.OutputStream output)
      Copies bytes from InputStream to OutputStream using buffer.
      static int copy​(java.io.InputStream input, java.io.OutputStream output, int count)
      Copies specified number of bytes from InputStream to OutputStream using buffer.
      static java.io.CharArrayWriter copy​(java.io.InputStream input, java.nio.charset.Charset encoding)  
      static java.io.CharArrayWriter copy​(java.io.InputStream input, java.nio.charset.Charset encoding, int count)
      Copies InputStream to a new CharArrayWriter using buffer and specified encoding.
      static <T extends java.io.Writer>
      T
      copy​(java.io.InputStream input, T output)  
      static <T extends java.io.Writer>
      T
      copy​(java.io.InputStream input, T output, int count)  
      static <T extends java.io.Writer>
      T
      copy​(java.io.InputStream input, T output, java.nio.charset.Charset encoding)  
      static <T extends java.io.Writer>
      T
      copy​(java.io.InputStream input, T output, java.nio.charset.Charset encoding, int count)
      Copies InputStream to Writer using buffer and specified encoding.
      static java.io.CharArrayWriter copy​(java.io.Reader input)  
      static java.io.CharArrayWriter copy​(java.io.Reader input, int count)
      Copies Reader to a new CharArrayWriter using buffer and specified encoding.
      static int copy​(java.io.Reader input, java.io.Writer output)
      Copies bytes from Reader to Writer using buffer.
      static int copy​(java.io.Reader input, java.io.Writer output, int count)
      Copies specified number of characters from Reader to Writer using buffer.
      static <T extends java.io.OutputStream>
      T
      copy​(java.io.Reader input, T output)  
      static <T extends java.io.OutputStream>
      T
      copy​(java.io.Reader input, T output, int count)  
      static <T extends java.io.OutputStream>
      T
      copy​(java.io.Reader input, T output, java.nio.charset.Charset encoding)  
      static <T extends java.io.OutputStream>
      T
      copy​(java.io.Reader input, T output, java.nio.charset.Charset encoding, int count)
      Copies Reader to OutputStream using buffer and specified encoding.
      static java.io.ByteArrayOutputStream copyToOutputStream​(java.io.InputStream input)  
      static java.io.ByteArrayOutputStream copyToOutputStream​(java.io.InputStream input, int count)
      Copies InputStream to a new ByteArrayOutputStream using buffer and specified encoding.
      static java.io.ByteArrayOutputStream copyToOutputStream​(java.io.Reader input)  
      static java.io.ByteArrayOutputStream copyToOutputStream​(java.io.Reader input, int count)  
      static java.io.ByteArrayOutputStream copyToOutputStream​(java.io.Reader input, java.nio.charset.Charset encoding)  
      static java.io.ByteArrayOutputStream copyToOutputStream​(java.io.Reader input, java.nio.charset.Charset encoding, int count)
      Copies Reader to a new ByteArrayOutputStream using buffer and specified encoding.
      static java.io.InputStreamReader inputStreamReadeOf​(java.io.InputStream input)  
      static java.io.InputStreamReader inputStreamReadeOf​(java.io.InputStream input, java.nio.charset.Charset encoding)
      Returns new InputStreamReader using specified InputStream and encoding.
      static java.io.OutputStreamWriter outputStreamWriterOf​(java.io.OutputStream output)  
      static java.io.OutputStreamWriter outputStreamWriterOf​(java.io.OutputStream output, java.nio.charset.Charset encoding)
      Returns new OutputStreamWriter using specified OutputStream and encoding.
      static byte[] readAvailableBytes​(java.io.InputStream input)
      Reads all available bytes from InputStream as a byte array.
      static byte[] readBytes​(java.io.InputStream input)  
      static byte[] readBytes​(java.io.InputStream input, int count)  
      static byte[] readBytes​(java.io.Reader input)  
      static byte[] readBytes​(java.io.Reader input, int count)  
      static byte[] readBytes​(java.io.Reader input, java.nio.charset.Charset encoding)  
      static byte[] readBytes​(java.io.Reader input, java.nio.charset.Charset encoding, int count)  
      static char[] readChars​(java.io.InputStream input)  
      static char[] readChars​(java.io.InputStream input, int count)  
      static char[] readChars​(java.io.InputStream input, java.nio.charset.Charset encoding)  
      static char[] readChars​(java.io.InputStream input, java.nio.charset.Charset encoding, int count)  
      static char[] readChars​(java.io.Reader input)  
      static char[] readChars​(java.io.Reader input, int count)  
      • Methods inherited from class java.lang.Object

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

      • ioBufferSize

        public static int ioBufferSize
        Buffer size for various I/O operations.
    • Constructor Detail

      • IOUtil

        public IOUtil()
    • Method Detail

      • close

        public static void close​(java.io.Closeable closeable)
        Closes silently the closable object. If it is Flushable, it will be flushed first. No exception will be thrown if an I/O error occurs.
      • copy

        public static int copy​(java.io.Reader input,
                               java.io.Writer output)
                        throws java.io.IOException
        Copies bytes from Reader to Writer using buffer. Reader and Writer don't have to be wrapped to buffered, since copying is already optimized.
        Parameters:
        input - Reader to read.
        output - Writer to write to.
        Returns:
        The total number of characters read.
        Throws:
        java.io.IOException - if there is an error reading or writing.
      • copy

        public static int copy​(java.io.InputStream input,
                               java.io.OutputStream output)
                        throws java.io.IOException
        Copies bytes from InputStream to OutputStream using buffer. InputStream and OutputStream don't have to be wrapped to buffered, since copying is already optimized.
        Parameters:
        input - InputStream to read.
        output - OutputStream to write to.
        Returns:
        The total number of bytes read.
        Throws:
        java.io.IOException - if there is an error reading or writing.
      • copy

        public static int copy​(java.io.Reader input,
                               java.io.Writer output,
                               int count)
                        throws java.io.IOException
        Copies specified number of characters from Reader to Writer using buffer. Reader and Writer don't have to be wrapped to buffered, since copying is already optimized.
        Parameters:
        input - Reader to read.
        output - Writer to write to.
        count - The number of characters to read.
        Returns:
        The total number of characters read.
        Throws:
        java.io.IOException - if there is an error reading or writing.
      • copy

        public static int copy​(java.io.InputStream input,
                               java.io.OutputStream output,
                               int count)
                        throws java.io.IOException
        Copies specified number of bytes from InputStream to OutputStream using buffer. InputStream and OutputStream don't have to be wrapped to buffered, since copying is already optimized.
        Parameters:
        input - InputStream to read.
        output - OutputStream to write to.
        count - The number of bytes to read.
        Returns:
        The total number of bytes read.
        Throws:
        java.io.IOException - if there is an error reading or writing.
      • readAvailableBytes

        public static byte[] readAvailableBytes​(java.io.InputStream input)
                                         throws java.io.IOException
        Reads all available bytes from InputStream as a byte array. Uses InputStream.available() to determine the size of input stream. This is the fastest method for reading InputStream to byte array, but depends on InputStream implementation of InputStream.available().
        Parameters:
        input - InputStream to read.
        Returns:
        byte[]
        Throws:
        java.io.IOException - if total read is less than InputStream.available();
      • copy

        public static <T extends java.io.OutputStream> T copy​(java.io.Reader input,
                                                              T output)
                                                       throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        copy(Reader, OutputStream, Charset)
      • copy

        public static <T extends java.io.OutputStream> T copy​(java.io.Reader input,
                                                              T output,
                                                              int count)
                                                       throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        copy(Reader, OutputStream, Charset, int)
      • copy

        public static <T extends java.io.OutputStream> T copy​(java.io.Reader input,
                                                              T output,
                                                              java.nio.charset.Charset encoding)
                                                       throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        copy(Reader, OutputStream, Charset, int)
      • copy

        public static <T extends java.io.OutputStream> T copy​(java.io.Reader input,
                                                              T output,
                                                              java.nio.charset.Charset encoding,
                                                              int count)
                                                       throws java.io.IOException
        Copies Reader to OutputStream using buffer and specified encoding.
        Throws:
        java.io.IOException
        See Also:
        copy(Reader, Writer, int)
      • copyToOutputStream

        public static java.io.ByteArrayOutputStream copyToOutputStream​(java.io.InputStream input)
                                                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        copyToOutputStream(InputStream, int)
      • copyToOutputStream

        public static java.io.ByteArrayOutputStream copyToOutputStream​(java.io.InputStream input,
                                                                       int count)
                                                                throws java.io.IOException
        Copies InputStream to a new ByteArrayOutputStream using buffer and specified encoding.
        Throws:
        java.io.IOException
        See Also:
        copy(InputStream, OutputStream, int)
      • copyToOutputStream

        public static java.io.ByteArrayOutputStream copyToOutputStream​(java.io.Reader input)
                                                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        copyToOutputStream(Reader, Charset)
      • copyToOutputStream

        public static java.io.ByteArrayOutputStream copyToOutputStream​(java.io.Reader input,
                                                                       java.nio.charset.Charset encoding)
                                                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        copyToOutputStream(Reader, Charset, int)
      • copyToOutputStream

        public static java.io.ByteArrayOutputStream copyToOutputStream​(java.io.Reader input,
                                                                       int count)
                                                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        copyToOutputStream(Reader, Charset, int)
      • copyToOutputStream

        public static java.io.ByteArrayOutputStream copyToOutputStream​(java.io.Reader input,
                                                                       java.nio.charset.Charset encoding,
                                                                       int count)
                                                                throws java.io.IOException
        Copies Reader to a new ByteArrayOutputStream using buffer and specified encoding.
        Throws:
        java.io.IOException
        See Also:
        copy(Reader, OutputStream, Charset, int)
      • copy

        public static <T extends java.io.Writer> T copy​(java.io.InputStream input,
                                                        T output)
                                                 throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        copy(InputStream, Writer, Charset)
      • copy

        public static <T extends java.io.Writer> T copy​(java.io.InputStream input,
                                                        T output,
                                                        int count)
                                                 throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        copy(InputStream, Writer, Charset, int)
      • copy

        public static <T extends java.io.Writer> T copy​(java.io.InputStream input,
                                                        T output,
                                                        java.nio.charset.Charset encoding)
                                                 throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        copy(InputStream, Writer, Charset, int)
      • copy

        public static <T extends java.io.Writer> T copy​(java.io.InputStream input,
                                                        T output,
                                                        java.nio.charset.Charset encoding,
                                                        int count)
                                                 throws java.io.IOException
        Copies InputStream to Writer using buffer and specified encoding.
        Throws:
        java.io.IOException
        See Also:
        copy(Reader, Writer, int)
      • copy

        public static java.io.CharArrayWriter copy​(java.io.InputStream input)
                                            throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        copy(InputStream, Charset)
      • copy

        public static java.io.CharArrayWriter copy​(java.io.InputStream input,
                                                   int count)
                                            throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        copy(InputStream, Charset, int)
      • copy

        public static java.io.CharArrayWriter copy​(java.io.InputStream input,
                                                   java.nio.charset.Charset encoding)
                                            throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        copy(InputStream, Charset, int)
      • copy

        public static java.io.CharArrayWriter copy​(java.io.InputStream input,
                                                   java.nio.charset.Charset encoding,
                                                   int count)
                                            throws java.io.IOException
        Copies InputStream to a new CharArrayWriter using buffer and specified encoding.
        Throws:
        java.io.IOException
        See Also:
        copy(InputStream, Writer, Charset, int)
      • copy

        public static java.io.CharArrayWriter copy​(java.io.Reader input)
                                            throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        copy(Reader, int)
      • copy

        public static java.io.CharArrayWriter copy​(java.io.Reader input,
                                                   int count)
                                            throws java.io.IOException
        Copies Reader to a new CharArrayWriter using buffer and specified encoding.
        Throws:
        java.io.IOException
        See Also:
        copy(Reader, Writer, int)
      • readBytes

        public static byte[] readBytes​(java.io.InputStream input)
                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        readBytes(InputStream, int)
      • readBytes

        public static byte[] readBytes​(java.io.InputStream input,
                                       int count)
                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        copyToOutputStream(InputStream, int)
      • readBytes

        public static byte[] readBytes​(java.io.Reader input)
                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        readBytes(Reader, Charset)
      • readBytes

        public static byte[] readBytes​(java.io.Reader input,
                                       int count)
                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        readBytes(Reader, Charset, int)
      • readBytes

        public static byte[] readBytes​(java.io.Reader input,
                                       java.nio.charset.Charset encoding)
                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        readBytes(Reader, Charset, int)
      • readBytes

        public static byte[] readBytes​(java.io.Reader input,
                                       java.nio.charset.Charset encoding,
                                       int count)
                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        copyToOutputStream(Reader, Charset, int)
      • readChars

        public static char[] readChars​(java.io.Reader input)
                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        readChars(Reader, int)
      • readChars

        public static char[] readChars​(java.io.Reader input,
                                       int count)
                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        copy(Reader, int)
      • readChars

        public static char[] readChars​(java.io.InputStream input)
                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        readChars(InputStream, int)
      • readChars

        public static char[] readChars​(java.io.InputStream input,
                                       java.nio.charset.Charset encoding)
                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        readChars(InputStream, Charset, int)
      • readChars

        public static char[] readChars​(java.io.InputStream input,
                                       int count)
                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        readChars(InputStream, Charset, int)
      • readChars

        public static char[] readChars​(java.io.InputStream input,
                                       java.nio.charset.Charset encoding,
                                       int count)
                                throws java.io.IOException
        Throws:
        java.io.IOException
        See Also:
        copy(InputStream, Charset, int)
      • compare

        public static boolean compare​(java.io.InputStream input1,
                                      java.io.InputStream input2)
                               throws java.io.IOException
        Compares the content of two byte streams (InputStreams).
        Returns:
        true if the content of the first InputStream is equal to the content of the second InputStream.
        Throws:
        java.io.IOException
      • compare

        public static boolean compare​(java.io.Reader input1,
                                      java.io.Reader input2)
                               throws java.io.IOException
        Compares the content of two character streams (Readers).
        Returns:
        true if the content of the first Reader is equal to the content of the second Reader.
        Throws:
        java.io.IOException
      • inputStreamReadeOf

        public static java.io.InputStreamReader inputStreamReadeOf​(java.io.InputStream input,
                                                                   java.nio.charset.Charset encoding)
        Returns new InputStreamReader using specified InputStream and encoding.
        Parameters:
        input - InputStream
        encoding - Encoding as String to use for InputStreamReader.
        Returns:
        new InputStreamReader
      • outputStreamWriterOf

        public static java.io.OutputStreamWriter outputStreamWriterOf​(java.io.OutputStream output,
                                                                      java.nio.charset.Charset encoding)
        Returns new OutputStreamWriter using specified OutputStream and encoding.
        Parameters:
        output - OutputStream
        encoding - Encoding as String to use for OutputStreamWriter.
        Returns:
        new OutputStreamWriter