Top Description Fields Constructors Methods
org.apache.commons.compress.utils

public final Class IOUtils

extends Object
Class Inheritance
Imports
java.io.ByteArrayOutputStream, .Closeable, .EOFException, .File, .IOException, .InputStream, .OutputStream, java.nio.ByteBuffer, java.nio.channels.ReadableByteChannel, java.nio.file.Files, .LinkOption, org.apache.commons.io.FileUtils

Utility functions.

Field Summary

Modifier and TypeField and Description
private static final int

Constructor Summary

AccessConstructor and Description
private
IOUtils()

Private constructor to prevent instantiation of this utility class.

Method Summary

Modifier and TypeMethod and Description
public static void
closeQuietly(final Closeable
Closeable to close, can be null
c
)

Closes the given Closeable and swallows any IOException that may occur.
public static void
copy(final File
The file to read.
sourceFile
,
final OutputStream
The output stream to write.
outputStream
)

Copies the source file to the given output stream.
public static long

Returns:

the number of bytes copied
copy
(final InputStream
the InputStream to copy
input
,
final OutputStream
the target, may be null to simulate output to dev/null on Linux and NUL on Windows
output
)

Copies the content of a InputStream into an OutputStream.
public static long

Returns:

the number of bytes copied
copy
(final InputStream
the InputStream to copy
input
,
final OutputStream
the target, may be null to simulate output to dev/null on Linux and NUL on Windows
output
,
final int
the buffer size to use, must be bigger than 0
bufferSize
)

Copies the content of a InputStream into an OutputStream
public static long

Returns:

the number of bytes copied
copyRange
(final InputStream
the InputStream to copy
input
,
final long
maximum amount of bytes to copy
len
,
final OutputStream
the target Stream
output
)

Copies part of the content of a InputStream into an OutputStream.
public static long

Returns:

the number of bytes copied
copyRange
(final InputStream
the InputStream to copy
input
,
final long
maximum amount of bytes to copy
length
,
final OutputStream
the target, may be null to simulate output to dev/null on Linux and NUL on Windows
output
,
final int
the buffer size to use, must be bigger than 0
bufferSize
)

Deprecated No longer used.
Copies part of the content of a InputStream into an OutputStream
public static int

Returns:

the number of bytes actually read
read
(final File
file to read
file
,
final byte[]
buffer to fill
array
)

Reads as much from the file as possible to fill the given array.
public static int

Returns:

the number of bytes actually read
readFully
(final InputStream
stream to read from
input
,
final byte[]
buffer to fill
array
)

Reads as much from input as possible to fill the given array.

public static int

Returns:

the number of bytes actually read
readFully
(final InputStream
stream to read from
input
,
final byte[]
buffer to fill
array
,
final int
offset into the buffer to start filling at
offset
,
final int
of bytes to read
length
)

Reads as much from input as possible to fill the given array with the given amount of bytes.

public static void
readFully(final ReadableByteChannel
the channel to read from
channel
,
final ByteBuffer
the buffer into which the data is read.
byteBuffer
)

Reads b.remaining() bytes from the given channel starting at the current channel's position.

public static byte[]

Returns:

the requested byte array
readRange
(final InputStream
the InputStream to read from
input
,
final int
maximum amount of bytes to copy
length
)

Gets part of the contents of an InputStream as a byte[].

public static byte[]

Returns:

the requested byte array
readRange
(final ReadableByteChannel
the ReadableByteChannel to read from
input
,
final int
maximum amount of bytes to copy
length
)

Gets part of the contents of an ReadableByteChannel as a byte[].

public static long

Returns:

the number of bytes actually skipped
skip
(final InputStream
stream to skip bytes in
input
,
final long
the number of bytes to skip
numToSkip
)

Skips the given number of bytes by repeatedly invoking skip on the given input stream if necessary.
public static byte[]

Returns:

the requested byte array
toByteArray
(final InputStream
the InputStream to read from
input
)

Gets the contents of an InputStream as a byte[].
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

COPY_BUF_SIZEback to summary
private static final int COPY_BUF_SIZE

Constructor Detail

IOUtilsback to summary
private IOUtils()

Private constructor to prevent instantiation of this utility class.

Method Detail

closeQuietlyback to summary
public static void closeQuietly(final Closeable c)

Closes the given Closeable and swallows any IOException that may occur.

Parameters
c:Closeable

Closeable to close, can be null

Annotations
@Deprecated
Since
1.7
copyback to summary
public static void copy(final File sourceFile, final OutputStream outputStream) throws IOException

Copies the source file to the given output stream.

Parameters
sourceFile:File

The file to read.

outputStream:OutputStream

The output stream to write.

Annotations
@Deprecated
Exceptions
IOException:
if an I/O error occurs when reading or writing.
Since
1.21
copyback to summary
public static long copy(final InputStream input, final OutputStream output) throws IOException

