Class StreamHelpers


  • public final class StreamHelpers
    extends java.lang.Object
    Miscellaneous operations on Streams.
    • Constructor Summary

      Constructors 
      Constructor Description
      StreamHelpers()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      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.
      static int readAll​(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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • StreamHelpers

        public StreamHelpers()
    • Method Detail

      • readAll

        public static int readAll​(java.io.InputStream stream,
                                  byte[] target,
                                  int startOffset,
                                  int maxLength)
                           throws java.io.IOException
        Reads 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 of InputStream.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.IOException
        Reads 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. Throws EOFException if 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 close
        log - the logger to log a warning with.
        message - the message to log in the event of an exception
        args - template args for the message.