Class IOUtils


  • public final class IOUtils
    extends Object
    General IO Stream manipulation.

    This class provides static utility methods for input/output operations.

    • closeQuietly - these method closes any kind of closeable resource, e.g. an input/output stream or reader/writer ignoring nulls and exceptions
    • toXxx - these methods read data from a stream
    • write - these methods write data to a stream
    • copy - these methods copy all the data from one stream to another
    • contentEquals - these methods compare the content of two streams

    The byte-to-char methods and char-to-byte methods involve a conversion step. Two methods are provided in each case, one that uses the platform default encoding and the other which allows you to specify an encoding. You are encouraged to always specify an encoding because relying on the platform default can lead to unexpected results, for example when moving from development to production.

    All the methods in this class that read a stream are buffered internally. This means that there is no cause to use a BufferedInputStream or BufferedReader. The default buffer size of 4K has been show to be efficient in tests.

    Wherever possible, the methods in this class do not flush or close the stream. This is to avoid making non-portable assumptions about the streams' origin and further use. Thus the caller is still responsible for closing streams after use.

    Origin of code: Apache Avalon (Excalibur)

    Author:
    Peter Donald, Jeff Turner, Matthew Hawthorne, Stephen Colebourne, Gareth Davis
    • Constructor Summary

      Constructors 
      Constructor Description
      IOUtils()
      Instances should NOT be constructed in standard programming.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void close​(Closeable closeable)
      Closes a closeable.
      static void closeQuietly​(Closeable closeable)
      Unconditionally close a Closeable.
      static boolean contentEquals​(InputStream input1, InputStream input2)
      Compare the contents of two Streams to determine if they are equal or not.
      static boolean contentEquals​(Reader input1, Reader input2)
      Compare the contents of two Readers to determine if they are equal or not.
      static int copy​(InputStream input, OutputStream output)
      Copy bytes from an InputStream to an OutputStream.
      static void copy​(InputStream input, Writer output)
      Copy bytes from an InputStream to chars on a Writer using the default character encoding of the platform.
      static void copy​(InputStream input, Writer output, String encoding)
      Copy bytes from an InputStream to chars on a Writer using the specified character encoding.
      static void copy​(Reader input, OutputStream output)
      Copy chars from a Reader to bytes on an OutputStream using the default character encoding of the platform, and calling flush.
      static void copy​(Reader input, OutputStream output, String encoding)
      Copy chars from a Reader to bytes on an OutputStream using the specified character encoding, and calling flush.
      static int copy​(Reader input, Writer output)
      Copy chars from a Reader to a Writer.
      static byte[] toByteArray​(InputStream input)
      Get the contents of an InputStream as a byte[].
      static byte[] toByteArray​(Reader input)
      Get the contents of a Reader as a byte[] using the default character encoding of the platform.
      static byte[] toByteArray​(Reader input, String encoding)
      Get the contents of a Reader as a byte[] using the specified character encoding.
      static char[] toCharArray​(InputStream is)
      Get the contents of an InputStream as a character array using the default character encoding of the platform.
      static char[] toCharArray​(InputStream is, String encoding)
      Get the contents of an InputStream as a character array using the specified character encoding.
      static char[] toCharArray​(Reader input)
      Get the contents of a Reader as a character array.
      static String toString​(InputStream input)
      Get the contents of an InputStream as a String using the default character encoding of the platform.
      static String toString​(InputStream input, String encoding)
      Get the contents of an InputStream as a String using the specified character encoding.
      static String toString​(Reader input)
      Get the contents of a Reader as a String.
      static void write​(byte[] data, OutputStream output)
      Writes bytes from a byte[] to an OutputStream.
      static void write​(byte[] data, Writer output)
      Writes bytes from a byte[] to chars on a Writer using the default character encoding of the platform.
      static void write​(byte[] data, Writer output, String encoding)
      Writes bytes from a byte[] to chars on a Writer using the specified character encoding.
      static void write​(char[] data, OutputStream output)
      Writes chars from a char[] to bytes on an OutputStream.
      static void write​(char[] data, OutputStream output, String encoding)
      Writes chars from a char[] to bytes on an OutputStream using the specified character encoding.
      static void write​(char[] data, Writer output)
      Writes chars from a char[] to a Writer using the default character encoding of the platform.
      static void write​(StringBuilder data, OutputStream output)
      Writes chars from a AppendingStringBuffer to bytes on an OutputStream using the default character encoding of the platform.
      static void write​(StringBuilder data, OutputStream output, String encoding)
      Writes chars from a AppendingStringBuffer to bytes on an OutputStream using the specified character encoding.
      static void write​(StringBuilder data, Writer output)
      Writes chars from a AppendingStringBuffer to a Writer.
      static void write​(String data, OutputStream output)
      Writes chars from a String to bytes on an OutputStream using the default character encoding of the platform.
      static void write​(String data, OutputStream output, String encoding)
      Writes chars from a String to bytes on an OutputStream using the specified character encoding.
      static void write​(String data, Writer output)
      Writes chars from a String to a Writer.
    • Constructor Detail

      • IOUtils

        public IOUtils()
        Instances should NOT be constructed in standard programming.
    • Method Detail

      • close

        public static void close​(Closeable closeable)
                          throws IOException
        Closes a closeable. Guards against null closables.
        Parameters:
        closeable - closeable to close
        Throws:
        IOException - when close fails
      • closeQuietly

        public static void closeQuietly​(Closeable closeable)
        Unconditionally close a Closeable.

        closeables can be input or output streams, reader, writers, and much more. Equivalent to Closeable.close(), except any exceptions will be ignored. This is typically used in finally blocks.

        Parameters:
        closeable - the Closeable to close, may be null or already closed
      • toByteArray

        public static byte[] toByteArray​(InputStream input)
                                  throws IOException
        Get the contents of an InputStream as a byte[].

        This method buffers the input internally, so there is no need to use a BufferedInputStream.

        Parameters:
        input - the InputStream to read from
        Returns:
        the requested byte array
        Throws:
        NullPointerException - if the input is null
        IOException - if an I/O error occurs
      • toByteArray

        public static byte[] toByteArray​(Reader input)
                                  throws IOException
        Get the contents of a Reader as a byte[] using the default character encoding of the platform.

        This method buffers the input internally, so there is no need to use a BufferedReader.

        Parameters:
        input - the Reader to read from
        Returns:
        the requested byte array
        Throws:
        NullPointerException - if the input is null
        IOException - if an I/O error occurs
      • toByteArray

        public static byte[] toByteArray​(Reader input,
                                         String encoding)
                                  throws IOException
        Get the contents of a Reader as a byte[] using the specified character encoding.

        Character encoding names can be found at IANA.

        This method buffers the input internally, so there is no need to use a BufferedReader.

        Parameters:
        input - the Reader to read from
        encoding - the encoding to use, null means platform default
        Returns:
        the requested byte array
        Throws:
        NullPointerException - if the input is null
        IOException - if an I/O error occurs
        Since:
        1.1
      • toCharArray

        public static char[] toCharArray​(InputStream is)
                                  throws IOException
        Get the contents of an InputStream as a character array using the default character encoding of the platform.

        This method buffers the input internally, so there is no need to use a BufferedInputStream.

        Parameters:
        is - the InputStream to read from
        Returns:
        the requested character array
        Throws:
        NullPointerException - if the input is null
        IOException - if an I/O error occurs
      • toCharArray

        public static char[] toCharArray​(InputStream is,
                                         String encoding)
                                  throws IOException
        Get the contents of an InputStream as a character array using the specified character encoding.

        Character encoding names can be found at IANA.

        This method buffers the input internally, so there is no need to use a BufferedInputStream.

        Parameters:
        is - the InputStream to read from
        encoding - the encoding to use, null means platform default
        Returns:
        the requested character array
        Throws:
        NullPointerException - if the input is null
        IOException - if an I/O error occurs
      • toCharArray

        public static char[] toCharArray​(Reader input)
                                  throws IOException
        Get the contents of a Reader as a character array.

        This method buffers the input internally, so there is no need to use a BufferedReader.

        Parameters:
        input - the Reader to read from
        Returns:
        the requested character array
        Throws:
        NullPointerException - if the input is null
        IOException - if an I/O error occurs
      • toString

        public static String toString​(InputStream input)
                               throws IOException
        Get the contents of an InputStream as a String using the default character encoding of the platform.

        This method buffers the input internally, so there is no need to use a BufferedInputStream.

        Parameters:
        input - the InputStream to read from
        Returns:
        the requested String
        Throws:
        NullPointerException - if the input is null
        IOException - if an I/O error occurs
      • toString

        public static String toString​(InputStream input,
                                      String encoding)
                               throws IOException
        Get the contents of an InputStream as a String using the specified character encoding.

        Character encoding names can be found at IANA.

        This method buffers the input internally, so there is no need to use a BufferedInputStream.

        Parameters:
        input - the InputStream to read from
        encoding - the encoding to use, null means platform default
        Returns:
        the requested String
        Throws:
        NullPointerException - if the input is null
        IOException - if an I/O error occurs
      • toString

        public static String toString​(Reader input)
                               throws IOException
        Get the contents of a Reader as a String.

        This method buffers the input internally, so there is no need to use a BufferedReader.

        Parameters:
        input - the Reader to read from
        Returns:
        the requested String
        Throws:
        NullPointerException - if the input is null
        IOException - if an I/O error occurs
      • write

        public static void write​(byte[] data,
                                 OutputStream output)
                          throws IOException
        Writes bytes from a byte[] to an OutputStream.
        Parameters:
        data - the byte array to write, do not modify during output, null ignored
        output - the OutputStream to write to
        Throws:
        NullPointerException - if output is null
        IOException - if an I/O error occurs
        Since:
        1.1
      • write

        public static void write​(byte[] data,
                                 Writer output)
                          throws IOException
        Writes bytes from a byte[] to chars on a Writer using the default character encoding of the platform.

        This method uses String(byte[]).

        Parameters:
        data - the byte array to write, do not modify during output, null ignored
        output - the Writer to write to
        Throws:
        NullPointerException - if output is null
        IOException - if an I/O error occurs
        Since:
        1.1
      • write

        public static void write​(byte[] data,
                                 Writer output,
                                 String encoding)
                          throws IOException
        Writes bytes from a byte[] to chars on a Writer using the specified character encoding.

        Character encoding names can be found at IANA.

        This method uses String(byte[], String).

        Parameters:
        data - the byte array to write, do not modify during output, null ignored
        output - the Writer to write to
        encoding - the encoding to use, null means platform default
        Throws:
        NullPointerException - if output is null
        IOException - if an I/O error occurs
        Since:
        1.1
      • write

        public static void write​(char[] data,
                                 Writer output)
                          throws IOException
        Writes chars from a char[] to a Writer using the default character encoding of the platform.
        Parameters:
        data - the char array to write, do not modify during output, null ignored
        output - the Writer to write to
        Throws:
        NullPointerException - if output is null
        IOException - if an I/O error occurs
        Since:
        1.1
      • write

        public static void write​(char[] data,
                                 OutputStream output,
                                 String encoding)
                          throws IOException
        Writes chars from a char[] to bytes on an OutputStream using the specified character encoding.

        Character encoding names can be found at IANA.

        This method uses String(char[]) and String.getBytes(String).

        Parameters:
        data - the char array to write, do not modify during output, null ignored
        output - the OutputStream to write to
        encoding - the encoding to use, null means platform default
        Throws:
        NullPointerException - if output is null
        IOException - if an I/O error occurs
        Since:
        1.1
      • write

        public static void write​(String data,
                                 Writer output)
                          throws IOException
        Writes chars from a String to a Writer.
        Parameters:
        data - the String to write, null ignored
        output - the Writer to write to
        Throws:
        NullPointerException - if output is null
        IOException - if an I/O error occurs
        Since:
        1.1
      • write

        public static void write​(String data,
                                 OutputStream output)
                          throws IOException
        Writes chars from a String to bytes on an OutputStream using the default character encoding of the platform.

        This method uses String.getBytes().

        Parameters:
        data - the String to write, null ignored
        output - the OutputStream to write to
        Throws:
        NullPointerException - if output is null
        IOException - if an I/O error occurs
        Since:
        1.1
      • write

        public static void write​(String data,
                                 OutputStream output,
                                 String encoding)
                          throws IOException
        Writes chars from a String to bytes on an OutputStream using the specified character encoding.

        Character encoding names can be found at IANA.

        This method uses String.getBytes(String).

        Parameters:
        data - the String to write, null ignored
        output - the OutputStream to write to
        encoding - the encoding to use, null means platform default
        Throws:
        NullPointerException - if output is null
        IOException - if an I/O error occurs
        Since:
        1.1
      • write

        public static void write​(StringBuilder data,
                                 Writer output)
                          throws IOException
        Writes chars from a AppendingStringBuffer to a Writer.
        Parameters:
        data - the AppendingStringBuffer to write, null ignored
        output - the Writer to write to
        Throws:
        NullPointerException - if output is null
        IOException - if an I/O error occurs
        Since:
        1.1
      • write

        public static void write​(StringBuilder data,
                                 OutputStream output)
                          throws IOException
        Writes chars from a AppendingStringBuffer to bytes on an OutputStream using the default character encoding of the platform.

        This method uses String.getBytes().

        Parameters:
        data - the AppendingStringBuffer to write, null ignored
        output - the OutputStream to write to
        Throws:
        NullPointerException - if output is null
        IOException - if an I/O error occurs
        Since:
        1.1
      • write

        public static void write​(StringBuilder data,
                                 OutputStream output,
                                 String encoding)
                          throws IOException
        Writes chars from a AppendingStringBuffer to bytes on an OutputStream using the specified character encoding.

        Character encoding names can be found at IANA.

        This method uses String.getBytes(String).

        Parameters:
        data - the AppendingStringBuffer to write, null ignored
        output - the OutputStream to write to
        encoding - the encoding to use, null means platform default
        Throws:
        NullPointerException - if output is null
        IOException - if an I/O error occurs
        Since:
        1.1
      • copy

        public static int copy​(InputStream input,
                               OutputStream output)
                        throws IOException
        Copy bytes from an InputStream to an OutputStream.

        This method buffers the input internally, so there is no need to use a BufferedInputStream.

        Parameters:
        input - the InputStream to read from
        output - the OutputStream to write to
        Returns:
        the number of bytes copied
        Throws:
        NullPointerException - if the input or output is null
        IOException - if an I/O error occurs
        Since:
        1.1
      • copy

        public static void copy​(InputStream input,
                                Writer output)
                         throws IOException
        Copy bytes from an InputStream to chars on a Writer using the default character encoding of the platform.

        This method buffers the input internally, so there is no need to use a BufferedInputStream.

        This method uses InputStreamReader.

        Parameters:
        input - the InputStream to read from
        output - the Writer to write to
        Throws:
        NullPointerException - if the input or output is null
        IOException - if an I/O error occurs
        Since:
        1.1
      • copy

        public static void copy​(InputStream input,
                                Writer output,
                                String encoding)
                         throws IOException
        Copy bytes from an InputStream to chars on a Writer using the specified character encoding.

        This method buffers the input internally, so there is no need to use a BufferedInputStream.

        Character encoding names can be found at IANA.

        This method uses InputStreamReader.

        Parameters:
        input - the InputStream to read from
        output - the Writer to write to
        encoding - the encoding to use, null means platform default
        Throws:
        NullPointerException - if the input or output is null
        IOException - if an I/O error occurs
        Since:
        1.1
      • copy

        public static int copy​(Reader input,
                               Writer output)
                        throws IOException
        Copy chars from a Reader to a Writer.

        This method buffers the input internally, so there is no need to use a BufferedReader.

        Parameters:
        input - the Reader to read from
        output - the Writer to write to
        Returns:
        the number of characters copied
        Throws:
        NullPointerException - if the input or output is null
        IOException - if an I/O error occurs
        Since:
        1.1
      • copy

        public static void copy​(Reader input,
                                OutputStream output)
                         throws IOException
        Copy chars from a Reader to bytes on an OutputStream using the default character encoding of the platform, and calling flush.

        This method buffers the input internally, so there is no need to use a BufferedReader.

        Due to the implementation of OutputStreamWriter, this method performs a flush.

        This method uses OutputStreamWriter.

        Parameters:
        input - the Reader to read from
        output - the OutputStream to write to
        Throws:
        NullPointerException - if the input or output is null
        IOException - if an I/O error occurs
        Since:
        1.1
      • copy

        public static void copy​(Reader input,
                                OutputStream output,
                                String encoding)
                         throws IOException
        Copy chars from a Reader to bytes on an OutputStream using the specified character encoding, and calling flush.

        This method buffers the input internally, so there is no need to use a BufferedReader.

        Character encoding names can be found at IANA.

        Due to the implementation of OutputStreamWriter, this method performs a flush.

        This method uses OutputStreamWriter.

        Parameters:
        input - the Reader to read from
        output - the OutputStream to write to
        encoding - the encoding to use, null means platform default
        Throws:
        NullPointerException - if the input or output is null
        IOException - if an I/O error occurs
        Since:
        1.1
      • contentEquals

        public static boolean contentEquals​(InputStream input1,
                                            InputStream input2)
                                     throws IOException
        Compare the contents of two Streams to determine if they are equal or not.

        This method buffers the input internally using BufferedInputStream if they are not already buffered.

        Parameters:
        input1 - the first stream
        input2 - the second stream
        Returns:
        true if the content of the streams are equal or they both don't exist, false otherwise
        Throws:
        NullPointerException - if either input is null
        IOException - if an I/O error occurs
      • contentEquals

        public static boolean contentEquals​(Reader input1,
                                            Reader input2)
                                     throws IOException
        Compare the contents of two Readers to determine if they are equal or not.

        This method buffers the input internally using BufferedReader if they are not already buffered.

        Parameters:
        input1 - the first reader
        input2 - the second reader
        Returns:
        true if the content of the readers are equal or they both don't exist, false otherwise
        Throws:
        NullPointerException - if either input is null
        IOException - if an I/O error occurs
        Since:
        1.1