Class StreamUtil

java.lang.Object
com.saperion.util.StreamUtil

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

    • copyInToOut

      public static void copyInToOut(InputStream in, String inputEncoding, OutputStream out, String outputEncoding, int bufferSize, boolean forceClose) throws 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:
      IOException - Throws an IOException if an IO error occurs.
    • copyInToOut

      public static void copyInToOut(InputStream in, OutputStream out, ByteBuffer buffer, boolean forceClose) throws 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:
      IOException - Throws an IOException if an IO error occurs.
    • transferFrom

      public static void transferFrom(ReadableByteChannel in, FileChannel out, int buffer, boolean forceClose) throws 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:
      IOException - Throws an IOException if an IO error occurs.
    • transferTo

      public static void transferTo(FileChannel in, WritableByteChannel out, int buffer, boolean forceClose) throws 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:
      IOException - Throws an IOException if an IO error occurs.
    • transferTo

      public static long transferTo(ReadableByteChannel in, WritableByteChannel out, int size, boolean forceClose) throws IOException
      Throws:
      IOException
    • copyInToOut

      @Deprecated public static void copyInToOut(InputStream in, OutputStream out, int bufferSize, boolean forceClose) throws 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:
      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(InputStream in, int bufferSize, boolean forceClose) throws 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:
      IOException - Throws an IOException if an IO error occurs.
    • closeQuietly

      public static void closeQuietly(Closeable closeable)
    • compare

      public static boolean compare(InputStream content1, InputStream content2) throws 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:
      IOException - Throws an IOException if an IO error occurs.