Package org.apache.commons.compress.utils

# Class IOUtils

java.lang.Object
org.apache.commons.compress.utils.IOUtils

public final class IOUtils extends Object
Utility functions.

This class is immutable
  (has mutable data but it is write-only).

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final LinkOption[]`
  `EMPTY_LINK_OPTIONS`
  Empty array of type `LinkOption`.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static void`
  `closeQuietly(Closeable c)`
  Deprecated.
  Use `IOUtils.closeQuietly(Closeable)`.
  `static void`
  `copy(File sourceFile, OutputStream outputStream)`
  Deprecated.
  Use `FileUtils.copyFile(File, OutputStream)`.
  `static long`
  `copy(InputStream input, OutputStream output)`
  Deprecated.
  Use `IOUtils.copy(InputStream, OutputStream)`.
  `static long`
  `copy(InputStream input, OutputStream output, int bufferSize)`
  Deprecated.
  Use `IOUtils.copy(InputStream, OutputStream, int)`.
  `static long`
  `copyRange(InputStream input, long len, OutputStream output)`
  Deprecated.
  Use `IOUtils.copyLarge(InputStream, OutputStream, long, long)`.
  `static long`
  `copyRange(InputStream input, long length, OutputStream output, int bufferSize)`
  Deprecated.
  No longer used.
  `static int`
  `read(File file, byte[] array)`
  Deprecated.
  Use `Files.readAllBytes(java.nio.file.Path)`.
  `static int`
  `readFully(InputStream input, byte[] array)`
  Reads as much from input as possible to fill the given array.
  `static int`
  `readFully(InputStream input, byte[] array, int offset, int length)`
  Reads as much from input as possible to fill the given array with the given amount of bytes.
  `static void`
  `readFully(ReadableByteChannel channel, ByteBuffer byteBuffer)`
  Reads `b.remaining()` bytes from the given channel starting at the current channel's position.
  `static byte[]`
  `readRange(InputStream input, int length)`
  Gets part of the contents of an `InputStream` as a `byte[]`.
  `static byte[]`
  `readRange(ReadableByteChannel input, int length)`
  Gets part of the contents of an `ReadableByteChannel` as a `byte[]`.
  `static long`
  `skip(InputStream input, long toSkip)`
  Skips bytes from an input byte stream.
  `static byte[]`
  `toByteArray(InputStream input)`
  Deprecated.
  Use `IOUtils.toByteArray(InputStream)`.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### EMPTY_LINK_OPTIONS

    public static final LinkOption[] EMPTY_LINK_OPTIONS
    Empty array of type `LinkOption`.
    Since:
      1.21

