Class IOUtils
- java.lang.Object
-
- org.apache.commons.compress.utils.IOUtils
-
public final class IOUtils extends Object
Utility functions
-
-
Field Summary
Fields Modifier and Type Field Description static LinkOption[]
EMPTY_LINK_OPTIONS
Empty array of of typeLinkOption
.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
closeQuietly(Closeable c)
Closes the given Closeable and swallows any IOException that may occur.static void
copy(File sourceFile, OutputStream outputStream)
Copies the source file to the given output stream.static long
copy(InputStream input, OutputStream output)
Copies the content of a InputStream into an OutputStream.static long
copy(InputStream input, OutputStream output, int buffersize)
Copies the content of a InputStream into an OutputStreamstatic long
copyRange(InputStream input, long len, OutputStream output)
Copies part of the content of a InputStream into an OutputStream.static long
copyRange(InputStream input, long len, OutputStream output, int buffersize)
Copies part of the content of a InputStream into an OutputStreamstatic int
read(File file, byte[] array)
Reads as much from the file as possible to fill the given array.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 len)
Reads as much from input as possible to fill the given array with the given amount of bytes.static void
readFully(ReadableByteChannel channel, ByteBuffer b)
Readsb.remaining()
bytes from the given channel starting at the current channel's position.static byte[]
readRange(InputStream input, int len)
Gets part of the contents of anInputStream
as abyte[]
.static byte[]
readRange(ReadableByteChannel input, int len)
Gets part of the contents of anReadableByteChannel
as abyte[]
.static long
skip(InputStream input, long numToSkip)
Skips the given number of bytes by repeatedly invoking skip on the given input stream if necessary.static byte[]
toByteArray(InputStream input)
Gets the contents of anInputStream
as abyte[]
.
-
-
-
Field Detail
-
EMPTY_LINK_OPTIONS
public static final LinkOption[] EMPTY_LINK_OPTIONS
Empty array of of typeLinkOption
.- Since:
- 1.21
-
-
Method Detail
-
copy
public static long copy(InputStream input, OutputStream output) throws IOException
Copies the content of a InputStream into an OutputStream. Uses a default buffer size of 8024 bytes.- Parameters:
input
- the InputStream to copyoutput
- the target Stream- Returns:
- the number of bytes copied
- Throws:
IOException
- if an error occurs
-
copy
public static long copy(InputStream input, OutputStream output, int buffersize) throws IOException
Copies the content of a InputStream into an OutputStream- Parameters:
input
- the InputStream to copyoutput
- the target Streambuffersize
- the buffer size to use, must be bigger than 0- Returns:
- the number of bytes copied
- Throws:
IOException
- if an error occursIllegalArgumentException
- if buffersize is smaller than or equal to 0
-
skip
public static long skip(InputStream input, 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
- stream to skip bytes innumToSkip
- the number of bytes to skip- Returns:
- the number of bytes actually skipped
- Throws:
IOException
- on error
-
read
public static int read(File file, 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 to readarray
- 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 fromarray
- 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 len) 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 fromarray
- buffer to filloffset
- offset into the buffer to start filling atlen
- 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 b) throws IOException
Readsb.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 fromb
- 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.
-
toByteArray
public static byte[] toByteArray(InputStream input) throws IOException
Gets the contents of anInputStream
as abyte[]
.This method buffers the input internally, so there is no need to use a
BufferedInputStream
.- Parameters:
input
- theInputStream
to read from- Returns:
- the requested byte array
- Throws:
NullPointerException
- if the input is nullIOException
- if an I/O error occurs- Since:
- 1.5
-
closeQuietly
public static void closeQuietly(Closeable c)
Closes the given Closeable and swallows any IOException that may occur.- Parameters:
c
- Closeable to close, can be null- Since:
- 1.7
-
copy
public static void copy(File sourceFile, OutputStream outputStream) throws IOException
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
-
copyRange
public static long copyRange(InputStream input, long len, 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
- the InputStream to copyoutput
- the target Streamlen
- maximum amount of bytes to copy- Returns:
- the number of bytes copied
- Throws:
IOException
- if an error occurs- Since:
- 1.21
-
copyRange
public static long copyRange(InputStream input, long len, OutputStream output, int buffersize) throws IOException
Copies part of the content of a InputStream into an OutputStream- Parameters:
input
- the InputStream to copylen
- maximum amount of bytes to copyoutput
- the target Streambuffersize
- the buffer size to use, must be bigger than 0- Returns:
- the number of bytes copied
- Throws:
IOException
- if an error occursIllegalArgumentException
- if buffersize is smaller than or equal to 0- Since:
- 1.21
-
readRange
public static byte[] readRange(InputStream input, int len) throws IOException
Gets part of the contents of anInputStream
as abyte[]
.- Parameters:
input
- theInputStream
to read fromlen
- maximum amount of bytes to copy- Returns:
- the requested byte array
- Throws:
NullPointerException
- if the input is nullIOException
- if an I/O error occurs- Since:
- 1.21
-
readRange
public static byte[] readRange(ReadableByteChannel input, int len) throws IOException
Gets part of the contents of anReadableByteChannel
as abyte[]
.- Parameters:
input
- theReadableByteChannel
to read fromlen
- maximum amount of bytes to copy- Returns:
- the requested byte array
- Throws:
NullPointerException
- if the input is nullIOException
- if an I/O error occurs- Since:
- 1.21
-
-