IOUtils

This utility class contains input/output functions.

Methods
static void closeSilently(Closeable out)
Close a Closeable without throwing an exception.
static void closeSilently(Closeable out)
Close a Closeable without throwing an exception.
Parameters:
out - the Closeable or null
static void closeSilently(AutoCloseable out)
Close an AutoCloseable without throwing an exception.
static void closeSilently(AutoCloseable out)
Close an AutoCloseable without throwing an exception.
Parameters:
out - the AutoCloseable or null
static void closeSilently(InputStream in)
Close an input stream without throwing an exception.
static void closeSilently(InputStream in)
Close an input stream without throwing an exception.
Parameters:
in - the input stream or null
static void closeSilently(Reader reader)
Close a reader without throwing an exception.
static void closeSilently(Reader reader)
Close a reader without throwing an exception.
Parameters:
reader - the reader or null
static void closeSilently(Writer writer)
Close a writer without throwing an exception.
static void closeSilently(Writer writer)
Close a writer without throwing an exception.
Parameters:
writer - the writer or null
static long copy(InputStream in, OutputStream out)
Copy all data from the input stream to the output stream.
static long copy(InputStream in, OutputStream out) throws IOException
Copy all data from the input stream to the output stream. Both streams are kept open.
Parameters:
in - the input stream
out - the output stream (null if writing is not required)
Returns:
the number of bytes copied
static long copy(InputStream in, OutputStream out, long length)
Copy all data from the input stream to the output stream.
static long copy(InputStream in, OutputStream out, long length) throws IOException
Copy all data from the input stream to the output stream. Both streams are kept open.
Parameters:
in - the input stream
out - the output stream (null if writing is not required)
length - the maximum number of bytes to copy
Returns:
the number of bytes copied
static long copyAndClose(InputStream in, OutputStream out)
Copy all data from the input stream to the output stream and close both streams.
static long copyAndClose(InputStream in, OutputStream out) throws IOException
Copy all data from the input stream to the output stream and close both streams. Exceptions while closing are ignored.
Parameters:
in - the input stream
out - the output stream
Returns:
the number of bytes copied
static long copyAndCloseInput(InputStream in, OutputStream out)
Copy all data from the input stream to the output stream and close the input stream.
static long copyAndCloseInput(InputStream in, OutputStream out) throws IOException
Copy all data from the input stream to the output stream and close the input stream. Exceptions while closing are ignored.
Parameters:
in - the input stream
out - the output stream (null if writing is not required)
Returns:
the number of bytes copied
static long copyAndCloseInput(Reader in, Writer out, long length)
Copy all data from the reader to the writer and close the reader.
static long copyAndCloseInput(Reader in, Writer out, long length) throws IOException
Copy all data from the reader to the writer and close the reader. Exceptions while closing are ignored.
Parameters:
in - the reader
out - the writer (null if writing is not required)
length - the maximum number of bytes to copy
Returns:
the number of characters copied
static void copyFiles(String original, String copy)
Copy a file from one directory to another, or to another file.
static void copyFiles(String original, String copy) throws IOException
Copy a file from one directory to another, or to another file.
Parameters:
original - the original file name
copy - the file name of the copy
static Reader getAsciiReader(InputStream in)
Wrap an input stream in a reader.
static Reader getAsciiReader(InputStream in)
Wrap an input stream in a reader. The bytes are converted to characters using the US-ASCII character set.
Parameters:
in - the input stream
Returns:
the reader
static Reader getBufferedReader(InputStream in)
Create a buffered reader to read from an input stream using the UTF-8 format.
static Reader getBufferedReader(InputStream in)
Create a buffered reader to read from an input stream using the UTF-8 format. If the input stream is null, this method returns null. The InputStreamReader that is used here is not exact, that means it may read some additional bytes when buffering.
Parameters:
in - the input stream or null
Returns:
the reader
static Writer getBufferedWriter(OutputStream out)
Create a buffered writer to write to an output stream using the UTF-8 format.
static Writer getBufferedWriter(OutputStream out)
Create a buffered writer to write to an output stream using the UTF-8 format. If the output stream is null, this method returns null.
Parameters:
out - the output stream or null
Returns:
the writer
static InputStream getInputStreamFromString(String s)
Create an input stream to read from a string.
static InputStream getInputStreamFromString(String s)
Create an input stream to read from a string. The string is converted to a byte array using UTF-8 encoding. If the string is null, this method returns null.
Parameters:
s - the string
Returns:
the input stream
static Reader getReader(InputStream in)
Create a reader to read from an input stream using the UTF-8 format.
static Reader getReader(InputStream in)
Create a reader to read from an input stream using the UTF-8 format. If the input stream is null, this method returns null. The InputStreamReader that is used here is not exact, that means it may read some additional bytes when buffering.
Parameters:
in - the input stream or null
Returns:
the reader
static byte[] readBytesAndClose(InputStream in, int length)
Read a number of bytes from an input stream and close the stream.
static byte[] readBytesAndClose(InputStream in, int length) throws IOException
Read a number of bytes from an input stream and close the stream.
Parameters:
in - the input stream
length - the maximum number of bytes to read, or -1 to read until the end of file
Returns:
the bytes read
static int readFully(InputStream in, byte[] buffer, int max)
Try to read the given number of bytes to the buffer.
static int readFully(InputStream in, byte[] buffer, int max) throws IOException
Try to read the given number of bytes to the buffer. This method reads until the maximum number of bytes have been read or until the end of file.
Parameters:
in - the input stream
buffer - the output buffer
max - the number of bytes to read at most
Returns:
the number of bytes read, 0 meaning EOF
static int readFully(Reader in, char[] buffer, int max)
Try to read the given number of characters to the buffer.
static int readFully(Reader in, char[] buffer, int max) throws IOException
Try to read the given number of characters to the buffer. This method reads until the maximum number of characters have been read or until the end of file.
Parameters:
in - the reader
buffer - the output buffer
max - the number of characters to read at most
Returns:
the number of characters read, 0 meaning EOF
static String readStringAndClose(Reader in, int length)
Read a number of characters from a reader and close it.
static String readStringAndClose(Reader in, int length) throws IOException
Read a number of characters from a reader and close it.
Parameters:
in - the reader
length - the maximum number of characters to read, or -1 to read until the end of file
Returns:
the string read
static void skipFully(InputStream in, long skip)
Skip a number of bytes in an input stream.
static void skipFully(InputStream in, long skip) throws IOException
Skip a number of bytes in an input stream.
Parameters:
in - the input stream
skip - the number of bytes to skip
Throws:
EOFException - if the end of file has been reached before all bytes could be skipped
IOException - if an IO exception occurred while skipping
static void skipFully(Reader reader, long skip)
Skip a number of characters in a reader.
static void skipFully(Reader reader, long skip) throws IOException
Skip a number of characters in a reader.
Parameters:
reader - the reader
skip - the number of characters to skip
Throws:
EOFException - if the end of file has been reached before all characters could be skipped
IOException - if an IO exception occurred while skipping
static void trace(String method, String fileName, Object o)
Trace input or output operations if enabled.
static void trace(String method, String fileName, Object o)
Trace input or output operations if enabled.
Parameters:
method - the method from where this method was called
fileName - the file name
o - the object to append to the message