- ## Method Details

  - ### closeQuietly

    @Deprecated public static void closeQuietly(Closeable c)
    Deprecated.
    Use `IOUtils.closeQuietly(Closeable)`.
    Closes the given Closeable and swallows any IOException that may occur.
    Parameters:
      `c` - Closeable to close, can be null
    Since:
      1.7

  - ### copy

    @Deprecated public static void copy(File sourceFile, OutputStream outputStream) throws IOException
    Deprecated.
    Use `FileUtils.copyFile(File, OutputStream)`.
    Copies the source file to the given output stream.
    Parameters:
      `sourceFile` - The file to read.
      `outputStream` - The output stream to write.
    Throws:
      `IOException` - if an I/O error occurs when reading or writing.
    Since:
      1.21

  - ### copy

    @Deprecated public static long copy(InputStream input, OutputStream output) throws IOException
    Deprecated.
    Use `IOUtils.copy(InputStream, OutputStream)`.
    Copies the content of a InputStream into an OutputStream. Uses a default buffer size of 8192 bytes.
    Parameters:
      `input` - the InputStream to copy
      `output` - the target
    Returns:
      the number of bytes copied, or -1 if greater than `Integer.MAX_VALUE`
    Throws:
      `IOException` - if an error occurs
      `NullPointerException` - if the `input` or the `output` is `null`

  - ### copy

    @Deprecated public static long copy(InputStream input, OutputStream output, int bufferSize) throws IOException
    Deprecated.
    Use `IOUtils.copy(InputStream, OutputStream, int)`.
    Copies the content of a InputStream into an OutputStream
    Parameters:
      `input` - the InputStream to copy
      `output` - the target
      `bufferSize` - the buffer size to use, must be bigger than 0
    Returns:
      the number of bytes copied
    Throws:
      `IOException` - if an error occurs
      `NullPointerException` - if the `input` or the `output` is `null`

  - ### copyRange

    @Deprecated public static long copyRange(InputStream input, long len, OutputStream output) throws IOException
    Deprecated.
    Use `IOUtils.copyLarge(InputStream, OutputStream, long, long)`.
    Copies part of the content of a InputStream into an OutputStream. Uses a default buffer size of 8192 bytes.
    Parameters:
      `input` - the InputStream to copy
      `len` - maximum amount of bytes to copy
      `output` - the target Stream
    Returns:
      the number of bytes copied
    Throws:
      `IOException` - if an error occurs
    Since:
      1.21

  - ### copyRange

    @Deprecated public static long copyRange(InputStream input, long length, OutputStream output, int bufferSize) throws IOException
    Deprecated.
    No longer used.
    Copies part of the content of a InputStream into an OutputStream
    Parameters:
      `input` - the InputStream to copy
      `length` - maximum amount of bytes to copy
      `output` - the target, may be null to simulate output to dev/null on Linux and NUL on Windows
      `bufferSize` - the buffer size to use, must be bigger than 0
    Returns:
      the number of bytes copied
    Throws:
      `IOException` - if an error occurs
      `IllegalArgumentException` - if bufferSize is smaller than or equal to 0
    Since:
      1.21

  - ### read

    @Deprecated public static int read(File file, byte[] array) throws IOException
    Deprecated.
    Use `Files.readAllBytes(java.nio.file.Path)`.
    Reads as much from the file as possible to fill the given array.

    This method may invoke read repeatedly to fill the array and only read less bytes than the length of the array if the end of the stream has been reached.

    Parameters:
      `file` - file to read
      `array` - buffer to fill
    Returns:
      the number of bytes actually read
    Throws:
      `IOException` - on error
    Since:
      1.20

  - ### readFully

    public static int readFully(InputStream input, byte[] array) throws IOException
    Reads as much from input as possible to fill the given array.

    This method may invoke read repeatedly to fill the array and only read less bytes than the length of the array if the end of the stream has been reached.

    Parameters:
      `input` - stream to read from
      `array` - buffer to fill
    Returns:
      the number of bytes actually read
    Throws:
      `IOException` - on error

  - ### readFully

    public static int readFully(InputStream input, byte[] array, int offset, int length) throws IOException
    Reads as much from input as possible to fill the given array with the given amount of bytes.

    This method may invoke read repeatedly to read the bytes and only read less bytes than the requested length if the end of the stream has been reached.

    Parameters:
      `input` - stream to read from
      `array` - buffer to fill
      `offset` - offset into the buffer to start filling at
      `length` - of bytes to read
    Returns:
      the number of bytes actually read
    Throws:
      `IOException` - if an I/O error has occurred

  - ### readFully

    public static void readFully(ReadableByteChannel channel, ByteBuffer byteBuffer) throws IOException
    Reads `b.remaining()` bytes from the given channel starting at the current channel's position.

    This method reads repeatedly from the channel until the requested number of bytes are read. This method blocks until the requested number of bytes are read, the end of the channel is detected, or an exception is thrown.

    Parameters:
      `channel` - the channel to read from
      `byteBuffer` - the buffer into which the data is read.
    Throws:
      `IOException` - if an I/O error occurs.
      `EOFException` - if the channel reaches the end before reading all the bytes.

  - ### readRange

    public static byte[] readRange(InputStream input, int length) throws IOException
    Gets part of the contents of an `InputStream` as a `byte[]`.
    Parameters:
      `input` - the `InputStream` to read from
      `length` - maximum amount of bytes to copy
    Returns:
      the requested byte array
    Throws:
      `NullPointerException` - if the input is null
      `IOException` - if an I/O error occurs
    Since:
      1.21

  - ### readRange

    public static byte[] readRange(ReadableByteChannel input, int length) throws IOException
    Gets part of the contents of an `ReadableByteChannel` as a `byte[]`.
    Parameters:
      `input` - the `ReadableByteChannel` to read from
      `length` - maximum amount of bytes to copy
    Returns:
      the requested byte array
    Throws:
      `NullPointerException` - if the input is null
      `IOException` - if an I/O error occurs
    Since:
      1.21

  - ### skip

    public static long skip(InputStream input, long toSkip) throws IOException
    Skips bytes from an input byte stream.

    This method will only skip less than the requested number of bytes if the end of the input stream has been reached.

    Parameters:
      `input` - stream to skip bytes in
      `toSkip` - the number of bytes to skip
    Returns:
      the number of bytes actually skipped
    Throws:
      `IOException` - on error

  - ### toByteArray

    @Deprecated public static byte[] toByteArray(InputStream input) throws IOException
    Deprecated.
    Use `IOUtils.toByteArray(InputStream)`.
    Gets 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 or reads more than `Integer.MAX_VALUE` occurs
    Since:
      1.5