Package org.apache.commons.exec
Class PumpStreamHandler
java.lang.Object
org.apache.commons.exec.PumpStreamHandler
- All Implemented Interfaces:
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 $
-
Constructor Summary
ConstructorsConstructorDescriptionConstruct a newPumpStreamHandler
.PumpStreamHandler
(OutputStream outAndErr) Construct a newPumpStreamHandler
.PumpStreamHandler
(OutputStream out, OutputStream err) Construct a newPumpStreamHandler
.PumpStreamHandler
(OutputStream out, OutputStream err, InputStream input) Construct a newPumpStreamHandler
. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Create the pump to handle error output.protected void
Create the pump to handle process output.protected Thread
createPump
(InputStream is, OutputStream os) Creates a stream pumper to copy the given input stream to the given output stream.protected Thread
createPump
(InputStream is, OutputStream os, boolean closeWhenExhausted) Creates a stream pumper to copy the given input stream to the given output stream.protected OutputStream
getErr()
Get the error stream.protected OutputStream
getOut()
Get the output stream.void
Set theInputStream
from which to read the standard error of the process.void
Set theOutputStream
by means of which input can be sent to the process.void
Set theInputStream
from which to read the standard output of the process.void
setStopTimeout
(long timeout) Set maximum time to wait until output streams are exchausted whenstop()
was called.void
start()
Start theThread
s.void
stop()
Stop pumping the streams.protected void
stopThread
(Thread thread, long timeout) Stopping a pumper thread.
-
Constructor Details
-
PumpStreamHandler
public PumpStreamHandler()Construct a newPumpStreamHandler
. -
PumpStreamHandler
Construct a newPumpStreamHandler
.- Parameters:
outAndErr
- the output/errorOutputStream
.
-
PumpStreamHandler
Construct a newPumpStreamHandler
.- Parameters:
out
- the outputOutputStream
.err
- the errorOutputStream
.
-
PumpStreamHandler
Construct a newPumpStreamHandler
.- Parameters:
out
- the outputOutputStream
.err
- the errorOutputStream
.input
- the inputInputStream
.
-
-
Method Details
-
setStopTimeout
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
Set theInputStream
from which to read the standard output of the process.- Specified by:
setProcessOutputStream
in interfaceExecuteStreamHandler
- Parameters:
is
- theInputStream
.
-
setProcessErrorStream
Set theInputStream
from which to read the standard error of the process.- Specified by:
setProcessErrorStream
in interfaceExecuteStreamHandler
- Parameters:
is
- theInputStream
.
-
setProcessInputStream
Set theOutputStream
by means of which input can be sent to the process.- Specified by:
setProcessInputStream
in interfaceExecuteStreamHandler
- Parameters:
os
- theOutputStream
.
-
start
Start theThread
s.- Specified by:
start
in interfaceExecuteStreamHandler
-
stop
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 interfaceExecuteStreamHandler
- Throws:
IOException
- thrown when an I/O exception occurs.
-
getErr
Get the error stream.- Returns:
OutputStream
.
-
getOut
Get the output stream.- Returns:
OutputStream
.
-
createProcessOutputPump
Create the pump to handle process output.- Parameters:
is
- theInputStream
.os
- theOutputStream
.
-
createProcessErrorPump
Create the pump to handle error output.- Parameters:
is
- theInputStream
.os
- theOutputStream
.
-
createPump
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
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
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
-