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.
-
-
Constructor Summary
Constructors Constructor Description PumpStreamHandler()Constructs a newPumpStreamHandler.PumpStreamHandler(java.io.OutputStream allOutputStream)Constructs a newPumpStreamHandler.PumpStreamHandler(java.io.OutputStream outputStream, java.io.OutputStream errorOutputStream)Constructs a newPumpStreamHandler.PumpStreamHandler(java.io.OutputStream outputStream, java.io.OutputStream errorOutputStream, java.io.InputStream inputStream)Constructs a newPumpStreamHandler.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated 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.protected java.io.OutputStreamgetErr()Gets the error stream.protected java.io.OutputStreamgetOut()Gets the output stream.voidsetProcessErrorStream(java.io.InputStream is)Sets theInputStreamfrom which to read the standard error of the process.voidsetProcessInputStream(java.io.OutputStream os)Sets theOutputStreamby means of which input can be sent to the process.voidsetProcessOutputStream(java.io.InputStream is)Sets theInputStreamfrom which to read the standard output of the process.voidsetStopTimeout(long timeout)Deprecated.voidsetStopTimeout(java.time.Duration timeout)Sets maximum time to wait until output streams are exhausted whenstop()was called.voidstart()Starts theThreads.voidstop()Stops pumping the streams.protected voidstopThread(java.lang.Thread thread, long timeoutMillis)Stops a pumper thread.
-
-
-
Constructor Detail
-
PumpStreamHandler
public PumpStreamHandler()
Constructs a newPumpStreamHandler.
-
PumpStreamHandler
public PumpStreamHandler(java.io.OutputStream allOutputStream)
Constructs a newPumpStreamHandler.- Parameters:
allOutputStream- the output/errorOutputStream. TheOutputStreamimplementation must be thread-safe because the output and error reader threads will concurrently write to it.
-
PumpStreamHandler
public PumpStreamHandler(java.io.OutputStream outputStream, java.io.OutputStream errorOutputStream)
Constructs a newPumpStreamHandler.If the same
OutputStreaminstance is used for output and error, then it must be thread-safe because the output and error reader threads will concurrently write to it.- Parameters:
outputStream- the outputOutputStream.errorOutputStream- the errorOutputStream.
-
PumpStreamHandler
public PumpStreamHandler(java.io.OutputStream outputStream, java.io.OutputStream errorOutputStream, java.io.InputStream inputStream)
Constructs a newPumpStreamHandler.If the same
OutputStreaminstance is used for output and error, then it must be thread-safe because the output and error reader threads will concurrently write to it.- Parameters:
outputStream- the outputOutputStream.errorOutputStream- the errorOutputStream.inputStream- the inputInputStream.
-
-
Method Detail
-
createProcessErrorPump
protected void createProcessErrorPump(java.io.InputStream is, java.io.OutputStream os)
Create the pump to handle error output.- Parameters:
is- theInputStream.os- theOutputStream.
-
createProcessOutputPump
protected void createProcessOutputPump(java.io.InputStream is, java.io.OutputStream os)
Create the pump to handle process 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' afterward 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 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.
-
getErr
protected java.io.OutputStream getErr()
Gets the error stream.- Returns:
OutputStream.
-
getOut
protected java.io.OutputStream getOut()
Gets the output stream.- Returns:
OutputStream.
-
setProcessErrorStream
public void setProcessErrorStream(java.io.InputStream is)
Sets 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)
Sets theOutputStreamby means of which input can be sent to the process.- Specified by:
setProcessInputStreamin interfaceExecuteStreamHandler- Parameters:
os- theOutputStream.
-
setProcessOutputStream
public void setProcessOutputStream(java.io.InputStream is)
Sets theInputStreamfrom which to read the standard output of the process.- Specified by:
setProcessOutputStreamin interfaceExecuteStreamHandler- Parameters:
is- theInputStream.
-
setStopTimeout
public void setStopTimeout(java.time.Duration timeout)
Sets maximum time to wait until output streams are exhausted whenstop()was called.- Parameters:
timeout- timeout or zero to wait forever (default).- Since:
- 1.4.0
-
setStopTimeout
@Deprecated public void setStopTimeout(long timeout)
Deprecated.Sets maximum time to wait until output streams are exhausted whenstop()was called.- Parameters:
timeout- timeout in milliseconds or zero to wait forever (default).
-
start
public void start()
Starts theThreads.- Specified by:
startin interfaceExecuteStreamHandler
-
stop
public void stop() throws java.io.IOException
Stops pumping the streams. When a timeout is specified 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.
-
stopThread
protected void stopThread(java.lang.Thread thread, long timeoutMillis)
Stops 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.timeoutMillis- the time in ms to wait to join.
-
-