Class StreamGobbler

java.lang.Object
java.lang.Thread
com.seeq.utilities.process.StreamGobbler
All Implemented Interfaces:
Runnable

public class StreamGobbler extends Thread
Consumes lines of text that emerge from an InputStream object on a separate thread from the caller, which is necessary for proper handling of spawned processes.
  • Constructor Details

    • StreamGobbler

      public StreamGobbler(InputStream inputStream)
      Allocates a StreamGobbler object and pointed to the specified input stream.
      Parameters:
      inputStream - The input stream to be "gobbled"
  • Method Details

    • run

      public void run()
      Gobbles text from the specified inputStream. This function should not be called directly, use start() instead so that a thread is spawned.
      Specified by:
      run in interface Runnable
      Overrides:
      run in class Thread
    • isCaptureTextEnabled

      public boolean isCaptureTextEnabled()
      True if all output and error content should be captured in a string and accessible via the getText() accessor.
      Returns:
      true if output and error content is being captured
    • setCaptureTextEnabled

      public void setCaptureTextEnabled(boolean captureTextEnabled)
      Controls whether output and error content should be captured in a string and accessible via the getText() accessor.
      Parameters:
      captureTextEnabled - true if output and error content should be captured
    • getText

      public String getText()
      Returns the gobbled text. Note that this is ALL text received since the last call to clearText().
      Returns:
      the gobbled text
    • clearText

      public void clearText()
      Clears the internal text buffer.
    • getLineReceivedCallback

      public StreamGobbler.LineReceivedCallback getLineReceivedCallback()
      Returns the callback that is used when a line of text is received on the stream.
      Returns:
      the callback that is used when a line of text is received on the stream
    • setLineReceivedCallback

      public void setLineReceivedCallback(StreamGobbler.LineReceivedCallback lineReceivedCallback)
      Sets the callback that is used when a line of text is received on the stream.
      Parameters:
      lineReceivedCallback - the callback that is used when a line of text is received on the stream
    • waitFor

      public void waitFor() throws InterruptedException
      Waits for the input stream to close and all gobbled text to be collected.
      Throws:
      InterruptedException - If the thread was interrupted by an exception.
    • waitFor

      public void waitFor(long timeout) throws InterruptedException
      Waits for the input stream to close and all gobbled text to be collected.
      Parameters:
      timeout - time to wait (in milliseconds)
      Throws:
      InterruptedException - If the thread was interrupted by an exception.