Class IOUtils

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int BUFFER_SIZE
      The buffer size for I/O
    • Constructor Summary

      Constructors 
      Constructor Description
      IOUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void close​(java.io.Closeable closeable)
      Unconditionally close a Closeable.
      static void close​(java.net.URLConnection conn)
      Closes a URLConnection.
      static int copy​(java.io.InputStream in, java.io.OutputStream out)
      Copy the contents of the given InputStream to the given OutputStream.
      static byte[] toByteArray​(java.io.InputStream in)
      Copy the contents of the given InputStream into a new byte array.
      static java.lang.String toString​(java.io.InputStream in, java.lang.String encoding)
      Copy the contents of the given InputStream into a new String.
      static java.lang.String toString​(java.io.InputStream in, java.nio.charset.Charset charset)
      Copy the contents of the given InputStream into a new String.
      • Methods inherited from class java.lang.Object

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

      • BUFFER_SIZE

        public static final int BUFFER_SIZE
        The buffer size for I/O
    • Constructor Detail

      • IOUtils

        public IOUtils()
    • Method Detail

      • toByteArray

        public static byte[] toByteArray​(java.io.InputStream in)
                                  throws java.io.IOException
        Copy the contents of the given InputStream into a new byte array.

        Leaves the stream open when done.

        Parameters:
        in - the stream to copy from (may be null or empty)
        Returns:
        the new byte array that has been copied to (possibly empty)
        Throws:
        java.io.IOException - in case of I/O errors
      • toString

        public static java.lang.String toString​(java.io.InputStream in,
                                                java.lang.String encoding)
                                         throws java.io.IOException
        Copy the contents of the given InputStream into a new String.

        Leaves the stream open when done.

        Parameters:
        in - the stream to copy from (may be null or empty)
        encoding - the encoding to use, if it's null, take the SystemUtils.FILE_ENCODING as default
        Returns:
        the new byte array that has been copied to (possibly empty)
        Throws:
        java.io.IOException - in case of I/O errors
      • toString

        public static java.lang.String toString​(java.io.InputStream in,
                                                java.nio.charset.Charset charset)
                                         throws java.io.IOException
        Copy the contents of the given InputStream into a new String.

        Leaves the stream open when done.

        Parameters:
        in - the stream to copy from (may be null or empty)
        charset - the charset to use, if it's null, take the SystemUtils.FILE_ENCODING as default
        Returns:
        the new byte array that has been copied to (possibly empty)
        Throws:
        java.io.IOException - in case of I/O errors
      • copy

        public static int copy​(java.io.InputStream in,
                               java.io.OutputStream out)
                        throws java.io.IOException
        Copy the contents of the given InputStream to the given OutputStream.

        Leaves both streams open when done.

        Parameters:
        in - the InputStream to copy from
        out - the OutputStream to copy to
        Returns:
        the number of bytes copied
        Throws:
        java.io.IOException - in case of I/O errors
      • close

        public static void close​(java.net.URLConnection conn)
        Closes a URLConnection.
        Parameters:
        conn - the connection to close.
      • close

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

        Equivalent to Closeable.close(), except any exceptions will be ignored. This is typically used in finally blocks.

        Example code:

           Closeable closeable = null;
           try {
               closeable = new FileReader("foo.txt");
               // process closeable
               closeable.close();
           } catch (Exception e) {
               // error handling
           } finally {
               IOUtils.closeQuietly(closeable);
           }
         
        Parameters:
        closeable - the object to close, may be null or already closed