public abstract class StreamUtils extends Object
FileCopyUtils
except that all affected streams are
left open when done. All copy methods use a block size of 4096 bytes.
Mainly for use within the framework, but also useful for application code.
FileCopyUtils
Modifier and Type | Field and Description |
---|---|
static int |
BUFFER_SIZE
The default buffer size used when copying bytes.
|
Constructor and Description |
---|
StreamUtils() |
Modifier and Type | Method and Description |
---|---|
static void |
copy(byte[] in,
OutputStream out)
Copy the contents of the given byte array to the given OutputStream.
|
static int |
copy(InputStream in,
OutputStream out)
Copy the contents of the given InputStream to the given OutputStream.
|
static void |
copy(String in,
Charset charset,
OutputStream out)
Copy the contents of the given String to the given output OutputStream.
|
static long |
copyRange(InputStream in,
OutputStream out,
long start,
long end)
Copy a range of content of the given InputStream to the given OutputStream.
|
static byte[] |
copyToByteArray(InputStream in)
Copy the contents of the given InputStream into a new byte array.
|
static String |
copyToString(ByteArrayOutputStream baos,
Charset charset)
Copy the contents of the given
ByteArrayOutputStream into a String . |
static String |
copyToString(InputStream in,
Charset charset)
Copy the contents of the given InputStream into a String.
|
static int |
drain(InputStream in)
Drain the remaining content of the given InputStream.
|
static InputStream |
emptyInput()
Return an efficient empty
InputStream . |
static InputStream |
nonClosing(InputStream in)
Return a variant of the given
InputStream where calling
close() has no effect. |
static OutputStream |
nonClosing(OutputStream out)
Return a variant of the given
OutputStream where calling
close() has no effect. |
public static final int BUFFER_SIZE
public static byte[] copyToByteArray(@Nullable InputStream in) throws IOException
Leaves the stream open when done.
in
- the stream to copy from (may be null
or empty)IOException
- in case of I/O errorspublic static String copyToString(@Nullable InputStream in, Charset charset) throws IOException
Leaves the stream open when done.
in
- the InputStream to copy from (may be null
or empty)charset
- the Charset
to use to decode the bytesIOException
- in case of I/O errorspublic static String copyToString(ByteArrayOutputStream baos, Charset charset)
ByteArrayOutputStream
into a String
.
This is a more effective equivalent of new String(baos.toByteArray(), charset)
.
As long as the charset
is already available at the point of
invocation, no exception is expected to be thrown by this method.
baos
- the ByteArrayOutputStream
to be copied into a Stringcharset
- the Charset
to use to decode the bytespublic static void copy(byte[] in, OutputStream out) throws IOException
Leaves the stream open when done.
in
- the byte array to copy fromout
- the OutputStream to copy toIOException
- in case of I/O errorspublic static void copy(String in, Charset charset, OutputStream out) throws IOException
Leaves the stream open when done.
in
- the String to copy fromcharset
- the Charsetout
- the OutputStream to copy toIOException
- in case of I/O errorspublic static int copy(InputStream in, OutputStream out) throws IOException
Leaves both streams open when done.
in
- the InputStream to copy fromout
- the OutputStream to copy toIOException
- in case of I/O errorspublic static long copyRange(InputStream in, OutputStream out, long start, long end) throws IOException
If the specified range exceeds the length of the InputStream, this copies up to the end of the stream and returns the actual number of copied bytes.
Leaves both streams open when done.
in
- the InputStream to copy fromout
- the OutputStream to copy tostart
- the position to start copying fromend
- the position to end copyingIOException
- in case of I/O errorspublic static int drain(InputStream in) throws IOException
Leaves the InputStream open when done.
in
- the InputStream to drainIOException
- in case of I/O errorspublic static InputStream emptyInput()
InputStream
.ByteArrayInputStream
based on an empty byte arraypublic static InputStream nonClosing(InputStream in)
InputStream
where calling
close()
has no effect.in
- the InputStream to decoratepublic static OutputStream nonClosing(OutputStream out)
OutputStream
where calling
close()
has no effect.out
- the OutputStream to decorate