Package org.apache.commons.exec
Class PumpStreamHandler
- java.lang.Object
-
- org.apache.commons.exec.PumpStreamHandler
-
- All Implemented Interfaces:
ExecuteStreamHandler
public class PumpStreamHandler extends java.lang.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 Summary
Fields Modifier and Type Field Description private java.io.IOExceptioncaughtthe last exception being caughtprivate java.io.OutputStreamerrprivate java.lang.ThreaderrorThreadprivate java.io.InputStreaminputprivate InputStreamPumperinputStreamPumperprivate java.lang.ThreadinputThreadprivate java.io.OutputStreamoutprivate java.lang.ThreadoutputThreadprivate static longSTOP_TIMEOUT_ADDITIONprivate longstopTimeoutthe timeout in ms the implementation waits when stopping the pumper threads
-
Constructor Summary
Constructors Constructor Description PumpStreamHandler()Construct a newPumpStreamHandler.PumpStreamHandler(java.io.OutputStream outAndErr)Construct a newPumpStreamHandler.PumpStreamHandler(java.io.OutputStream out, java.io.OutputStream err)Construct a newPumpStreamHandler.PumpStreamHandler(java.io.OutputStream out, java.io.OutputStream err, java.io.InputStream input)Construct a newPumpStreamHandler.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidcreateProcessErrorPump(java.io.InputStream is, java.io.OutputStream os)Create the pump to handle error output.protected voidcreateProcessOutputPump(java.io.InputStream is, java.io.OutputStream os)Create the pump to handle process output.protected java.lang.ThreadcreatePump(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.ThreadcreatePump(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.private java.lang.ThreadcreateSystemInPump(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.io.OutputStreamgetErr()Get the error stream.protected java.io.OutputStreamgetOut()Get the output stream.voidsetProcessErrorStream(java.io.InputStream is)Set theInputStreamfrom which to read the standard error of the process.voidsetProcessInputStream(java.io.OutputStream os)Set theOutputStreamby means of which input can be sent to the process.voidsetProcessOutputStream(java.io.InputStream is)Set theInputStreamfrom which to read the standard output of the process.voidsetStopTimeout(long timeout)Set maximum time to wait until output streams are exchausted whenstop()was called.voidstart()Start theThreads.voidstop()Stop pumping the streams.protected voidstopThread(java.lang.Thread thread, long timeout)Stopping a pumper thread.
-
-
-
Field Detail
-
STOP_TIMEOUT_ADDITION
private static final long STOP_TIMEOUT_ADDITION
- See Also:
- Constant Field Values
-
outputThread
private java.lang.Thread outputThread
-
errorThread
private java.lang.Thread errorThread
-
inputThread
private java.lang.Thread inputThread
-
out
private final java.io.OutputStream out
-
err
private final java.io.OutputStream err
-
input
private final java.io.InputStream input
-
inputStreamPumper
private InputStreamPumper inputStreamPumper
-
stopTimeout
private long stopTimeout
the timeout in ms the implementation waits when stopping the pumper threads
-
caught
private java.io.IOException caught
the last exception being caught
-
-
Constructor Detail
-
PumpStreamHandler
public PumpStreamHandler()
Construct a newPumpStreamHandler.
-
PumpStreamHandler
public PumpStreamHandler(java.io.OutputStream outAndErr)
Construct a newPumpStreamHandler.- Parameters:
outAndErr- the output/errorOutputStream.
-
PumpStreamHandler
public PumpStreamHandler(java.io.OutputStream out, java.io.OutputStream err)Construct a newPumpStreamHandler.- Parameters:
out- the outputOutputStream.err- the errorOutputStream.
-
PumpStreamHandler
public PumpStreamHandler(java.io.OutputStream out, java.io.OutputStream err, java.io.InputStream input)Construct a newPumpStreamHandler.- Parameters:
out- the outputOutputStream.err- the errorOutputStream.input- the inputInputStream.
-
-
Method Detail
-
setStopTimeout
public void setStopTimeout(long timeout)
Set maximum time to wait until output streams are exchausted whenstop()was called.- Parameters:
timeout- timeout in milliseconds or zero to wait forever (default)
-
setProcessOutputStream
public void setProcessOutputStream(java.io.InputStream is)
Set theInputStreamfrom which to read the standard output of the process.- Specified by:
setProcessOutputStreamin interfaceExecuteStreamHandler- Parameters:
is- theInputStream.
-
setProcessErrorStream
public void setProcessErrorStream(java.io.InputStream is)
Set theInputStreamfrom which to read the standard error of the process.- Specified by:
setProcessErrorStreamin interfaceExecuteStreamHandler- Parameters:
is- theInputStream.
-
setProcessInputStream
public void setProcessInputStream(java.io.OutputStream os)
Set theOutputStreamby means of which input can be sent to the process.- Specified by:
setProcessInputStreamin interfaceExecuteStreamHandler- Parameters:
os- theOutputStream.
-
start
public void start()
Start theThreads.- Specified by:
startin interfaceExecuteStreamHandler
-
stop
public void stop() throws java.io.IOExceptionStop pumping the streams. When a timeout is specified it it is not guaranteed that the pumper threads are cleanly terminated.- Specified by:
stopin interfaceExecuteStreamHandler- Throws:
java.io.IOException- thrown when an I/O exception occurs.
-
getErr
protected java.io.OutputStream getErr()
Get the error stream.- Returns:
OutputStream.
-
getOut
protected java.io.OutputStream getOut()
Get the output stream.- Returns:
OutputStream.
-
createProcessOutputPump
protected void createProcessOutputPump(java.io.InputStream is, java.io.OutputStream os)Create the pump to handle process output.- Parameters:
is- theInputStream.os- theOutputStream.
-
createProcessErrorPump
protected void createProcessErrorPump(java.io.InputStream is, java.io.OutputStream os)Create the pump to handle error output.- Parameters:
is- theInputStream.os- theOutputStream.
-
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. 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 fromos- 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 fromos- the output stream to copy intocloseWhenExhausted- close the output stream when the input stream is exhausted- Returns:
- the stream pumper thread
-
stopThread
protected void stopThread(java.lang.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 stoppedtimeout- the time in ms to wait to join
-
createSystemInPump
private 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 fromos- the output stream to copy into- Returns:
- the stream pumper thread
-
-