Class StreamUtils


  • public final class StreamUtils
    extends java.lang.Object
    Provides utility methods to copy streams.
    • Constructor Summary

      Constructors 
      Constructor Description
      StreamUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void closeQuietly​(java.io.Closeable c)
      Close and ignore all errors.
      static void copyStream​(java.io.InputStream input, java.io.OutputStream output)
      Allocates a 4096 byte[] for use as a temporary buffer and calls copyStream(InputStream, OutputStream, byte[]).
      static void copyStream​(java.io.InputStream input, java.io.OutputStream output, byte[] buffer)
      Copy the data from an InputStream to an OutputStream, using the specified byte[] as a temporary buffer.
      static void copyStream​(java.io.InputStream input, java.io.OutputStream output, int bufferSize)
      Allocates a byte[] of the specified size for use as a temporary buffer and calls copyStream(InputStream, OutputStream, byte[]).
      static void copyStream​(java.io.InputStream input, java.nio.ByteBuffer output)
      Allocates a 4096 byte[] for use as a temporary buffer and calls copyStream(InputStream, OutputStream, byte[]).
      static int copyStream​(java.io.InputStream input, java.nio.ByteBuffer output, byte[] buffer)
      Copy the data from an InputStream to a ByteBuffer, using the specified byte[] as a temporary buffer.
      static void copyStream​(java.io.InputStream input, java.nio.ByteBuffer output, int bufferSize)
      Allocates a byte[] of the specified size for use as a temporary buffer and calls copyStream(InputStream, ByteBuffer, byte[]).
      static byte[] copyStreamToByteArray​(java.io.InputStream input)
      Copy the data from an InputStream to a byte array.
      static byte[] copyStreamToByteArray​(java.io.InputStream input, int estimatedSize)
      Copy the data from an InputStream to a byte array.
      static java.lang.String copyStreamToString​(java.io.InputStream input)
      Calls copyStreamToString(InputStream, int, String) using the input's available size and the platform's default charset.
      static java.lang.String copyStreamToString​(java.io.InputStream input, int estimatedSize)
      Calls copyStreamToString(InputStream, int, String) using the platform's default charset.
      static java.lang.String copyStreamToString​(java.io.InputStream input, int estimatedSize, java.lang.String charset)
      Copy the data from an InputStream to a string using the specified charset.
      • Methods inherited from class java.lang.Object

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

      • EMPTY_BYTES

        public static final byte[] EMPTY_BYTES
    • Constructor Detail

      • StreamUtils

        public StreamUtils()
    • Method Detail

      • copyStream

        public static void copyStream​(java.io.InputStream input,
                                      java.io.OutputStream output)
                               throws java.io.IOException
        Allocates a 4096 byte[] for use as a temporary buffer and calls copyStream(InputStream, OutputStream, byte[]).
        Throws:
        java.io.IOException
      • copyStream

        public static void copyStream​(java.io.InputStream input,
                                      java.io.OutputStream output,
                                      int bufferSize)
                               throws java.io.IOException
        Allocates a byte[] of the specified size for use as a temporary buffer and calls copyStream(InputStream, OutputStream, byte[]).
        Throws:
        java.io.IOException
      • copyStream

        public static void copyStream​(java.io.InputStream input,
                                      java.io.OutputStream output,
                                      byte[] buffer)
                               throws java.io.IOException
        Copy the data from an InputStream to an OutputStream, using the specified byte[] as a temporary buffer. The stream is not closed.
        Throws:
        java.io.IOException
      • copyStream

        public static void copyStream​(java.io.InputStream input,
                                      java.nio.ByteBuffer output)
                               throws java.io.IOException
        Allocates a 4096 byte[] for use as a temporary buffer and calls copyStream(InputStream, OutputStream, byte[]).
        Throws:
        java.io.IOException
      • copyStream

        public static void copyStream​(java.io.InputStream input,
                                      java.nio.ByteBuffer output,
                                      int bufferSize)
                               throws java.io.IOException
        Allocates a byte[] of the specified size for use as a temporary buffer and calls copyStream(InputStream, ByteBuffer, byte[]).
        Throws:
        java.io.IOException
      • copyStream

        public static int copyStream​(java.io.InputStream input,
                                     java.nio.ByteBuffer output,
                                     byte[] buffer)
                              throws java.io.IOException
        Copy the data from an InputStream to a ByteBuffer, using the specified byte[] as a temporary buffer. The buffer's limit is increased by the number of bytes copied, the position is left unchanged. The stream is not closed.
        Parameters:
        output - Must be a direct Buffer with native byte order and the buffer MUST be large enough to hold all the bytes in the stream. No error checking is performed.
        Returns:
        the number of bytes copied.
        Throws:
        java.io.IOException
      • copyStreamToByteArray

        public static byte[] copyStreamToByteArray​(java.io.InputStream input)
                                            throws java.io.IOException
        Copy the data from an InputStream to a byte array. The stream is not closed.
        Throws:
        java.io.IOException
      • copyStreamToByteArray

        public static byte[] copyStreamToByteArray​(java.io.InputStream input,
                                                   int estimatedSize)
                                            throws java.io.IOException
        Copy the data from an InputStream to a byte array. The stream is not closed.
        Parameters:
        estimatedSize - Used to allocate the output byte[] to possibly avoid an array copy.
        Throws:
        java.io.IOException
      • copyStreamToString

        public static java.lang.String copyStreamToString​(java.io.InputStream input)
                                                   throws java.io.IOException
        Calls copyStreamToString(InputStream, int, String) using the input's available size and the platform's default charset.
        Throws:
        java.io.IOException
      • copyStreamToString

        public static java.lang.String copyStreamToString​(java.io.InputStream input,
                                                          int estimatedSize)
                                                   throws java.io.IOException
        Calls copyStreamToString(InputStream, int, String) using the platform's default charset.
        Throws:
        java.io.IOException
      • copyStreamToString

        public static java.lang.String copyStreamToString​(java.io.InputStream input,
                                                          int estimatedSize,
                                                          @Null
                                                          java.lang.String charset)
                                                   throws java.io.IOException
        Copy the data from an InputStream to a string using the specified charset.
        Parameters:
        estimatedSize - Used to allocate the output buffer to possibly avoid an array copy.
        charset - May be null to use the platform's default charset.
        Throws:
        java.io.IOException
      • closeQuietly

        public static void closeQuietly​(java.io.Closeable c)
        Close and ignore all errors.