Class PumpStreamHandler

java.lang.Object
org.apache.commons.exec.PumpStreamHandler
All Implemented Interfaces:
ExecuteStreamHandler

public class PumpStreamHandler extends Object implements ExecuteStreamHandler
Copies standard output and error of sub-processes to standard output and error of the parent process. If output or error stream are set to null, any feedback from that stream will be lost.
Version:
$Id: PumpStreamHandler.java 1557263 2014-01-10 21:18:09Z ggregory $
  • Field Details

    • STOP_TIMEOUT_ADDITION

      private static final long STOP_TIMEOUT_ADDITION
      See Also:
    • outputThread

      private Thread outputThread
    • errorThread

      private Thread errorThread
    • inputThread

      private Thread inputThread
    • out

      private final OutputStream out
    • err

      private final OutputStream err
    • input

      private final InputStream input
    • inputStreamPumper

      private InputStreamPumper inputStreamPumper
    • stopTimeout

      private long stopTimeout
      the timeout in ms the implementation waits when stopping the pumper threads
    • caught

      private IOException caught
      the last exception being caught
  • Constructor Details

    • PumpStreamHandler

      public PumpStreamHandler()
      Construct a new PumpStreamHandler.
    • PumpStreamHandler

      public PumpStreamHandler(OutputStream outAndErr)
      Construct a new PumpStreamHandler.
      Parameters:
      outAndErr - the output/error OutputStream.
    • PumpStreamHandler

      public PumpStreamHandler(OutputStream out, OutputStream err)
      Construct a new PumpStreamHandler.
      Parameters:
      out - the output OutputStream.
      err - the error OutputStream.
    • PumpStreamHandler

      public PumpStreamHandler(OutputStream out, OutputStream err, InputStream input)
      Construct a new PumpStreamHandler.
      Parameters:
      out - the output OutputStream.
      err - the error OutputStream.
      input - the input InputStream.
  • Method Details

    • setStopTimeout

      public void setStopTimeout(long timeout)
      Set maximum time to wait until output streams are exchausted when stop() was called.
      Parameters:
      timeout - timeout in milliseconds or zero to wait forever (default)
    • setProcessOutputStream

      public void setProcessOutputStream(InputStream is)
      Set the InputStream from which to read the standard output of the process.
      Specified by:
      setProcessOutputStream in interface ExecuteStreamHandler
      Parameters:
      is - the InputStream.
    • setProcessErrorStream

      public void setProcessErrorStream(InputStream is)
      Set the InputStream from which to read the standard error of the process.
      Specified by:
      setProcessErrorStream in interface ExecuteStreamHandler
      Parameters:
      is - the InputStream.
    • setProcessInputStream

      public void setProcessInputStream(OutputStream os)
      Set the OutputStream by means of which input can be sent to the process.
      Specified by:
      setProcessInputStream in interface ExecuteStreamHandler
      Parameters:
      os - the OutputStream.
    • start

      public void start()
      Start the Threads.
      Specified by:
      start in interface ExecuteStreamHandler
    • stop

      public void stop() throws IOException
      Stop pumping the streams. When a timeout is specified it it is not guaranteed that the pumper threads are cleanly terminated.
      Specified by:
      stop in interface ExecuteStreamHandler
      Throws:
      IOException - thrown when an I/O exception occurs.
    • getErr

      protected OutputStream getErr()
      Get the error stream.
      Returns:
      OutputStream.
    • getOut

      protected OutputStream getOut()
      Get the output stream.
      Returns:
      OutputStream.
    • createProcessOutputPump

      protected void createProcessOutputPump(InputStream is, OutputStream os)
      Create the pump to handle process output.
      Parameters:
      is - the InputStream.
      os - the OutputStream.
    • createProcessErrorPump

      protected void createProcessErrorPump(InputStream is, OutputStream os)
      Create the pump to handle error output.
      Parameters:
      is - the InputStream.
      os - the OutputStream.
    • createPump

      protected Thread createPump(InputStream is, OutputStream os)
      Creates a stream pumper to copy the given input stream to the given output stream. When the 'os' is an PipedOutputStream we are closing 'os' afterwards to avoid an IOException ("Write end dead").
      Parameters:
      is - the input stream to copy from
      os - the output stream to copy into
      Returns:
      the stream pumper thread
    • createPump

      protected Thread createPump(InputStream is, OutputStream os, boolean closeWhenExhausted)
      Creates a stream pumper to copy the given input stream to the given output stream.
      Parameters:
      is - the input stream to copy from
      os - the output stream to copy into
      closeWhenExhausted - close the output stream when the input stream is exhausted
      Returns:
      the stream pumper thread
    • stopThread

      protected void stopThread(Thread thread, long timeout)
      Stopping a pumper thread. The implementation actually waits longer than specified in 'timeout' to detect if the timeout was indeed exceeded. If the timeout was exceeded an IOException is created to be thrown to the caller.
      Parameters:
      thread - the thread to be stopped
      timeout - the time in ms to wait to join
    • createSystemInPump

      private Thread createSystemInPump(InputStream is, OutputStream os)
      Creates a stream pumper to copy the given input stream to the given output stream.
      Parameters:
      is - the System.in input stream to copy from
      os - the output stream to copy into
      Returns:
      the stream pumper thread