Top Description Inners Fields Constructors Methods
org.springframework.util

public abstract Class StreamUtils

extends Object
Class Inheritance
Imports
java.io.ByteArrayOutputStream, .FilterInputStream, .FilterOutputStream, .IOException, .InputStream, .InputStreamReader, .OutputStream, java.nio.charset.Charset, org.springframework.lang.Nullable

Simple utility methods for dealing with streams. The copy methods of this class are similar to those defined in FileCopyUtils except that all affected streams are left open when done. All copy methods use a block size of 8192 bytes.

Mainly for use within the framework, but also useful for application code.

Authors
Juergen Hoeller, Phillip Webb, Brian Clozel
Since
3.2.2
See Also
FileCopyUtils

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class

Field Summary

Modifier and TypeField and Description
public static final int
BUFFER_SIZE

The default buffer size used when copying bytes.

private static final byte[]

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static void
copy(byte[]
the byte array to copy from
in
,
OutputStream
the OutputStream to copy to
out
)

Copy the contents of the given byte array to the given OutputStream.

public static void
copy(String
the String to copy from
in
,
Charset
the Charset
charset
,
OutputStream
the OutputStream to copy to
out
)

Copy the contents of the given String to the given OutputStream.

public static int

Returns:

the number of bytes copied
copy
(InputStream
the InputStream to copy from
in
,
OutputStream
the OutputStream to copy to
out
)

Copy the contents of the given InputStream to the given OutputStream.

public static long

Returns:

the number of bytes copied
copyRange
(InputStream
the InputStream to copy from
in
,
OutputStream
the OutputStream to copy to
out
,
long
the position to start copying from
start
,
long
the position to end copying
end
)

Copy a range of content of the given InputStream to the given OutputStream.

public static byte[]

Returns:

the new byte array that has been copied to (possibly empty)
copyToByteArray
(InputStream
the stream to copy from (may be null or empty)
in
)

Copy the contents of the given InputStream into a new byte array.

public static String

Returns:

the String that has been copied to (possibly empty)
copyToString
(InputStream
the InputStream to copy from (may be null or empty)
in
,
Charset
the Charset to use to decode the bytes
charset
)

Copy the contents of the given InputStream into a String.

public static String

Returns:

the String that has been copied to (possibly empty)
copyToString
(ByteArrayOutputStream
the ByteArrayOutputStream to be copied into a String
baos
,
Charset
the Charset to use to decode the bytes
charset
)

Copy the contents of the given ByteArrayOutputStream into a String.

public static int

Returns:

the number of bytes read
drain
(InputStream
the InputStream to drain
in
)

Drain the remaining content of the given InputStream.

public static InputStream

Returns:

an InputStream which contains no bytes
emptyInput
()
Deprecated since 6.0. as of 6.0 in favor of InputStream#nullInputStream()

Return an efficient empty InputStream.

public static InputStream

Returns:

a version of the InputStream that ignores calls to close
nonClosing
(InputStream
the InputStream to decorate
in
)

Return a variant of the given InputStream where calling close() has no effect.

public static OutputStream

Returns:

a version of the OutputStream that ignores calls to close
nonClosing
(OutputStream
the OutputStream to decorate
out
)

Return a variant of the given OutputStream where calling close() has no effect.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

BUFFER_SIZEback to summary
public static final int BUFFER_SIZE

The default buffer size used when copying bytes.

EMPTY_CONTENTback to summary
private static final byte[] EMPTY_CONTENT

Constructor Detail

StreamUtilsback to summary
public StreamUtils()

Method Detail

copyback to summary
public static void copy(byte[] in, OutputStream out) throws IOException

Copy the contents of the given byte array to the given OutputStream.

Leaves the stream open when done.

Parameters
in:byte[]

the byte array to copy from

out:OutputStream

the OutputStream to copy to

Exceptions
IOException:
in case of I/O errors
copyback to summary
public static void copy(String in, Charset charset, OutputStream out) throws IOException

Copy the contents of the given String to the given OutputStream.

Leaves the stream open when done.

Parameters
in:String

the String to copy from

charset:Charset

the Charset

out:OutputStream

the OutputStream to copy to

Exceptions
IOException:
in case of I/O errors
copyback to summary
public static int copy(InputStream in, OutputStream out) throws IOException

Copy the contents of the given InputStream to the given OutputStream.

Leaves both streams open when done.

Parameters
in:InputStream

the InputStream to copy from

out:OutputStream

the OutputStream to copy to

Returns:int

the number of bytes copied

Exceptions
IOException:
in case of I/O errors
copyRangeback to summary
public static long copyRange(InputStream in, OutputStream out, long start, long end) throws IOException

Copy a range of content of the given InputStream to the given OutputStream.

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.

