public final class IOUtils extends Object
Modifier and Type | Method and Description |
---|---|
static void |
closeQuietly(Closeable... closeables)
Closes a
Closeable unconditionally. |
static void |
closeQuietly(Closeable closeable)
Closes a
Closeable unconditionally. |
static void |
closeQuietly(InputStream input)
Closes an
InputStream unconditionally. |
static int |
copy(InputStream input,
OutputStream output)
Copies bytes from an
InputStream to an
OutputStream . |
static long |
copy(InputStream input,
OutputStream output,
int bufferSize)
Copies bytes from an
InputStream to an OutputStream using an internal buffer of the
given size. |
static void |
copy(InputStream input,
Writer output,
Charset inputEncoding)
Copies bytes from an
InputStream to chars on a
Writer using the specified character encoding. |
static int |
copy(Reader input,
Writer output)
Copies chars from a
Reader to a Writer . |
static long |
copyLarge(InputStream input,
OutputStream output)
Copies bytes from a large (over 2GB)
InputStream to an
OutputStream . |
static long |
copyLarge(InputStream input,
OutputStream output,
byte[] buffer)
Copies bytes from a large (over 2GB)
InputStream to an
OutputStream . |
static long |
copyLarge(Reader input,
Writer output)
Copies chars from a large (over 2GB)
Reader to a Writer . |
static long |
copyLarge(Reader input,
Writer output,
char[] buffer)
Copies chars from a large (over 2GB)
Reader to a Writer . |
static List<String> |
readLines(InputStream input,
Charset encoding)
Gets the contents of an
InputStream as a list of Strings,
one entry per line, using the specified character encoding. |
static List<String> |
readLines(Reader input)
Gets the contents of a
Reader as a list of Strings,
one entry per line. |
static BufferedReader |
toBufferedReader(Reader reader)
Returns the given reader if it is a
BufferedReader , otherwise creates a BufferedReader from the given
reader. |
static byte[] |
toByteArray(InputStream input,
int size)
Gets the contents of an
InputStream as a byte[] . |
static byte[] |
toByteArray(InputStream input,
long size)
Gets contents of an
InputStream as a byte[] . |
static Charset |
toCharset(Charset charset)
Returns the given Charset or the default Charset if the given Charset is null.
|
static Charset |
toCharset(String charset)
Returns a Charset for the named charset.
|
static String |
toString(InputStream input,
Charset encoding)
Gets the contents of an
InputStream as a String
using the specified character encoding. |
static void |
write(String data,
OutputStream output,
Charset encoding)
Writes chars from a
String to bytes on an
OutputStream using the specified character encoding. |
public static void closeQuietly(InputStream input)
InputStream
unconditionally.
Equivalent to InputStream.close()
, except any exceptions will be ignored.
This is typically used in finally blocks.
Example code:
byte[] data = new byte[1024]; InputStream in = null; try { in = new FileInputStream("foo.txt"); in.read(data); in.close(); //close errors are handled } catch (Exception e) { // error handling } finally { IOUtils.closeQuietly(in); }
input
- the InputStream to close, may be null or already closedpublic static void closeQuietly(Closeable... closeables)
Closeable
unconditionally.
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); }Closing all streams:
try { return IOUtils.copy(inputStream, outputStream); } finally { IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(outputStream); }
closeables
- the objects to close, may be null or already closedpublic static void closeQuietly(Closeable closeable)
Closeable
unconditionally.
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); }Closing all streams:
try { return IOUtils.copy(inputStream, outputStream); } finally { IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(outputStream); }
closeable
- the objects to close, may be null or already closedpublic static String toString(InputStream input, Charset encoding) throws IOException
InputStream
as a String
using the specified character encoding.
This method buffers the input internally, so there is no need to use a
BufferedInputStream
.
input
- the InputStream
to read fromencoding
- the encoding to use, null means platform defaultNullPointerException
- if the input is nullIOException
- if an I/O error occurspublic static Charset toCharset(Charset charset)
charset
- A charset or null.public static Charset toCharset(String charset)
charset
- The name of the requested charset, may be null.public static void copy(InputStream input, Writer output, Charset inputEncoding) throws IOException
InputStream
to chars on a
Writer
using the specified character encoding.
This method buffers the input internally, so there is no need to use a
BufferedInputStream
.
This method uses InputStreamReader
.
input
- the InputStream
to read fromoutput
- the Writer
to write toinputEncoding
- the encoding to use for the input stream, null means platform defaultNullPointerException
- if the input or output is nullIOException
- if an I/O error occurspublic static int copy(Reader input, Writer output) throws IOException
Reader
to a Writer
.
This method buffers the input internally, so there is no need to use a
BufferedReader
.
Large streams (over 2GB) will return a chars copied value of
-1
after the copy has completed since the correct
number of chars cannot be returned as an int. For large streams
use the copyLarge(Reader, Writer)
method.
input
- the Reader
to read fromoutput
- the Writer
to write toNullPointerException
- if the input or output 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 copy(InputStream input, OutputStream output, int bufferSize) throws IOException
InputStream
to an OutputStream
using an internal buffer of the
given size.
This method buffers the input internally, so there is no need to use a BufferedInputStream
.
input
- the InputStream
to read fromoutput
- the OutputStream
to write tobufferSize
- the bufferSize used to copy from the input to the outputNullPointerException
- if the input or output is nullIOException
- if an I/O error occurspublic static long copyLarge(Reader input, Writer output) throws IOException
Reader
to a Writer
.
This method buffers the input internally, so there is no need to use a
BufferedReader
.
The buffer size is given by #DEFAULT_BUFFER_SIZE.
input
- the Reader
to read fromoutput
- the Writer
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 long copyLarge(Reader input, Writer output, char[] buffer) throws IOException
Reader
to a Writer
.
This method uses the provided buffer, so there is no need to use a
BufferedReader
.
input
- the Reader
to read fromoutput
- the Writer
to write tobuffer
- the buffer to be used for the copyNullPointerException
- if the input or output is nullIOException
- if an I/O error occurspublic static void write(String data, OutputStream output, Charset encoding) throws IOException
String
to bytes on an
OutputStream
using the specified character encoding.
This method uses String.getBytes(String)
.
data
- the String
to write, null ignoredoutput
- the OutputStream
to write toencoding
- the encoding to use, null means platform defaultNullPointerException
- if output is nullIOException
- if an I/O error occurspublic static byte[] toByteArray(InputStream input, int size) throws IOException
InputStream
as a byte[]
.
Use this method instead of toByteArray(InputStream)
when InputStream
size is knowninput
- the InputStream
to read fromsize
- the size of InputStream
IOException
- if an I/O error occurs or InputStream
size differ from parameter sizeIllegalArgumentException
- if size is less than zeropublic static byte[] toByteArray(InputStream input, long size) throws IOException
InputStream
as a byte[]
.
Use this method instead of toByteArray(InputStream)
when InputStream
size is known.
NOTE: the method checks that the length can safely be cast to an int without truncation
before using toByteArray(java.io.InputStream, int)
to read into the byte array.
(Arrays can have no more than Integer.MAX_VALUE entries anyway)input
- the InputStream
to read fromsize
- the size of InputStream
IOException
- if an I/O error occurs or InputStream
size differ from parameter sizeIllegalArgumentException
- if size is less than zero or size is greater than Integer.MAX_VALUEtoByteArray(java.io.InputStream, int)
public static List<String> readLines(InputStream input, Charset encoding) throws IOException
InputStream
as a list of Strings,
one entry per line, using the specified character encoding.
This method buffers the input internally, so there is no need to use a
BufferedInputStream
.
input
- the InputStream
to read from, not nullencoding
- the encoding to use, null means platform defaultNullPointerException
- if the input is nullIOException
- if an I/O error occurspublic static List<String> readLines(Reader input) throws IOException
Reader
as a list of Strings,
one entry per line.
This method buffers the input internally, so there is no need to use a
BufferedReader
.
input
- the Reader
to read from, not nullNullPointerException
- if the input is nullIOException
- if an I/O error occurspublic static BufferedReader toBufferedReader(Reader reader)
BufferedReader
, otherwise creates a BufferedReader from the given
reader.reader
- the reader to wrap or return (not null)BufferedReader
for the given readerNullPointerException
- if the input parameter is nullCopyright © 2003–2020 The Apache Software Foundation. All rights reserved.