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 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 intcopy(Reader input, Writer output)Copies 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 longcopyLarge(Reader input, Writer output)Copies chars from a large (over 2GB)Readerto aWriter.static longcopyLarge(Reader input, Writer output, char[] buffer)Copies chars from a large (over 2GB)Readerto aWriter.static List<String>readLines(InputStream input, Charset encoding)Gets the contents of anInputStreamas a list of Strings, one entry per line, using the specified character encoding.static List<String>readLines(Reader input)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 CharsettoCharset(String charset)Returns a Charset for the named charset.static CharsettoCharset(Charset charset)Returns 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 Detail
-
closeQuietly
public static void closeQuietly(InputStream input)
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
public static void closeQuietly(Closeable... closeables)
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
public static void closeQuietly(Closeable closeable)
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
public static String toString(InputStream input, Charset encoding) throws IOException
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
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 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
public static int copy(Reader input, Writer output) throws IOException
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
public static int copy(InputStream input, OutputStream output) throws IOException
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
public static long copy(InputStream input, OutputStream output, int bufferSize) throws IOException
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
public static long copyLarge(Reader input, Writer output) throws IOException
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
public static long copyLarge(InputStream input, OutputStream output) throws IOException
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
public static long copyLarge(Reader input, Writer output, char[] buffer) throws IOException
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
public static void write(String data, OutputStream output, Charset encoding) throws IOException
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
public static byte[] toByteArray(InputStream input, int size) throws IOException
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
public static byte[] toByteArray(InputStream input, long size) throws IOException
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:
toByteArray(java.io.InputStream, int)
-
readLines
public static List<String> readLines(InputStream input, Charset encoding) throws IOException
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
public static List<String> readLines(Reader input) throws IOException
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
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
BufferedReaderfor the given reader - Throws:
NullPointerException- if the input parameter is null- Since:
- 2.2
-
-