Class StreamUtil

java.lang.Object
com.kosdev.kos.commons.util.StreamUtil

public final class StreamUtil extends Object
A few common stream utilities.
Since:
9
Version:
9
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    close(Closeable closeable)
    Closes the given closeable and ignores any thrown exceptions
    static boolean
    copy(File src, File dst)
    Copy the source file to the destination.
    static boolean
    copy(File file, OutputStream os)
    Copy the contents of the file to the output stream.
    static boolean
    copy(File file, OutputStream os, boolean closeOutput)
    Copy the contents of the file to the output stream.
    static boolean
    copy(InputStream is, File file)
    Copy the input stream to the specified file.
    static boolean
    copy(InputStream is, File file, boolean closeInput)
    Copy the stream to the specified file.
    static boolean
    Copy the contents of the input stream to the output stream.
    static boolean
    copy(InputStream is, OutputStream os, boolean closeInput, boolean closeOutput)
    Copy the contents of the input stream to the output stream and close inputs based on supplied flags.
    static boolean
    copy(InputStream is, OutputStream os, boolean closeInput, boolean closeOutput, int size)
    Copy the contents of the input stream to the output stream.
    static byte[]
    Reads all remaining bytes from the stream
    static byte[]
    readAllBytes(InputStream in, boolean closeInput)
    Reads all remaining bytes from the stream
    static byte[]
    readBytes(InputStream in, int len)
    Forces reading the given length from the given stream
    static byte[]
    readBytes(InputStream in, int len, boolean closeInput)
    Forces reading the given length from the given stream
    static void
    write(byte[] bytes, OutputStream os)
    Write the array of bytes to given output stream.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • StreamUtil

      public StreamUtil()
  • Method Details

    • copy

      public static boolean copy(InputStream is, OutputStream os, boolean closeInput, boolean closeOutput, int size)
      Copy the contents of the input stream to the output stream. Close the steams based on the close flags.
      Parameters:
      is - the input stream to copy from
      os - the output stream to copy to
      closeInput - close the input when done
      closeOutput - close the output when done
      size - the block size
      Returns:
      false if there was any error
    • copy

      public static boolean copy(InputStream is, OutputStream os, boolean closeInput, boolean closeOutput)
      Copy the contents of the input stream to the output stream and close inputs based on supplied flags.
      Parameters:
      is - the input stream to copy from
      os - the output stream to copy to
      closeInput - close the input when done
      closeOutput - close the output when done
      Returns:
      false if there was any error
    • copy

      public static boolean copy(InputStream is, OutputStream os)
      Copy the contents of the input stream to the output stream. This will close the input and output stream automatically.
      Parameters:
      is - the input stream to copy from
      os - the output stream to copy to
      Returns:
      false if there was any error
    • copy

      public static boolean copy(File file, OutputStream os, boolean closeOutput)
      Copy the contents of the file to the output stream. Close the output stream if specified.
      Parameters:
      file - the file to send
      os - the output stream to copy to
      closeOutput - close the output when done
      Returns:
      false if there was any error
    • copy

      public static boolean copy(File file, OutputStream os)
      Copy the contents of the file to the output stream. Close the output stream upon completion.
      Parameters:
      file - the file to send
      os - the output stream to copy to
      Returns:
      false if there was any error
    • copy

      public static boolean copy(InputStream is, File file, boolean closeInput)
      Copy the stream to the specified file. Close the input stream if specified.
      Parameters:
      is - the input stream to copy from
      file - the file to write to
      closeInput - close the input when done
      Returns:
      false if there was any error
    • copy

      public static boolean copy(InputStream is, File file)
      Copy the input stream to the specified file. Close the input stream upon completion.
      Parameters:
      is - the input stream to copy from
      file - the file to write to
      Returns:
      false if there was any error
    • copy

      public static boolean copy(File src, File dst)
      Copy the source file to the destination.
      Parameters:
      src - the file to copy
      dst - the file to copy to
      Returns:
      false if there was any error
    • readAllBytes

      public static byte[] readAllBytes(InputStream in)
      Reads all remaining bytes from the stream
      Parameters:
      in - input stream
      Returns:
      read bytes
    • readAllBytes

      public static byte[] readAllBytes(InputStream in, boolean closeInput)
      Reads all remaining bytes from the stream
      Parameters:
      in - input stream
      closeInput - indicator whether the input stream should be closed
      Returns:
      read bytes
    • readBytes

      public static byte[] readBytes(InputStream in, int len)
      Forces reading the given length from the given stream
      Parameters:
      in - input stream
      len - length to read
      Returns:
      read bytes
    • readBytes

      public static byte[] readBytes(InputStream in, int len, boolean closeInput)
      Forces reading the given length from the given stream
      Parameters:
      in - input stream
      len - length to read
      closeInput - indicates if the input should be closed
      Returns:
      read bytes
    • close

      public static void close(Closeable closeable)
      Closes the given closeable and ignores any thrown exceptions
      Parameters:
      closeable - closeable
    • write

      public static void write(byte[] bytes, OutputStream os)
      Write the array of bytes to given output stream.
      Parameters:
      bytes - write to output stream
      os - output stream to write to