Class PumpStreamHandler

  • All Implemented Interfaces:
    ExecuteStreamHandler

    public class PumpStreamHandler
    extends java.lang.Object
    implements ExecuteStreamHandler
    Copies standard output and error of subprocesses 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.
    • Constructor Summary

      Constructors 
      Constructor Description
      PumpStreamHandler()
      Construct a new PumpStreamHandler.
      PumpStreamHandler​(java.io.OutputStream outAndErr)
      Construct a new PumpStreamHandler.
      PumpStreamHandler​(java.io.OutputStream out, java.io.OutputStream err)
      Construct a new PumpStreamHandler.
      PumpStreamHandler​(java.io.OutputStream out, java.io.OutputStream err, java.io.InputStream input)
      Construct a new PumpStreamHandler.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void createProcessErrorPump​(java.io.InputStream is, java.io.OutputStream os)
      Create the pump to handle error output.
      protected void createProcessOutputPump​(java.io.InputStream is, java.io.OutputStream os)
      Create the pump to handle process output.
      protected java.lang.Thread createPump​(java.io.InputStream is, java.io.OutputStream os)
      Creates a stream pumper to copy the given input stream to the given output stream.
      protected java.lang.Thread createPump​(java.io.InputStream is, java.io.OutputStream os, boolean closeWhenExhausted)
      Creates a stream pumper to copy the given input stream to the given output stream.
      protected java.lang.Thread createPump​(java.io.InputStream is, java.io.OutputStream os, boolean closeWhenExhausted, boolean flushImmediately)
      Creates a stream pumper to copy the given input stream to the given output stream.
      protected java.lang.Thread createSystemInPump​(java.io.InputStream is, java.io.OutputStream os)
      Creates a stream pumper to copy the given input stream to the given output stream.
      void flush()  
      java.io.OutputStream getErr()
      Get the error stream.
      java.io.InputStream getInput()
      Get the input stream.
      java.io.OutputStream getOut()
      Get the output stream.
      protected java.lang.Thread newThread​(java.lang.Runnable task)
      Override this to customize how the background task is created.
      void setProcessErrorStream​(java.io.InputStream is)
      Set the InputStream from which to read the standard error of the process.
      void setProcessInputStream​(java.io.OutputStream os)
      Set the OutputStream by means of which input can be sent to the process.
      void setProcessOutputStream​(java.io.InputStream is)
      Set the InputStream from which to read the standard output of the process.
      void start()
      Start the Threads.
      void stop()
      Stop pumping the streams.
      protected java.lang.Runnable wrapTask​(java.lang.Runnable task)
      Override this to customize how the background task is created.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • log

        private static final org.slf4j.Logger log
      • outputThread

        protected java.lang.Thread outputThread
      • errorThread

        protected java.lang.Thread errorThread
      • inputThread

        protected java.lang.Thread inputThread
      • out

        protected final java.io.OutputStream out
      • err

        protected final java.io.OutputStream err
      • input

        protected final java.io.InputStream input
    • Constructor Detail

      • PumpStreamHandler

        public PumpStreamHandler()
        Construct a new PumpStreamHandler.
      • PumpStreamHandler

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

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

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

      • setProcessOutputStream

        public void setProcessOutputStream​(java.io.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​(java.io.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​(java.io.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.
      • flush

        public void flush()
      • getOut

        public java.io.OutputStream getOut()
        Get the output stream.
        Returns:
        OutputStream.
      • getErr

        public java.io.OutputStream getErr()
        Get the error stream.
        Returns:
        OutputStream.
      • getInput

        public java.io.InputStream getInput()
        Get the input stream.
        Returns:
        InputStream.
      • createProcessOutputPump

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

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

        protected java.lang.Thread createPump​(java.io.InputStream is,
                                              java.io.OutputStream os)
        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
        Returns:
        the stream pumper thread
      • createPump

        protected java.lang.Thread createPump​(java.io.InputStream is,
                                              java.io.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
      • createPump

        protected java.lang.Thread createPump​(java.io.InputStream is,
                                              java.io.OutputStream os,
                                              boolean closeWhenExhausted,
                                              boolean flushImmediately)
        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
        flushImmediately - flush the output stream whenever data was written to it
        Returns:
        the stream pumper thread
      • createSystemInPump

        protected java.lang.Thread createSystemInPump​(java.io.InputStream is,
                                                      java.io.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
      • newThread

        protected java.lang.Thread newThread​(java.lang.Runnable task)
        Override this to customize how the background task is created.
        Parameters:
        task - the task to be run in the background
        Returns:
        the thread of the task
      • wrapTask

        protected java.lang.Runnable wrapTask​(java.lang.Runnable task)
        Override this to customize how the background task is created.
        Parameters:
        task - the task to be run in the background
        Returns:
        the runnable of the wrapped task