Package io.pravega.common.io
Class StreamHelpers
- java.lang.Object
-
- io.pravega.common.io.StreamHelpers
-
public final class StreamHelpers extends java.lang.ObjectMiscellaneous operations on Streams.
-
-
Constructor Summary
Constructors Constructor Description StreamHelpers()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidcloseQuietly(java.io.Closeable toClose, org.slf4j.Logger log, java.lang.String message, java.lang.Object... args)Closes a stream, logging a warning if it fails.static intreadAll(java.io.InputStream stream, byte[] target, int startOffset, int maxLength)Reads at most 'maxLength' bytes from the given input stream, as long as the stream still has data to serve.static byte[]readAll(java.io.InputStream source, int length)Reads a number of bytes from the given InputStream and returns it as the given byte array.
-
-
-
Method Detail
-
readAll
public static int readAll(java.io.InputStream stream, byte[] target, int startOffset, int maxLength) throws java.io.IOExceptionReads at most 'maxLength' bytes from the given input stream, as long as the stream still has data to serve. A note about performance: - This uses the default implementation ofInputStream.read(byte[], int, int), which means in most cases it will copy byte-by-byte into the target array, which is rather inefficient. - See https://github.com/pravega/pravega/issues/2924 for more details.- Parameters:
stream- The InputStream to read from.target- The target array to write data to.startOffset- The offset within the target array to start writing data to.maxLength- The maximum number of bytes to copy.- Returns:
- The number of bytes copied.
- Throws:
java.io.IOException- If unable to read from the given stream.
-
readAll
public static byte[] readAll(java.io.InputStream source, int length) throws java.io.IOExceptionReads a number of bytes from the given InputStream and returns it as the given byte array.- Parameters:
source- The InputStream to read.length- The number of bytes to read.- Returns:
- A byte array containing the contents of the Stream.
- Throws:
java.io.IOException- If unable to read from the given InputStream. ThrowsEOFExceptionif the number of bytes remaining in the InputStream is less than length.
-
closeQuietly
public static void closeQuietly(java.io.Closeable toClose, org.slf4j.Logger log, java.lang.String message, java.lang.Object... args)Closes a stream, logging a warning if it fails.- Parameters:
toClose- the stream/socket etc to closelog- the logger to log a warning with.message- the message to log in the event of an exceptionargs- template args for the message.
-
-