Package com.oracle.bmc.util
Class StreamUtils
- java.lang.Object
-
- com.oracle.bmc.util.StreamUtils
-
public class StreamUtils extends Object
Utility methods to work with streams.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
closeQuietly(InputStream stream)
Close a stream without raising an exception.static Optional<String>
contentDiffers(InputStream input1, InputStream input2)
ReturnOptional.empty()
if the content of the two input streams is the same.static ByteArrayInputStream
createByteArrayInputStream(byte[] buffer)
Creates a newByteArrayInputStream
.static String
getStreamWarningMessage(String clientName, String operationNames)
Gets the stream warning message to warn the users about closing the streamstatic 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.
-
-
-
Method Detail
-
isExtraStreamLogsEnabled
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.
-
closeQuietly
public static void closeQuietly(InputStream stream)
Close a stream without raising an exception.- Parameters:
stream
- The stream to close.
-
toInputStream
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.- Parameters:
file
- The file to convert to a stream.- Returns:
- A new InputStream instance.
-
createByteArrayInputStream
public static ByteArrayInputStream createByteArrayInputStream(byte[] buffer)
Creates a newByteArrayInputStream
.This uses
WrappedByteArrayInputStream
so the underlying buffer can be reused by multiple copies of the stream if needed.- Parameters:
buffer
- The byte buffer.- Returns:
- A new ByteArrayInputStream instance.
-
toString
public static String toString(InputStream is, Charset charset)
Convert the input stream to a string.- Parameters:
is
- input streamcharset
- charset to use- Returns:
- string
-
toByteArray
public static byte[] toByteArray(InputStream inputStream) throws IOException
Convert the input stream to a byte array.- Parameters:
inputStream
- input stream- Returns:
- byte array
- Throws:
IOException
-
contentDiffers
public static Optional<String> contentDiffers(InputStream input1, InputStream input2) throws IOException
ReturnOptional.empty()
if the content of the two input streams is the same.Otherwise, it returns a description of the first difference.
- Parameters:
input1
- first input streaminput2
- second input stream- Returns:
- description of the first difference, or
Optional.empty()
if the contents are the same - Throws:
IOException
-
skipBytesInStream
public static long skipBytesInStream(InputStream is, long count) throws IOException
Skip the specified number of bytes in the stream.- Parameters:
is
- streamcount
- number of bytes to skip- Returns:
- number of bytes that were skipped
- Throws:
IOException
-
limitRemainingStreamLength
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.
- Parameters:
is
- input streamlimit
- upper limit of remaining bytes- Returns:
- limited stream
-
-