Class IOUtils
- java.lang.Object
-
- org.apache.directory.api.util.IOUtils
-
public final class IOUtils extends Object
This code comes from Apache commons.io library. Origin of code: Excalibur, Alexandria, Tomcat, Commons-Utils.- Author:
- Apache Directory Project
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
closeQuietly(Closeable closeable)
Closes aCloseable
unconditionally.static void
closeQuietly(Closeable... closeables)
Closes aCloseable
unconditionally.static void
closeQuietly(InputStream input)
Closes anInputStream
unconditionally.static int
copy(InputStream input, OutputStream output)
Copies bytes from anInputStream
to anOutputStream
.static long
copy(InputStream input, OutputStream output, int bufferSize)
Copies bytes from anInputStream
to anOutputStream
using an internal buffer of the given size.static void
copy(InputStream input, Writer output, Charset inputEncoding)
Copies bytes from anInputStream
to chars on aWriter
using the specified character encoding.static int
copy(Reader input, Writer output)
Copies chars from aReader
to aWriter
.static long
copyLarge(InputStream input, OutputStream output)
Copies bytes from a large (over 2GB)InputStream
to anOutputStream
.static long
copyLarge(InputStream input, OutputStream output, byte[] buffer)
Copies bytes from a large (over 2GB)InputStream
to anOutputStream
.static long
copyLarge(Reader input, Writer output)
Copies chars from a large (over 2GB)Reader
to aWriter
.static long
copyLarge(Reader input, Writer output, char[] buffer)
Copies chars from a large (over 2GB)Reader
to aWriter
.static List<String>
readLines(InputStream input, Charset encoding)
Gets the contents of anInputStream
as a list of Strings, one entry per line, using the specified character encoding.static List<String>
readLines(Reader input)
Gets the contents of aReader
as a list of Strings, one entry per line.static BufferedReader
toBufferedReader(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 anInputStream
as abyte[]
.static byte[]
toByteArray(InputStream input, long size)
Gets contents of anInputStream
as abyte[]
.static Charset
toCharset(String charset)
Returns a Charset for the named charset.static Charset
toCharset(Charset charset)
Returns the given Charset or the default Charset if the given Charset is null.static String
toString(InputStream input, Charset encoding)
Gets the contents of anInputStream
as a String using the specified character encoding.static void
write(String data, OutputStream output, Charset encoding)
Writes chars from aString
to bytes on anOutputStream
using the specified character encoding.
-
-
-
Method Detail
-
closeQuietly
public static void closeQuietly(InputStream input)
Closes anInputStream
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); }
- Parameters:
input
- the InputStream to close, may be null or already closed
-
closeQuietly
public static void closeQuietly(Closeable... closeables)
Closes aCloseable
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); }
- Parameters:
closeables
- the objects to close, may be null or already closed- Since:
- 2.5
-
closeQuietly
public static void closeQuietly(Closeable closeable)
Closes aCloseable
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); }
- Parameters:
closeable
- the objects to close, may be null or already closed- Since:
- 2.0
-
toString
public static String toString(InputStream input, Charset encoding) throws IOException
Gets the contents of anInputStream
as a String using the specified character encoding.This method buffers the input internally, so there is no need to use a
BufferedInputStream
.- Parameters:
input
- theInputStream
to 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
public static Charset toCharset(Charset charset)
Returns the given Charset or the default Charset if the given Charset is null.- Parameters:
charset
- A charset or null.- Returns:
- the given Charset or the default Charset if the given Charset is null
-
toCharset
public static Charset toCharset(String charset)
Returns a Charset for the named charset. If the name is null, return the default Charset.- Parameters:
charset
- The name of the requested charset, may be null.- Returns:
- a Charset for the named charset
-
copy
public static void copy(InputStream input, Writer output, Charset inputEncoding) throws IOException
Copies bytes from anInputStream
to chars on aWriter
using 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
- theInputStream
to read fromoutput
- theWriter
to 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
public static int copy(Reader input, Writer output) throws IOException
Copies chars from aReader
to 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
-1
after 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
- theReader
to read fromoutput
- theWriter
to 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
public static int copy(InputStream input, OutputStream output) throws IOException
Copies bytes from anInputStream
to 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
-1
after 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
- theInputStream
to read fromoutput
- theOutputStream
to 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
public static long copy(InputStream input, OutputStream output, int bufferSize) throws IOException
Copies bytes from anInputStream
to anOutputStream
using an internal buffer of the given size.This method buffers the input internally, so there is no need to use a
BufferedInputStream
.- Parameters:
input
- theInputStream
to read fromoutput
- theOutputStream
to 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
public static long copyLarge(Reader input, Writer output) throws IOException
Copies chars from a large (over 2GB)Reader
to 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
- theReader
to read fromoutput
- theWriter
to 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
public static long copyLarge(InputStream input, OutputStream output) throws IOException
Copies bytes from a large (over 2GB)InputStream
to 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
- theInputStream
to read fromoutput
- theOutputStream
to 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)InputStream
to anOutputStream
.This method uses the provided buffer, so there is no need to use a
BufferedInputStream
.- Parameters:
input
- theInputStream
to read fromoutput
- theOutputStream
to 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
public static long copyLarge(Reader input, Writer output, char[] buffer) throws IOException
Copies chars from a large (over 2GB)Reader
to aWriter
.This method uses the provided buffer, so there is no need to use a
BufferedReader
.- Parameters:
input
- theReader
to read fromoutput
- theWriter
to 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
public static void write(String data, OutputStream output, Charset encoding) throws IOException
Writes chars from aString
to bytes on anOutputStream
using the specified character encoding.This method uses
String.getBytes(String)
.- Parameters:
data
- theString
to write, null ignoredoutput
- theOutputStream
to 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
public static byte[] toByteArray(InputStream input, int size) throws IOException
Gets the contents of anInputStream
as abyte[]
. Use this method instead oftoByteArray(InputStream)
whenInputStream
size is known- Parameters:
input
- theInputStream
to read fromsize
- the size ofInputStream
- Returns:
- the requested byte array
- Throws:
IOException
- if an I/O error occurs orInputStream
size differ from parameter sizeIllegalArgumentException
- if size is less than zero- Since:
- 2.1
-
toByteArray
public static byte[] toByteArray(InputStream input, long size) throws IOException
Gets contents of anInputStream
as abyte[]
. Use this method instead oftoByteArray(InputStream)
whenInputStream
size 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
- theInputStream
to read fromsize
- the size ofInputStream
- Returns:
- the requested byte array
- Throws:
IOException
- if an I/O error occurs orInputStream
size differ from parameter sizeIllegalArgumentException
- if size is less than zero or size is greater than Integer.MAX_VALUE- Since:
- 2.1
- See Also:
toByteArray(java.io.InputStream, int)
-
readLines
public static List<String> readLines(InputStream input, Charset encoding) throws IOException
Gets the contents of anInputStream
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
.- Parameters:
input
- theInputStream
to 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
public static List<String> readLines(Reader input) throws IOException
Gets the contents of aReader
as 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
- theReader
to 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
public static BufferedReader toBufferedReader(Reader reader)
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
BufferedReader
for the given reader - Throws:
NullPointerException
- if the input parameter is null- Since:
- 2.2
-
-