public final class IOUtils
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_BUFFER_SIZE
The default buffer size (4096) to use for
copyLarge(InputStream, OutputStream)
and
copyLarge(Reader, Writer) |
static int |
EOF
Represents the end-of-file (or stream).
|
Constructor and Description |
---|
IOUtils() |
Modifier and Type | Method and Description |
---|---|
static long |
copy(java.io.InputStream input,
java.io.OutputStream output,
int bufferSize)
Copies bytes from an
InputStream to an OutputStream using an internal buffer of the
given size. |
static int |
copy(java.io.Reader input,
java.io.Writer output)
Copies chars from a
Reader to a Writer . |
static long |
copyLarge(java.io.InputStream input,
java.io.OutputStream output)
Copies bytes from a large (over 2GB)
InputStream to an
OutputStream . |
static long |
copyLarge(java.io.InputStream input,
java.io.OutputStream output,
byte[] buffer)
Copies bytes from a large (over 2GB)
InputStream to an
OutputStream . |
static long |
copyLarge(java.io.InputStream input,
java.io.OutputStream output,
long inputOffset,
long length)
Copies some or all bytes from a large (over 2GB)
InputStream to an
OutputStream , optionally skipping input bytes. |
static long |
copyLarge(java.io.InputStream input,
java.io.OutputStream output,
long inputOffset,
long length,
byte[] buffer)
Copies some or all bytes from a large (over 2GB)
InputStream to an
OutputStream , optionally skipping input bytes. |
static long |
copyLarge(java.io.Reader input,
java.io.Writer output)
Copies chars from a large (over 2GB)
Reader to a Writer . |
static long |
copyLarge(java.io.Reader input,
java.io.Writer output,
char[] buffer)
Copies chars from a large (over 2GB)
Reader to a Writer . |
static long |
skip(java.io.InputStream input,
long toSkip)
Skips bytes from an input byte stream.
|
static void |
skipFully(java.io.InputStream input,
long toSkip)
Skips the requested number of bytes or fail if there are not enough left.
|
public static final int EOF
public static final int DEFAULT_BUFFER_SIZE
copyLarge(InputStream, OutputStream)
and
copyLarge(Reader, Writer)
public static long copy(java.io.InputStream input, java.io.OutputStream output, int bufferSize) throws java.io.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 outputjava.lang.NullPointerException
- if the input or output is nulljava.io.IOException
- if an I/O error occurspublic static long copyLarge(java.io.InputStream input, java.io.OutputStream output, long inputOffset, long length) throws java.io.IOException
InputStream
to an
OutputStream
, optionally skipping input bytes.
This method buffers the input internally, 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.
DEFAULT_BUFFER_SIZE
.input
- the InputStream
to read fromoutput
- the OutputStream
to write toinputOffset
- : number of bytes to skip from input before copying
-ve values are ignoredlength
- : number of bytes to copy. -ve means alljava.lang.NullPointerException
- if the input or output is nulljava.io.IOException
- if an I/O error occurspublic static long copyLarge(java.io.InputStream input, java.io.OutputStream output) throws java.io.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 tojava.lang.NullPointerException
- if the input or output is nulljava.io.IOException
- if an I/O error occurspublic static long copyLarge(java.io.InputStream input, java.io.OutputStream output, byte[] buffer) throws java.io.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 copyjava.lang.NullPointerException
- if the input or output is nulljava.io.IOException
- if an I/O error occurspublic static long copyLarge(java.io.InputStream input, java.io.OutputStream output, long inputOffset, long length, byte[] buffer) throws java.io.IOException
InputStream
to an
OutputStream
, 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.
input
- the InputStream
to read fromoutput
- the OutputStream
to 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 copyjava.lang.NullPointerException
- if the input or output is nulljava.io.IOException
- if an I/O error occurspublic static void skipFully(java.io.InputStream input, long toSkip) throws java.io.IOException
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.
input
- stream to skiptoSkip
- the number of bytes to skipjava.io.IOException
- if there is a problem reading the filejava.lang.IllegalArgumentException
- if toSkip is negativejava.io.EOFException
- if the number of bytes skipped was incorrectInputStream.skip(long)
public static long skip(java.io.InputStream input, long toSkip) throws java.io.IOException
InputStream
.
Note that the implementation uses InputStream.read(byte[], int, int)
rather
than delegating to InputStream.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.
input
- byte stream to skiptoSkip
- number of bytes to skip.java.io.IOException
- if there is a problem reading the filejava.lang.IllegalArgumentException
- if toSkip is negativeInputStream.skip(long)
,
IO-203 - Add skipFully() method for InputStreamspublic static int copy(java.io.Reader input, java.io.Writer output) throws java.io.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 tojava.lang.NullPointerException
- if the input or output is nulljava.io.IOException
- if an I/O error occurspublic static long copyLarge(java.io.Reader input, java.io.Writer output) throws java.io.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 tojava.lang.NullPointerException
- if the input or output is nulljava.io.IOException
- if an I/O error occurspublic static long copyLarge(java.io.Reader input, java.io.Writer output, char[] buffer) throws java.io.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 copyjava.lang.NullPointerException
- if the input or output is nulljava.io.IOException
- if an I/O error occurs