Class IOUtils
- Author:
- Apache Directory Project
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidcloseQuietly(Closeable closeable) Closes aCloseableunconditionally.static voidcloseQuietly(Closeable... closeables) Closes aCloseableunconditionally.static voidcloseQuietly(InputStream input) Closes anInputStreamunconditionally.static intcopy(InputStream input, OutputStream output) Copies bytes from anInputStreamto anOutputStream.static longcopy(InputStream input, OutputStream output, int bufferSize) Copies bytes from anInputStreamto anOutputStreamusing an internal buffer of the given size.static voidcopy(InputStream input, Writer output, Charset inputEncoding) Copies bytes from anInputStreamto chars on aWriterusing the specified character encoding.static intCopies chars from aReaderto aWriter.static longcopyLarge(InputStream input, OutputStream output) Copies bytes from a large (over 2GB)InputStreamto anOutputStream.static longcopyLarge(InputStream input, OutputStream output, byte[] buffer) Copies bytes from a large (over 2GB)InputStreamto anOutputStream.static longCopies chars from a large (over 2GB)Readerto aWriter.static longCopies chars from a large (over 2GB)Readerto aWriter.readLines(InputStream input, Charset encoding) Gets the contents of anInputStreamas a list of Strings, one entry per line, using the specified character encoding.Gets the contents of aReaderas a list of Strings, one entry per line.static BufferedReadertoBufferedReader(Reader reader) Returns the given reader if it is aBufferedReader, otherwise creates a BufferedReader from the given reader.static byte[]toByteArray(InputStream input, int size) Gets the contents of anInputStreamas abyte[].static byte[]toByteArray(InputStream input, long size) Gets contents of anInputStreamas abyte[].static CharsetReturns a Charset for the named charset.static CharsetReturns the given Charset or the default Charset if the given Charset is null.static StringtoString(InputStream input, Charset encoding) Gets the contents of anInputStreamas a String using the specified character encoding.static voidwrite(String data, OutputStream output, Charset encoding) Writes chars from aStringto bytes on anOutputStreamusing the specified character encoding.
-
Method Details
-
closeQuietly
Closes anInputStreamunconditionally.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); }- Parameters:
input- the InputStream to close, may be null or already closed
-
closeQuietly
Closes aCloseableunconditionally.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); }- Parameters:
closeables- the objects to close, may be null or already closed- Since:
- 2.5
-
closeQuietly
Closes aCloseableunconditionally.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); }- Parameters:
closeable- the objects to close, may be null or already closed- Since:
- 2.0
-
toString
Gets the contents of anInputStreamas a String using the specified character encoding.This method buffers the input internally, so there is no need to use a
BufferedInputStream.- Parameters:
input- theInputStreamto read fromencoding- the encoding to use, null means platform default- Returns:
- the requested String
- Throws:
NullPointerException- if the input is nullIOException- if an I/O error occurs- Since:
- 2.3
-
toCharset
-
toCharset
-
copy
Copies bytes from anInputStreamto chars on aWriterusing the specified character encoding.This method buffers the input internally, so there is no need to use a
BufferedInputStream.This method uses
InputStreamReader.- Parameters:
input- theInputStreamto read fromoutput- theWriterto write toinputEncoding- the encoding to use for the input stream, null means platform default- Throws:
NullPointerException- if the input or output is nullIOException- if an I/O error occurs- Since:
- 2.3
-
copy
Copies chars from aReaderto aWriter.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
-1after the copy has completed since the correct number of chars cannot be returned as an int. For large streams use thecopyLarge(Reader, Writer)method.- Parameters:
input- theReaderto read fromoutput- theWriterto write to- Returns:
- the number of characters copied, or -1 if > Integer.MAX_VALUE
- Throws:
NullPointerException- if the input or output is nullIOException- if an I/O error occurs- Since:
- 1.1
-
copy
Copies bytes from anInputStreamto anOutputStream.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
-1after the copy has completed since the correct number of bytes cannot be returned as an int. For large streams use thecopyLarge(InputStream, OutputStream)method.- Parameters:
input- theInputStreamto read fromoutput- theOutputStreamto write to- Returns:
- the number of bytes copied, or -1 if > Integer.MAX_VALUE
- Throws:
NullPointerException- if the input or output is nullIOException- if an I/O error occurs- Since:
- 1.1
-
copy
Copies bytes from anInputStreamto anOutputStreamusing an internal buffer of the given size.This method buffers the input internally, so there is no need to use a
BufferedInputStream.- Parameters:
input- theInputStreamto read fromoutput- theOutputStreamto write tobufferSize- the bufferSize used to copy from the input to the output- Returns:
- the number of bytes copied
- Throws:
NullPointerException- if the input or output is nullIOException- if an I/O error occurs- Since:
- 2.5
-
copyLarge
Copies chars from a large (over 2GB)Readerto aWriter.This method buffers the input internally, so there is no need to use a
BufferedReader.The buffer size is given by #DEFAULT_BUFFER_SIZE.
- Parameters:
input- theReaderto read fromoutput- theWriterto write to- Returns:
- the number of characters copied
- Throws:
NullPointerException- if the input or output is nullIOException- if an I/O error occurs- Since:
- 1.3
-
copyLarge
Copies bytes from a large (over 2GB)InputStreamto anOutputStream.This method buffers the input internally, so there is no need to use a
BufferedInputStream.The buffer size is given by #DEFAULT_BUFFER_SIZE.
- Parameters:
input- theInputStreamto read fromoutput- theOutputStreamto write to- Returns:
- the number of bytes copied
- Throws:
NullPointerException- if the input or output is nullIOException- if an I/O error occurs- Since:
- 1.3
-
copyLarge
public static long copyLarge(InputStream input, OutputStream output, byte[] buffer) throws IOException Copies bytes from a large (over 2GB)InputStreamto anOutputStream.This method uses the provided buffer, so there is no need to use a
BufferedInputStream.- Parameters:
input- theInputStreamto read fromoutput- theOutputStreamto write tobuffer- the buffer to use for the copy- Returns:
- the number of bytes copied
- Throws:
NullPointerException- if the input or output is nullIOException- if an I/O error occurs- Since:
- 2.2
-
copyLarge
Copies chars from a large (over 2GB)Readerto aWriter.This method uses the provided buffer, so there is no need to use a
BufferedReader.- Parameters:
input- theReaderto read fromoutput- theWriterto write tobuffer- the buffer to be used for the copy- Returns:
- the number of characters copied
- Throws:
NullPointerException- if the input or output is nullIOException- if an I/O error occurs- Since:
- 2.2
-
write
Writes chars from aStringto bytes on anOutputStreamusing the specified character encoding.This method uses
String.getBytes(String).- Parameters:
data- theStringto write, null ignoredoutput- theOutputStreamto write toencoding- the encoding to use, null means platform default- Throws:
NullPointerException- if output is nullIOException- if an I/O error occurs- Since:
- 2.3
-
toByteArray
Gets the contents of anInputStreamas abyte[]. Use this method instead oftoByteArray(InputStream)whenInputStreamsize is known- Parameters:
input- theInputStreamto read fromsize- the size ofInputStream- Returns:
- the requested byte array
- Throws:
IOException- if an I/O error occurs orInputStreamsize differ from parameter sizeIllegalArgumentException- if size is less than zero- Since:
- 2.1
-
toByteArray
Gets contents of anInputStreamas abyte[]. Use this method instead oftoByteArray(InputStream)whenInputStreamsize is known. NOTE: the method checks that the length can safely be cast to an int without truncation before usingtoByteArray(java.io.InputStream, int)to read into the byte array. (Arrays can have no more than Integer.MAX_VALUE entries anyway)- Parameters:
input- theInputStreamto read fromsize- the size ofInputStream- Returns:
- the requested byte array
- Throws:
IOException- if an I/O error occurs orInputStreamsize differ from parameter sizeIllegalArgumentException- if size is less than zero or size is greater than Integer.MAX_VALUE- Since:
- 2.1
- See Also:
-
readLines
Gets the contents of anInputStreamas 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.- Parameters:
input- theInputStreamto read from, not nullencoding- the encoding to use, null means platform default- Returns:
- the list of Strings, never null
- Throws:
NullPointerException- if the input is nullIOException- if an I/O error occurs- Since:
- 2.3
-
readLines
Gets the contents of aReaderas a list of Strings, one entry per line.This method buffers the input internally, so there is no need to use a
BufferedReader.- Parameters:
input- theReaderto read from, not null- Returns:
- the list of Strings, never null
- Throws:
NullPointerException- if the input is nullIOException- if an I/O error occurs- Since:
- 1.1
-
toBufferedReader
Returns the given reader if it is aBufferedReader, otherwise creates a BufferedReader from the given reader.- Parameters:
reader- the reader to wrap or return (not null)- Returns:
- the given reader or a new
BufferedReaderfor the given reader - Throws:
NullPointerException- if the input parameter is null- Since:
- 2.2
-