public class StreamUtils extends Object
Utility methods to work with streams.
Modifier and Type | Method and Description |
---|---|
static void |
closeQuietly(InputStream stream)
Close a stream without raising an exception.
|
static Optional<String> |
contentDiffers(InputStream input1,
InputStream input2)
Return
Optional.empty() if the content of the two input streams is the same. |
static ByteArrayInputStream |
createByteArrayInputStream(byte[] buffer)
Creates a new
ByteArrayInputStream . |
static String |
getStreamWarningMessage(String clientName,
String operationNames)
Gets the stream warning message to warn the users about closing the stream
|
static boolean |
isExtraStreamLogsEnabled()
The boolean value indicating if extra logs related to operations that return streams are
enabled/disabled.
|
static InputStream |
limitRemainingStreamLength(InputStream is,
long limit)
Limit the number of bytes remaining in the input stream to at most this many bytes.
|
static long |
skipBytesInStream(InputStream is,
long count)
Skip the specified number of bytes in the stream.
|
static byte[] |
toByteArray(InputStream inputStream)
Convert the input stream to a byte array.
|
static InputStream |
toInputStream(File file)
Convert a File to an InputStream.
|
static String |
toString(InputStream is,
Charset charset)
Convert the input stream to a string.
|
public static boolean isExtraStreamLogsEnabled()
The boolean value indicating if extra logs related to operations that return streams are enabled/disabled. Disabling this will disable warnings to close the streams, logs about wrapping response stream in an auto-closeble stream. Default is true.
public static void closeQuietly(InputStream stream)
Close a stream without raising an exception.
stream
- The stream to close.public static InputStream toInputStream(File file)
Convert a File to an InputStream. This uses WrappedFileInputStream
so the caller
still has access to the original file if needed. If the file is not valid, an
IllegalArgumentException will be raised.
file
- The file to convert to a stream.public static ByteArrayInputStream createByteArrayInputStream(byte[] buffer)
Creates a new ByteArrayInputStream
. This uses WrappedByteArrayInputStream
so
the underlying buffer can be reused by multiple copies of the stream if needed.
buffer
- The byte buffer.public static String toString(InputStream is, Charset charset)
Convert the input stream to a string.
is
- input streamcharset
- charset to usepublic static byte[] toByteArray(InputStream inputStream) throws IOException
Convert the input stream to a byte array.
inputStream
- input streamIOException
public static Optional<String> contentDiffers(InputStream input1, InputStream input2) throws IOException
Return Optional.empty()
if the content of the two input streams is the same.
Otherwise, it returns a description of the first difference.
input1
- first input streaminput2
- second input streamOptional.empty()
if the contents are
the sameIOException
public static long skipBytesInStream(InputStream is, long count) throws IOException
Skip the specified number of bytes in the stream.
is
- streamcount
- number of bytes to skipIOException
public static InputStream limitRemainingStreamLength(InputStream is, long limit)
Limit the number of bytes remaining in the input stream to at most this many bytes. The input stream may reach its end earlier.
is
- input streamlimit
- upper limit of remaining bytesCopyright © 2016–2024. All rights reserved.