Class StreamUtil

java.lang.Object
com.randomnoun.common.StreamUtil

public class StreamUtil extends Object
Utility class to copy streams synchronously and asynchronously.
Author:
knoxg
  • Constructor Details

    • StreamUtil

      public StreamUtil()
      Creates a new StreamUtils object.
  • Method Details

    • copyStream

      public static void copyStream(InputStream input, OutputStream output, int bufSize) throws IOException
      Copies the data from an inputStream to an outputstream (used to mimic pipes).
      Parameters:
      input - The stream to retrieve information from
      output - The stream to send data to
      bufSize - buffer size in bytes
      Throws:
      IOException
    • copyStreamCount

      public static long copyStreamCount(InputStream input, OutputStream output, int bufSize) throws IOException
      Copies a stream and return number of bytes copied
      Parameters:
      input - The stream to retrieve information from
      output - The stream to send data to
      bufSize - buffer size in bytes
      Throws:
      IOException
    • copyStream

      public static void copyStream(InputStream input, OutputStream output) throws IOException
      Copies the data from an inputStream to an outputstream
      Parameters:
      input - The stream to retrieve information from
      output - The stream to send data to
      Throws:
      IOException
    • getByteArray

      public static byte[] getByteArray(InputStream input) throws IOException
      Reads all available data from an InputStream, and returns it in a single byte array.
      Parameters:
      input - The stream to receive information from
      Returns:
      A byte array containing the contents of the stream
      Throws:
      IOException
    • copyThread

      public static Thread copyThread(InputStream input, OutputStream output, int bufSize)
      Returns a thread that, when started, will pipe all data from one inputstream to an outputstream. The thread will complete when the inputStream returns EOF.
      Parameters:
      input - The stream to retrieve information from
      output - The stream to send data to
      bufSize - buffer size in bytes
    • indexOf

      public static int indexOf(InputStream input, String searchText) throws IOException
      Scans this input stream until the text in 'searchText' is found. Returns the number of bytes skipped if the search text was found, or -1 if the text was not found.
      Parameters:
      input - The input stream to scan
      searchText - The text we are searching for
      Throws:
      IOException - if an IO Exception occurs reading the stream
    • readUntil

      public static String readUntil(InputStream input, String searchText) throws IOException
      Reads this input stream until the text in 'searchText' is found. Returns a String containing the data read, including the searchText. Returns null if the text was not found.
      Parameters:
      input - The input stream to scan
      searchText - The text we are searching for
      Throws:
      IOException - if an IO Exception occurs reading the stream
    • copyAndCloseThread

      public static Thread copyAndCloseThread(InputStream input, OutputStream output, int bufSize)
      Returns a thread that, when started, will pipe all data from one inputstream to an outputstream. The thread will complete when the inputStream returns EOF. The output stream will also be closed.
      Parameters:
      input - The stream to retrieve information from
      output - The stream to send data to
      bufSize - buffer size in bytes