InputStream and
OutputStreams. The content is inspired / copied from
org.apache.commons.io.IOUtils- Author:
- Oliver Wolff
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default buffer size (4096) to use in copy methods.static final intRepresents the end-of-file (or stream). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleancontentEquals(InputStream input1, InputStream input2) Compares the contents of two Streams to determine if they are equal or not.static booleancontentEquals(Reader input1, Reader input2) Compares the contents of two Readers to determine if they are equal or not.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, String inputEncoding) Copies bytes from anInputStreamto chars on aWriterusing the specified character encoding.static voidcopy(InputStream input, Writer output, Charset inputEncoding) Copies bytes from anInputStreamto chars on aWriterusing the specified character encoding.static voidcopy(Reader input, OutputStream output, String outputEncoding) Copies chars from aReaderto bytes on anOutputStreamusing the specified character encoding, and calling flush.static voidcopy(Reader input, OutputStream output, Charset outputEncoding) Copies chars from aReaderto bytes on anOutputStreamusing the specified character encoding, and calling flush.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 longcopyLarge(InputStream input, OutputStream output, long inputOffset, long length) Copies some or all bytes from a large (over 2GB)InputStreamto anOutputStream, optionally skipping input bytes.static longcopyLarge(InputStream input, OutputStream output, long inputOffset, long length, byte[] buffer) Copies some or all bytes from a large (over 2GB)InputStreamto anOutputStream, optionally skipping input bytes.static longCopies chars from a large (over 2GB)Readerto aWriter.static longCopies chars from a large (over 2GB)Readerto aWriter.static longCopies some or all chars from a large (over 2GB)InputStreamto anOutputStream, optionally skipping input chars.static longCopies some or all chars from a large (over 2GB)InputStreamto anOutputStream, optionally skipping input chars.static longskip(InputStream input, long toSkip) Skips bytes from an input byte stream.static longSkips characters from an input character stream.static voidskipFully(InputStream input, long toSkip) Skips the requested number of bytes or fail if there are not enough left.static voidSkips the requested number of characters or fail if there are not enough left.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) Gets the 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 InputStreamtoInputStream(String input) static StringtoString(InputStream input) Gets the contents of anInputStreamas a String using the specified character encoding.static StringtoString(InputStream input, Charset encoding) Gets the contents of anInputStreamas a String using the specified character encoding.
-
Field Details
-
EOF
Represents the end-of-file (or stream).- See Also:
-
DEFAULT_BUFFER_SIZE
The default buffer size (4096) to use in copy methods.- See Also:
-
-
Constructor Details
-
IOStreams
public IOStreams()
-
-
Method Details
-
contentEquals
Compares the contents of two Streams to determine if they are equal or not.This method buffers the input internally using
BufferedInputStreamif they are not already buffered.- Parameters:
input1- the first streaminput2- the second stream- Returns:
- true if the content of the streams are equal or they both don't exist, false otherwise
- Throws:
NullPointerException- if either input is nullIOException- if an I/O error occurs
-
contentEquals
Compares the contents of two Readers to determine if they are equal or not.This method buffers the input internally using
BufferedReaderif they are not already buffered.- Parameters:
input1- the first readerinput2- the second reader- Returns:
- true if the content of the readers are equal or they both don't exist, false otherwise
- Throws:
NullPointerException- if either input is nullIOException- if an I/O error occurs
-
toInputStream
- Parameters:
input- to be wrapped, may be null or empty- Returns:
- the created
InputStream
-
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 from, using UTF-8 encoding.- Returns:
- the requested String
- Throws:
NullPointerException- if the input is nullIOException- if an I/O error occurs
-
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:
IllegalArgumentException- if the input is nullIOException- if an I/O error occurs
-
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
-
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
-
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
-
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
-
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
-
copyLarge
public static long copyLarge(InputStream input, OutputStream output, long inputOffset, long length) throws IOException Copies some or all bytes from a large (over 2GB)InputStreamto anOutputStream, optionally skipping input bytes.This method buffers the input internally, so there is no need to use a
BufferedInputStream.Note that the implementation uses
The buffer size is given byskip(InputStream, long). This means that the method may be considerably less efficient than using the actual skip implementation, this is done to guarantee that the correct number of characters are skipped.DEFAULT_BUFFER_SIZE.- Parameters:
input- theInputStreamto read fromoutput- theOutputStreamto write toinputOffset- number of bytes to skip from input before copying -ve values are ignoredlength- number of bytes to copy. -ve means all- Returns:
- the number of bytes copied
- Throws:
NullPointerException- if the input or output is nullIOException- if an I/O error occurs
-
copyLarge
public static long copyLarge(InputStream input, OutputStream output, long inputOffset, long length, byte[] buffer) throws IOException Copies some or all bytes from a large (over 2GB)InputStreamto anOutputStream, optionally skipping input bytes.This method uses the provided buffer, so there is no need to use a
BufferedInputStream.Note that the implementation uses
skip(InputStream, long). This means that the method may be considerably less efficient than using the actual skip implementation, this is done to guarantee that the correct number of characters are skipped.- Parameters:
input- theInputStreamto read fromoutput- theOutputStreamto write toinputOffset- number of bytes to skip from input before copying -ve values are ignoredlength- number of bytes to copy. -ve means allbuffer- 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
-
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
-
toCharset
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
-
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.Character encoding names can be found at IANA.
This method uses
InputStreamReader.- Parameters:
input- theInputStreamto read fromoutput- theWriterto write toinputEncoding- the encoding to use for the InputStream, null means platform default- Throws:
NullPointerException- if the input or output is nullIOException- if an I/O error occursUnsupportedCharsetException- thrown instead of.UnsupportedEncodingExceptionin version 2.2 if the encoding is not supported.
-
toCharset
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
- Throws:
UnsupportedCharsetException- If the named charset is unavailable
-
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
-
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
-
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
-
copyLarge
public static long copyLarge(Reader input, Writer output, long inputOffset, long length) throws IOException Copies some or all chars from a large (over 2GB)InputStreamto anOutputStream, optionally skipping input chars.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 toinputOffset- number of chars to skip from input before copying -ve values are ignoredlength- number of chars to copy. -ve means all- Returns:
- the number of chars copied
- Throws:
NullPointerException- if the input or output is nullIOException- if an I/O error occurs
-
copyLarge
public static long copyLarge(Reader input, Writer output, long inputOffset, long length, char[] buffer) throws IOException Copies some or all chars from a large (over 2GB)InputStreamto anOutputStream, optionally skipping input chars.This method uses the provided buffer, so there is no need to use a
BufferedReader.- Parameters:
input- theReaderto read fromoutput- theWriterto write toinputOffset- number of chars to skip from input before copying -ve values are ignoredlength- number of chars to copy. -ve means allbuffer- the buffer to be used for the copy- Returns:
- the number of chars copied
- Throws:
NullPointerException- if the input or output is nullIOException- if an I/O error occurs
-
copy
public static void copy(Reader input, OutputStream output, Charset outputEncoding) throws IOException Copies chars from aReaderto bytes on anOutputStreamusing the specified character encoding, and calling flush.This method buffers the input internally, so there is no need to use a
BufferedReader.Due to the implementation of OutputStreamWriter, this method performs a flush.
This method uses
OutputStreamWriter.- Parameters:
input- theReaderto read fromoutput- theOutputStreamto write tooutputEncoding- the encoding to use for the OutputStream, null means platform default- Throws:
NullPointerException- if the input or output is nullIOException- if an I/O error occurs
-
copy
public static void copy(Reader input, OutputStream output, String outputEncoding) throws IOException Copies chars from aReaderto bytes on anOutputStreamusing the specified character encoding, and calling flush.This method buffers the input internally, so there is no need to use a
BufferedReader.Character encoding names can be found at IANA.
Due to the implementation of OutputStreamWriter, this method performs a flush.
This method uses
OutputStreamWriter.- Parameters:
input- theReaderto read fromoutput- theOutputStreamto write tooutputEncoding- the encoding to use for the OutputStream, null means platform default- Throws:
NullPointerException- if the input or output is nullIOException- if an I/O error occursUnsupportedCharsetException- thrown instead of.UnsupportedEncodingExceptionin version 2.2 if the encoding is not supported.
-
skip
Skips bytes from an input byte stream. This implementation guarantees that it will read as many bytes as possible before giving up; this may not always be the case for skip() implementations in subclasses ofInputStream.Note that the implementation uses
InputStream.read(byte[], int, int)rather than delegating toInputStream.skip(long). This means that the method may be considerably less efficient than using the actual skip implementation, this is done to guarantee that the correct number of bytes are skipped.- Parameters:
input- byte stream to skiptoSkip- number of bytes to skip.- Returns:
- number of bytes actually skipped.
- Throws:
IOException- if there is a problem reading the fileIllegalArgumentException- if toSkip is negative- See Also:
-
skip
Skips characters from an input character stream. This implementation guarantees that it will read as many characters as possible before giving up; this may not always be the case for skip() implementations in subclasses ofReader.Note that the implementation uses
Reader.read(char[], int, int)rather than delegating toReader.skip(long). This means that the method may be considerably less efficient than using the actual skip implementation, this is done to guarantee that the correct number of characters are skipped.- Parameters:
input- character stream to skiptoSkip- number of characters to skip.- Returns:
- number of characters actually skipped.
- Throws:
IOException- if there is a problem reading the fileIllegalArgumentException- if toSkip is negative- See Also:
-
skipFully
Skips the requested number of bytes or fail if there are not enough left.This allows for the possibility that
InputStream.skip(long)may not skip as many bytes as requested (most likely because of reaching EOF).Note that the implementation uses
skip(InputStream, long). This means that the method may be considerably less efficient than using the actual skip implementation, this is done to guarantee that the correct number of characters are skipped.- Parameters:
input- stream to skiptoSkip- the number of bytes to skip- Throws:
IOException- if there is a problem reading the fileIllegalArgumentException- if toSkip is negativeEOFException- if the number of bytes skipped was incorrect- See Also:
-
skipFully
Skips the requested number of characters or fail if there are not enough left.This allows for the possibility that
Reader.skip(long)may not skip as many characters as requested (most likely because of reaching EOF).Note that the implementation uses
skip(Reader, long). This means that the method may be considerably less efficient than using the actual skip implementation, this is done to guarantee that the correct number of characters are skipped.- Parameters:
input- stream to skiptoSkip- the number of characters to skip- Throws:
IOException- if there is a problem reading the fileIllegalArgumentException- if toSkip is negativeEOFException- if the number of characters skipped was incorrect- See Also:
-
toByteArray
Gets the contents of anInputStreamas abyte[].This method buffers the input internally, so there is no need to use a
BufferedInputStream.- Parameters:
input- theInputStreamto read from- Returns:
- the requested byte array
- Throws:
NullPointerException- if the input is nullIOException- if an I/O error occurs
-