public class IOUtils extends Object
This class provides static utility methods for input/output operations.
It was taken from commons-io:commons-io:2.6 and stripped down to the functionality actually used by this library.
| Modifier and Type | Field and Description |
|---|---|
static int |
EOF
Represents the end-of-file (or stream).
|
| Modifier and Type | Method and Description |
|---|---|
static int |
copy(InputStream input,
OutputStream output)
Copies bytes from an
InputStream to an
OutputStream. |
static long |
copy(InputStream input,
OutputStream output,
int bufferSize)
Copies bytes from an
InputStream to an OutputStream using an internal buffer of the
given size. |
static long |
copyLarge(InputStream input,
OutputStream output)
Copies bytes from a large (over 2GB)
InputStream to an
OutputStream. |
static long |
copyLarge(InputStream input,
OutputStream output,
byte[] buffer)
Copies bytes from a large (over 2GB)
InputStream to an
OutputStream. |
static long |
copyLarge(InputStream input,
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(InputStream input,
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 |
skip(InputStream input,
long toSkip)
Skips bytes from an input byte stream.
|
static void |
skipFully(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 int copy(InputStream input, OutputStream output) throws IOException
InputStream to an
OutputStream.
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 the copyLarge(InputStream, OutputStream) method.
input - the InputStream to read fromoutput - the OutputStream to write toNullPointerException - if the input or output is nullIOException - if an I/O error occurspublic static long copy(InputStream input, OutputStream output, int bufferSize) throws 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 outputNullPointerException - if the input or output is nullIOException - if an I/O error occurspublic static long copyLarge(InputStream input, OutputStream output) throws 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 toNullPointerException - if the input or output is nullIOException - if an I/O error occurspublic static long copyLarge(InputStream input, OutputStream output, byte[] buffer) throws 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 copyNullPointerException - if the input or output is nullIOException - if an I/O error occurspublic static long copyLarge(InputStream input, OutputStream output, long inputOffset, long length) throws 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 allNullPointerException - if the input or output is nullIOException - if an I/O error occurspublic static long copyLarge(InputStream input, OutputStream output, long inputOffset, long length, byte[] buffer) throws 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 copyNullPointerException - if the input or output is nullIOException - if an I/O error occurspublic static long skip(InputStream input, long toSkip) throws 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.IOException - if there is a problem reading the fileIllegalArgumentException - if toSkip is negativeInputStream.skip(long),
IO-203 - Add skipFully() method for InputStreamspublic static void skipFully(InputStream input, long toSkip) throws 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 skipIOException - if there is a problem reading the fileIllegalArgumentException - if toSkip is negativeEOFException - if the number of bytes skipped was incorrectInputStream.skip(long)Copyright © 2013–2019 mklinger GmbH. All rights reserved.