Class StreamUtil


  • public final class StreamUtil
    extends java.lang.Object
    Utility class for copying streams, file channels and append byte arrays.
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static byte[] append​(byte[] array1, byte[] array2)
      Creates a new array from the content of the two given byte arrays.
      static void closeQuietly​(java.io.Closeable closeable)  
      static boolean compare​(java.io.InputStream content1, java.io.InputStream content2)
      Compares the given input streams (pair-wise byte comparison).
      static void copyInToOut​(java.io.InputStream in, java.io.OutputStream out, int bufferSize, boolean forceClose)
      static void copyInToOut​(java.io.InputStream in, java.io.OutputStream out, java.nio.ByteBuffer buffer, boolean forceClose)
      Copies the content from in to out.
      static void copyInToOut​(java.io.InputStream in, java.lang.String inputEncoding, java.io.OutputStream out, java.lang.String outputEncoding, int bufferSize, boolean forceClose)
      Copies the content from in to out.
      static byte[] readBytes​(java.io.InputStream in, int bufferSize, boolean forceClose)
      static void transferFrom​(java.nio.channels.ReadableByteChannel in, java.nio.channels.FileChannel out, int buffer, boolean forceClose)
      Transfers (copies) the content from in to out.
      static void transferTo​(java.nio.channels.FileChannel in, java.nio.channels.WritableByteChannel out, int buffer, boolean forceClose)
      Transforms (copies) the content of the given FileChannel to the given WritableByteChannel.
      static long transferTo​(java.nio.channels.ReadableByteChannel in, java.nio.channels.WritableByteChannel out, int size, boolean forceClose)  
      • Methods inherited from class java.lang.Object

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

      • copyInToOut

        public static void copyInToOut​(java.io.InputStream in,
                                       java.lang.String inputEncoding,
                                       java.io.OutputStream out,
                                       java.lang.String outputEncoding,
                                       int bufferSize,
                                       boolean forceClose)
                                throws java.io.IOException
        Copies the content from in to out.
        Parameters:
        in - The source InputStream.
        inputEncoding - The encoding of the input stream.
        out - The destination OutputStream.
        outputEncoding - The encoding of the output stream.
        bufferSize - The buffer size.
        forceClose - If true, the given streams will be closed after execution.
        Throws:
        java.io.IOException - Throws an IOException if an IO error occurs.
      • copyInToOut

        public static void copyInToOut​(java.io.InputStream in,
                                       java.io.OutputStream out,
                                       java.nio.ByteBuffer buffer,
                                       boolean forceClose)
                                throws java.io.IOException
        Copies the content from in to out.
        Parameters:
        in - The source InputStream.
        out - The destination OutputStream.
        buffer - The buffer size.
        forceClose - If true, the given channels will be closed after execution.
        Throws:
        java.io.IOException - Throws an IOException if an IO error occurs.
      • transferFrom

        public static void transferFrom​(java.nio.channels.ReadableByteChannel in,
                                        java.nio.channels.FileChannel out,
                                        int buffer,
                                        boolean forceClose)
                                 throws java.io.IOException
        Transfers (copies) the content from in to out.
        Parameters:
        in - The source ReadableByteChannel.
        out - The destination FileChannel.
        buffer - The buffer size.
        forceClose - If true, the given channels will be closed after execution.
        Throws:
        java.io.IOException - Throws an IOException if an IO error occurs.
      • transferTo

        public static void transferTo​(java.nio.channels.FileChannel in,
                                      java.nio.channels.WritableByteChannel out,
                                      int buffer,
                                      boolean forceClose)
                               throws java.io.IOException
        Transforms (copies) the content of the given FileChannel to the given WritableByteChannel.
        Parameters:
        in - The FileChannel to read byte from.
        out - The WritableByteChannel to transform bytes to.
        buffer - The buffer size.
        forceClose - If true, the given channels will be closed after execution.
        Throws:
        java.io.IOException - Throws an IOException if an IO error occurs.
      • transferTo

        public static long transferTo​(java.nio.channels.ReadableByteChannel in,
                                      java.nio.channels.WritableByteChannel out,
                                      int size,
                                      boolean forceClose)
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • copyInToOut

        @Deprecated
        public static void copyInToOut​(java.io.InputStream in,
                                       java.io.OutputStream out,
                                       int bufferSize,
                                       boolean forceClose)
                                throws java.io.IOException
        Copies the content of the given InputStream to the given OutputStream.
        Parameters:
        in - The InputStream to read the content from.
        out - The OutputStream to copy the content to.
        bufferSize - The buffer size.
        forceClose - If true, the given streams will be closed after execution.
        Throws:
        java.io.IOException - Throws an IOException if an IO error occurs.
      • append

        public static byte[] append​(byte[] array1,
                                    byte[] array2)
        Creates a new array from the content of the two given byte arrays.
        Parameters:
        array1 - The first content array.
        array2 - The second content array.
        Returns:
        The newly created byte array.
      • readBytes

        @Deprecated
        public static byte[] readBytes​(java.io.InputStream in,
                                       int bufferSize,
                                       boolean forceClose)
                                throws java.io.IOException
        Creates a byte array of the given InputStream and returns it.
        Parameters:
        in - The InputSteam in to copy.
        bufferSize - The size of the array to be created.
        forceClose - It true, all streams will be closed.
        Returns:
        the created byte array.
        Throws:
        java.io.IOException - Throws an IOException if an IO error occurs.
      • closeQuietly

        public static void closeQuietly​(java.io.Closeable closeable)
      • compare

        public static boolean compare​(java.io.InputStream content1,
                                      java.io.InputStream content2)
                               throws java.io.IOException
        Compares the given input streams (pair-wise byte comparison).
        Parameters:
        content1 - The first InputStream in question.
        content2 - The second InputStream in question.
        Returns:
        Returns true, if the given stream are equal.
        Throws:
        java.io.IOException - Throws an IOException if an IO error occurs.