Parameters
in:InputStream

the InputStream to copy from

out:OutputStream

the OutputStream to copy to

start:long

the position to start copying from

end:long

the position to end copying

Returns:long

the number of bytes copied

Exceptions
IOException:
in case of I/O errors
Since
4.3
copyToByteArrayback to summary
public static byte[] copyToByteArray(InputStream in) throws IOException

Copy the contents of the given InputStream into a new byte array.

Leaves the stream open when done.

Parameters
in:InputStream

the stream to copy from (may be null or empty)

Returns:byte[]

the new byte array that has been copied to (possibly empty)

Exceptions
IOException:
in case of I/O errors
copyToStringback to summary
public static String copyToString(InputStream in, Charset charset) throws IOException

Copy the contents of the given InputStream into a String.

Leaves the stream open when done.

Parameters
in:InputStream

the InputStream to copy from (may be null or empty)

charset:Charset

the Charset to use to decode the bytes

Returns:String

the String that has been copied to (possibly empty)

Exceptions
IOException:
in case of I/O errors
copyToStringback to summary
public static String copyToString(ByteArrayOutputStream baos, Charset charset)

Copy the contents of the given ByteArrayOutputStream into a String.

This is a more effective equivalent of new String(baos.toByteArray(), charset).

Parameters
baos:ByteArrayOutputStream

the ByteArrayOutputStream to be copied into a String

charset:Charset

the Charset to use to decode the bytes

Returns:String

the String that has been copied to (possibly empty)

Since
5.2.6
drainback to summary
public static int drain(InputStream in) throws IOException

Drain the remaining content of the given InputStream.

Leaves the InputStream open when done.

Parameters
in:InputStream

the InputStream to drain

Returns:int

the number of bytes read

Exceptions
IOException:
in case of I/O errors
Since
4.3
emptyInputback to summary
public static InputStream emptyInput()

Deprecated

since 6.0.

as of 6.0 in favor of InputStream#nullInputStream()

Return an efficient empty InputStream.

Returns:InputStream

an InputStream which contains no bytes

Annotations
@Deprecated
since:6.0
Since
4.2.2
nonClosingback to summary
public static InputStream nonClosing(InputStream in)

Return a variant of the given InputStream where calling close() has no effect.

Parameters
in:InputStream

the InputStream to decorate

Returns:InputStream

a version of the InputStream that ignores calls to close

nonClosingback to summary
public static OutputStream nonClosing(OutputStream out)

Return a variant of the given OutputStream where calling close() has no effect.

Parameters
out:OutputStream

the OutputStream to decorate

Returns:OutputStream

a version of the OutputStream that ignores calls to close

org.springframework.util back to summary

private Class StreamUtils.NonClosingInputStream

extends FilterInputStream
Class Inheritance

Field Summary

Inherited from java.io.FilterInputStream:
in

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
close()

Overrides java.io.FilterInputStream.close.

Implements java.io.Closeable.close.

Closes this input stream and releases any system resources associated with the stream.

Inherited from java.io.FilterInputStream:
availablemarkmarkSupportedreadreadreadresetskip

Constructor Detail

NonClosingInputStreamback to summary
public NonClosingInputStream(InputStream in)

Method Detail

closeback to summary
public void close() throws IOException

Overrides java.io.FilterInputStream.close.

Implements java.io.Closeable.close.

Doc from java.io.FilterInputStream.close.

Closes this input stream and releases any system resources associated with the stream.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
org.springframework.util back to summary

private Class StreamUtils.NonClosingOutputStream

extends FilterOutputStream
Class Inheritance

Field Summary

Inherited from java.io.FilterOutputStream:
out

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
close()

Overrides java.io.FilterOutputStream.close.

Implements java.io.Closeable.close.

Closes this output stream and releases any system resources associated with the stream.

public void
write(byte[]
the data.
b
,
int
the start offset in the data.
off
,
int
the number of bytes to write.
let
)

Overrides java.io.FilterOutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream.

Inherited from java.io.FilterOutputStream:
flushwritewrite

Constructor Detail

NonClosingOutputStreamback to summary
public NonClosingOutputStream(OutputStream out)

Method Detail

closeback to summary
public void close() throws IOException

Overrides java.io.FilterOutputStream.close.

Implements java.io.Closeable.close.

Doc from java.io.FilterOutputStream.close.

Closes this output stream and releases any system resources associated with the stream.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
writeback to summary
public void write(byte[] b, int off, int let) throws IOException

Overrides java.io.FilterOutputStream.write.

Doc from java.io.FilterOutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream.

Parameters
b:byte[]

the data.

off:int

the start offset in the data.

let:int

the number of bytes to write.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.