Class StreamUtils


  • public class StreamUtils
    extends Object
    Utility methods to work with streams.
    • 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 new ByteArrayInputStream.

        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 stream
        charset - 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
      • skipBytesInStream

        public static long skipBytesInStream​(InputStream is,
                                             long count)
                                      throws IOException
        Skip the specified number of bytes in the stream.
        Parameters:
        is - stream
        count - 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 stream
        limit - upper limit of remaining bytes
        Returns:
        limited stream
      • getStreamWarningMessage

        public static String getStreamWarningMessage​(String clientName,
                                                     String operationNames)
        Gets the stream warning message to warn the users about closing the stream