Class IOUtils

java.lang.Object
com.alibaba.cloud.commons.io.IOUtils

public final class IOUtils extends Object
The IOUtils. copy from apache commons-io.
Author:
theonefx
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Represents the end-of-file (or stream).
  • Method Summary

    Modifier and Type
    Method
    Description
    static long
    copy(InputStream input, OutputStream output, int bufferSize)
    Copies bytes from an InputStream to an OutputStream using an internal buffer of the given size.
    static void
    copy(InputStream input, Writer output, Charset inputEncoding)
    Copies bytes from an InputStream to chars on a Writer using the specified character encoding.
    static int
    copy(Reader input, Writer output)
    Copies chars from a Reader to a Writer.
    static long
    Copies bytes from a large (over 2GB) InputStream to an OutputStream.
    static long
    copyLarge(InputStream input, OutputStream output, byte[] buffer)
    Copies bytes from a large (over 2GB) InputStream to an OutputStream.
    static long
    copyLarge(Reader input, Writer output)
    Copies chars from a large (over 2GB) Reader to a Writer.
    static long
    copyLarge(Reader input, Writer output, char[] buffer)
    Copies chars from a large (over 2GB) Reader to a Writer.
    static String
    toString(InputStream input, Charset encoding)
    Gets the contents of an InputStream as a String using the specified character encoding.

    Methods inherited from class java.lang.Object

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

    • EOF

      public static final int EOF
      Represents the end-of-file (or stream).
      Since:
      2.5 (made public)
      See Also:
  • Method Details

    • toString

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

      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
      Since:
      2.3
    • copy

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

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

      Large streams (over 2GB) will return a chars copied value of -1 after the copy has completed since the correct number of chars cannot be returned as an int. For large streams use the copyLarge(Reader, Writer) method.

      Parameters:
      input - the Reader to read from
      output - the Writer to write to
      Returns:
      the number of characters copied, or -1 if > Integer.MAX_VALUE
      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, Charset inputEncoding) throws IOException
      Copies 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.

      This method uses InputStreamReader.

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

      public static long copy(InputStream input, OutputStream output, int bufferSize) throws IOException
      Copies bytes from an InputStream to an OutputStream using an internal buffer of the given size.

      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
      bufferSize - the bufferSize used to copy from the input to the output
      Returns:
      the number of bytes copied
      Throws:
      NullPointerException - if the input or output is null
      IOException - if an I/O error occurs
      Since:
      2.5
    • copyLarge

      public static long copyLarge(Reader input, Writer output) throws IOException
      Copies chars from a large (over 2GB) Reader to a Writer.

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

      The buffer size is given by DEFAULT_BUFFER_SIZE.

      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.3
    • copyLarge

      public static long copyLarge(Reader input, Writer output, char[] buffer) throws IOException
      Copies chars from a large (over 2GB) Reader to a Writer.

      This method uses the provided buffer, so there is no need to use a BufferedReader.

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

      public static long copyLarge(InputStream input, OutputStream output) throws IOException
      Copies bytes from a large (over 2GB) InputStream to an OutputStream.

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

      The buffer size is given by DEFAULT_BUFFER_SIZE.

      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.3
    • copyLarge

      public static long copyLarge(InputStream input, OutputStream output, byte[] buffer) throws IOException
      Copies bytes from a large (over 2GB) InputStream to an OutputStream.

      This method uses the provided buffer, so there is no need to use a BufferedInputStream.

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