Package org.springframework.util
Class StreamUtils
java.lang.Object
org.springframework.util.StreamUtils
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.
- Since:
- 3.2.2
- Author:
- Juergen Hoeller, Phillip Webb, Brian Clozel
- See Also:
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final intThe default buffer size used when copying bytes.
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic voidcopy(byte[] in, OutputStream out) Copy the contents of the given byte array to the given OutputStream.static intcopy(InputStream in, OutputStream out) Copy the contents of the given InputStream to the given OutputStream.static voidcopy(String in, Charset charset, OutputStream out) Copy the contents of the given String to the given OutputStream.static longcopyRange(InputStream in, OutputStream out, long start, long end) Copy a range of content of the given InputStream to the given OutputStream.static byte[]Copy the contents of the given InputStream into a new byte array.static StringcopyToString(ByteArrayOutputStream baos, Charset charset) Copy the contents of the givenByteArrayOutputStreaminto aString.static StringcopyToString(InputStream in, Charset charset) Copy the contents of the given InputStream into a String.static intdrain(InputStream in) Drain the remaining content of the givenInputStream.static InputStreamDeprecated.static InputStreamReturn a variant of the givenInputStreamwhere callingclose()has no effect.static OutputStreamnonClosing(OutputStream out) Return a variant of the givenOutputStreamwhere callingclose()has no effect.
- 
Field Details- 
BUFFER_SIZEpublic static final int BUFFER_SIZEThe default buffer size used when copying bytes.- See Also:
 
 
- 
- 
Constructor Details- 
StreamUtilspublic StreamUtils()
 
- 
- 
Method Details- 
copyToByteArrayCopy the contents of the given InputStream into a new byte array.Leaves the stream open when done. - Parameters:
- in- the stream to copy from (may be- nullor empty)
- Returns:
- the new byte array that has been copied to (possibly empty)
- Throws:
- IOException- in case of I/O errors
 
- 
copyToStringCopy the contents of the given InputStream into a String.Leaves the stream open when done. - Parameters:
- in- the InputStream to copy from (may be- nullor empty)
- charset- the- Charsetto use to decode the bytes
- Returns:
- the String that has been copied to (possibly empty)
- Throws:
- IOException- in case of I/O errors
 
- 
copyToStringCopy the contents of the givenByteArrayOutputStreaminto aString.This is a more effective equivalent of new String(baos.toByteArray(), charset).- Parameters:
- baos- the- ByteArrayOutputStreamto be copied into a String
- charset- the- Charsetto use to decode the bytes
- Returns:
- the String that has been copied to (possibly empty)
- Since:
- 5.2.6
 
- 
copyCopy the contents of the given byte array to the given OutputStream.Leaves the stream open when done. - Parameters:
- in- the byte array to copy from
- out- the OutputStream to copy to
- Throws:
- IOException- in case of I/O errors
 
- 
copyCopy the contents of the given String to the given OutputStream.Leaves the stream open when done. - Parameters:
- in- the String to copy from
- charset- the Charset
- out- the OutputStream to copy to
- Throws:
- IOException- in case of I/O errors
 
- 
copyCopy the contents of the given InputStream to the given OutputStream.Leaves both streams open when done. - Parameters:
- in- the InputStream to copy from
- out- the OutputStream to copy to
- Returns:
- the number of bytes copied
- Throws:
- IOException- in case of I/O errors
 
- 
copyRangepublic 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- the InputStream to copy from
- out- the OutputStream to copy to
- start- the position to start copying from
- end- the position to end copying
- Returns:
- the number of bytes copied
- Throws:
- IOException- in case of I/O errors
- Since:
- 4.3
 
- 
drainDrain the remaining content of the givenInputStream.Leaves the InputStreamopen when done.- Parameters:
- in- the- InputStreamto drain
- Returns:
- the number of bytes read, or 0if the suppliedInputStreamisnullor empty
- Throws:
- IOException- in case of I/O errors
- Since:
- 4.3
 
- 
emptyInputDeprecated.as of 6.0 in favor ofInputStream.nullInputStream()Return an efficient emptyInputStream.- Returns:
- an InputStream which contains no bytes
- Since:
- 4.2.2
 
- 
nonClosingReturn a variant of the givenInputStreamwhere callingclose()has no effect.- Parameters:
- in- the InputStream to decorate
- Returns:
- a version of the InputStream that ignores calls to close
 
- 
nonClosingReturn a variant of the givenOutputStreamwhere callingclose()has no effect.- Parameters:
- out- the OutputStream to decorate
- Returns:
- a version of the OutputStream that ignores calls to close
 
 
- 
InputStream.nullInputStream()