public final class IOUtils extends Object
| Modifier and Type | Method and Description |
|---|---|
static void |
closeQuietly(Closeable closeable)
Unconditionally close a
Closeable. |
static int |
copy(InputStream input,
OutputStream output)
copy bytes from an
InputStream to an
OutputStream. |
static long |
copyLarge(InputStream input,
OutputStream output)
copy bytes from a large (over 2GB)
InputStream to an
OutputStream. |
static long |
copyLarge(InputStream input,
OutputStream output,
byte[] buffer)
copy bytes from a large (over 2GB)
InputStream to an
OutputStream. |
static byte[] |
toByteArray(InputStream input)
Get the contents of an
InputStream as a byte[]. |
public static byte[] toByteArray(InputStream input) throws IOException
InputStream as a byte[].
This method buffers the input internally, so there is no need to use a
BufferedInputStream.
input - the InputStream to read fromNullPointerException - if the input is nullIOException - if an I/O error occurspublic static int copy(InputStream input, OutputStream output) throws IOException
InputStream to an
OutputStream.
This method buffers the input internally, so there is no need to use a
BufferedInputStream.
Large streams (over 2GB) will return a bytes copied value of
-1 after the copy has completed since the correct
number of bytes cannot be returned as an int. For large streams
use the copyLarge(InputStream, OutputStream) method.
input - the InputStream to read fromoutput - the OutputStream to write toNullPointerException - if the input or output is nullIOException - if an I/O error occurspublic static long copyLarge(InputStream input, OutputStream output) throws IOException
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.
input - the InputStream to read fromoutput - the OutputStream to write toNullPointerException - if the input or output is nullIOException - if an I/O error occurspublic static long copyLarge(InputStream input, OutputStream output, byte[] buffer) throws IOException
InputStream to an
OutputStream.
This method uses the provided buffer, so there is no need to use a
BufferedInputStream.
input - the InputStream to read fromoutput - the OutputStream to write tobuffer - the buffer to use for the copyNullPointerException - if the input or output is nullIOException - if an I/O error occurspublic static void closeQuietly(Closeable closeable)
Closeable.
Equivalent to Closeable.close(), except any exceptions will be ignored.
This is typically used in finally blocks.
Example code:
Closeable closeable = null;
try {
closeable = new FileReader("foo.txt");
// process closeable
closeable.close();
} catch (Exception e) {
// error handling
} finally {
IOUtils.closeQuietly(closeable);
}
closeable - the object to close, may be null or already closedCopyright © 2018. All rights reserved.