Class IOUtils


  • public final class IOUtils
    extends java.lang.Object
    Utility class providing convenience methods for I/O operations.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void closeQuietly​(java.io.Closeable closeable)
      Closes a Closeable object quietly, suppressing any exceptions that might occur during the close operation.
      static long copy​(java.io.InputStream input, java.io.OutputStream output)
      Copies all bytes from the provided input stream to the given output stream.
      static byte[] toByteArray​(java.io.InputStream input)
      Converts the contents of the provided InputStream into a byte array.
      static java.lang.String toString​(java.io.Reader reader)
      Converts the contents of the provided Reader into a String.
      • Methods inherited from class java.lang.Object

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

      • copy

        public static long copy​(java.io.InputStream input,
                                java.io.OutputStream output)
                         throws java.io.IOException
        Copies all bytes from the provided input stream to the given output stream. The method operates using a buffer and is useful for transferring data between streams without modifying the contents.
        Parameters:
        input - the InputStream to read data from, must not be null
        output - the OutputStream to write data to, must not be null
        Returns:
        the total number of bytes copied
        Throws:
        java.io.IOException - if an I/O error occurs during reading or writing
      • toByteArray

        public static byte[] toByteArray​(java.io.InputStream input)
                                  throws java.io.IOException
        Converts the contents of the provided InputStream into a byte array. This method reads all the data from the InputStream and writes it into a ByteArrayOutputStream, which is then converted to a byte array.
        Parameters:
        input - the InputStream to read data from, must not be null
        Returns:
        a byte array containing all the data read from the InputStream
        Throws:
        java.io.IOException - if an I/O error occurs while reading from the InputStream
      • toString

        public static java.lang.String toString​(java.io.Reader reader)
                                         throws java.io.IOException
        Converts the contents of the provided Reader into a String. This method reads characters from the Reader into a buffer and then appends them to a StringBuilder, which is returned as a String.
        Parameters:
        reader - the Reader to read data from, must not be null
        Returns:
        a String containing all the characters read from the Reader
        Throws:
        java.io.IOException - if an I/O error occurs while reading from the Reader
      • closeQuietly

        public static void closeQuietly​(java.io.Closeable closeable)
        Closes a Closeable object quietly, suppressing any exceptions that might occur during the close operation. If the provided Closeable is null, this method does nothing.
        Parameters:
        closeable - the Closeable object to close; may be null.