Copies the content of a InputStream into an OutputStream. Uses a default buffer size of 8024 bytes.

Parameters
input:InputStream

the InputStream to copy

output:OutputStream

the target, may be null to simulate output to dev/null on Linux and NUL on Windows

Returns:long

the number of bytes copied

Annotations
@Deprecated
Exceptions
IOException:
if an error occurs
copyback to summary
public static long copy(final InputStream input, final OutputStream output, final int bufferSize) throws IOException

Copies the content of a InputStream into an OutputStream

Parameters
input:InputStream

the InputStream to copy

output:OutputStream

the target, may be null to simulate output to dev/null on Linux and NUL on Windows

bufferSize:int

the buffer size to use, must be bigger than 0

Returns:long

the number of bytes copied

Annotations
@Deprecated
Exceptions
IOException:
if an error occurs
IllegalArgumentException:
if bufferSize is smaller than or equal to 0
copyRangeback to summary
public static long copyRange(final InputStream input, final long len, final OutputStream output) throws IOException

Copies part of the content of a InputStream into an OutputStream. Uses a default buffer size of 8024 bytes.

Parameters
input:InputStream

the InputStream to copy

len:long

maximum amount of bytes to copy

output:OutputStream

the target Stream

Returns:long

the number of bytes copied

Annotations
@Deprecated
Exceptions
IOException:
if an error occurs
Since
1.21
copyRangeback to summary
public static long copyRange(final InputStream input, final long length, final OutputStream output, final int bufferSize) throws IOException

Deprecated

No longer used.

Copies part of the content of a InputStream into an OutputStream

Parameters
input:InputStream

the InputStream to copy

length:long

maximum amount of bytes to copy

output:OutputStream

the target, may be null to simulate output to dev/null on Linux and NUL on Windows

bufferSize:int

the buffer size to use, must be bigger than 0

Returns:long

the number of bytes copied

Annotations
@Deprecated
Exceptions
IOException:
if an error occurs
IllegalArgumentException:
if bufferSize is smaller than or equal to 0
Since
1.21
readback to summary
public static int read(final File file, final byte[] array) throws IOException

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

file to read

array:byte[]

buffer to fill

Returns:int

the number of bytes actually read

Annotations
@Deprecated
Exceptions
IOException:
on error
Since
1.20
readFullyback to summary
public static int readFully(final InputStream input, final 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:InputStream

stream to read from

array:byte[]

buffer to fill

Returns:int

the number of bytes actually read

Exceptions
IOException:
on error
readFullyback to summary
public static int readFully(final InputStream input, final byte[] array, final int offset, final 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:InputStream

stream to read from

array:byte[]

buffer to fill

offset:int

offset into the buffer to start filling at

length:int

of bytes to read

Returns:int

the number of bytes actually read

Exceptions
IOException:
if an I/O error has occurred
readFullyback to summary
public static void readFully(final ReadableByteChannel channel, final 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:ReadableByteChannel

the channel to read from

byteBuffer:ByteBuffer

the buffer into which the data is read.

Exceptions
IOException:
if an I/O error occurs.
EOFException:
if the channel reaches the end before reading all the bytes.
readRangeback to summary
public static byte[] readRange(final InputStream input, final int length) throws IOException

Gets part of the contents of an InputStream as a byte[].

Parameters
input:InputStream

the InputStream to read from

length:int

maximum amount of bytes to copy

Returns:byte[]

the requested byte array

Exceptions
IOException:
if an I/O error occurs
NullPointerException:
if the input is null
Since
1.21
readRangeback to summary
public static byte[] readRange(final ReadableByteChannel input, final int length) throws IOException

Gets part of the contents of an ReadableByteChannel as a byte[].

Parameters
input:ReadableByteChannel

the ReadableByteChannel to read from

length:int

maximum amount of bytes to copy

Returns:byte[]

the requested byte array

Exceptions
IOException:
if an I/O error occurs
NullPointerException:
if the input is null
Since
1.21
skipback to summary
public static long skip(final InputStream input, final long numToSkip) throws IOException

Skips the given number of bytes by repeatedly invoking skip on the given input stream if necessary.

In a case where the stream's skip() method returns 0 before the requested number of bytes has been skip this implementation will fall back to using the read() method.

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

Parameters
input:InputStream

stream to skip bytes in

numToSkip:long

the number of bytes to skip

Returns:long

the number of bytes actually skipped

Annotations
@Deprecated
Exceptions
IOException:
on error
toByteArrayback to summary
public static byte[] toByteArray(final InputStream input) throws IOException

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:InputStream

the InputStream to read from

Returns:byte[]

the requested byte array

Annotations
@Deprecated
Exceptions
IOException:
if an I/O error occurs
NullPointerException:
if the input is null
Since
1.5