This class provides static utility methods for input/output operations.
The byte-to-char methods and char-to-byte methods involve a conversion step. Two methods are provided in each case, one that uses the platform default encoding and the other which allows you to specify an encoding. You are encouraged to always specify an encoding because relying on the platform default can lead to unexpected results, for example when moving from development to production.
All the methods in this class that read a stream are buffered internally.
This means that there is no cause to use a BufferedInputStream
or BufferedReader. The default buffer size of 4K has been shown
to be efficient in tests.
Wherever possible, the methods in this class do not flush or close the stream. This is to avoid making non-portable assumptions about the streams' origin and further use. Thus the caller is still responsible for closing streams after use.
Origin of code: Excalibur.
| Modifier and Type | Field and Description |
|---|---|
| private static final int | DEFAULT_BUFFER_SIZE
The default buffer size (4096) to use for
|
| public static final int | EOF
Represents the end-of-file (or stream). |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public static void | closeQuietly(final Closeable
the objects to close, may be null or already closed closeable)Closes a |
| public static int | Returns: the number of bytes copied, or -1 if > Integer.MAX_VALUEthe input, final OutputStream InputStream to read fromthe output)OutputStream to write toCopies bytes from an |
| public static long | Returns: the number of bytes copiedthe input, final OutputStream InputStream to read fromthe output)OutputStream to write toCopies bytes from a large (over 2GB) |
| public static int | Returns: actual length read; may be less than requested if EOF was reachedwhere to read input from input, final byte[] destination buffer, final int initial offset into buffer offset, final int length to read, must be >= 0 length)Reads bytes from an input stream. |
| public static void | readFully(final InputStream
where to read input from input, final byte[] destination buffer, final int initial offset into buffer offset, final int length to read, must be >= 0 length)Reads the requested number of bytes or fail if there are not enough left. |
| public static void | readFully(final InputStream
where to read input from input, final byte[] destination buffer)Reads the requested number of bytes or fail if there are not enough left. |
| DEFAULT_BUFFER_SIZE | back to summary |
|---|---|
| private static final int DEFAULT_BUFFER_SIZE The default buffer size (4096) to use for
| |
| EOF | back to summary |
|---|---|
| public static final int EOF Represents the end-of-file (or stream).
| |
| IOUtils | back to summary |
|---|---|
| public IOUtils() | |
| closeQuietly | back to summary |
|---|---|
| public static void closeQuietly(final Closeable closeable) Closes a
Equivalent to Example code:
Closeable closeable = null;
try {
closeable = new FileReader("foo.txt");
// process closeable
closeable.close();
} catch (Exception e) {
// error handling
} finally {
IOUtils.closeQuietly(closeable);
}
Closing all streams:
try {
return IOUtils.copy(inputStream, outputStream);
} finally {
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
}
| |
| copy | back to summary |
|---|---|
| public static int copy(final InputStream input, final OutputStream output) throws IOException Copies bytes from an
This method buffers the input internally, so there is no need to use a
Large streams (over 2GB) will return a bytes copied value of
| |
| copyLarge | back to summary |
|---|---|
| public static long copyLarge(final InputStream input, final OutputStream output) throws IOException Copies bytes from a large (over 2GB)
This method buffers the input internally, so there is no need to use a
The buffer size is given by
| |
| read | back to summary |
|---|---|
| public static int read(final InputStream input, final byte[] buffer, final int offset, final int length) throws IOException Reads bytes from an input stream.
This implementation guarantees that it will read as many bytes
as possible before giving up; this may not always be the case for
subclasses of
| |
| readFully | back to summary |
|---|---|
| public static void readFully(final InputStream input, final byte[] buffer, final int offset, final int length) throws IOException Reads the requested number of bytes or fail if there are not enough left.
This allows for the possibility that
| |
| readFully | back to summary |
|---|---|
| public static void readFully(final InputStream input, final byte[] buffer) throws IOException Reads the requested number of bytes or fail if there are not enough left.
This allows for the possibility that
| |