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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate IOException
The last exception being caught.private final OutputStream
private Thread
private final InputStream
private InputStreamPumper
private Thread
private final OutputStream
private Thread
private static final Duration
private Duration
The timeout Duration the implementation waits when stopping the pumper threads.private final ThreadFactory
The thread factory. -
Constructor Summary
ConstructorsModifierConstructorDescriptionConstructs a newPumpStreamHandler
.PumpStreamHandler
(OutputStream allOutputStream) Constructs a newPumpStreamHandler
.PumpStreamHandler
(OutputStream outputStream, OutputStream errorOutputStream) Constructs a newPumpStreamHandler
.PumpStreamHandler
(OutputStream outputStream, OutputStream errorOutputStream, InputStream inputStream) Constructs a newPumpStreamHandler
.private
PumpStreamHandler
(ThreadFactory threadFactory, OutputStream outputStream, OutputStream errorOutputStream, InputStream inputStream) Constructs 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.private Thread
Creates a stream pumper to copy the given input stream to the given output stream.protected OutputStream
getErr()
Gets the error stream.protected OutputStream
getOut()
Gets the output stream.(package private) Duration
void
Sets theInputStream
from which to read the standard error of the process.void
Sets theOutputStream
by means of which input can be sent to the process.void
Sets theInputStream
from which to read the standard output of the process.void
setStopTimeout
(long timeout) Deprecated.void
setStopTimeout
(Duration timeout) Sets maximum time to wait until output streams are exhausted whenstop()
was called.void
start()
Starts theThread
s.private void
Starts the givenThread
.void
stop()
Stops pumping the streams.private void
Stops a pumper thread.protected void
stopThread
(Thread thread, long timeoutMillis) Stops a pumper thread.
-
Field Details
-
STOP_TIMEOUT_ADDITION
-
outputThread
-
errorThread
-
inputThread
-
outputStream
-
errorOutputStream
-
inputStream
-
inputStreamPumper
-
stopTimeout
The timeout Duration the implementation waits when stopping the pumper threads. -
caught
The last exception being caught. -
threadFactory
The thread factory.
-
-
Constructor Details
-
PumpStreamHandler
public PumpStreamHandler()Constructs a newPumpStreamHandler
. -
PumpStreamHandler
Constructs a newPumpStreamHandler
.- Parameters:
allOutputStream
- the output/errorOutputStream
. TheOutputStream
implementation must be thread-safe because the output and error reader threads will concurrently write to it.
-
PumpStreamHandler
Constructs a newPumpStreamHandler
.If the same
OutputStream
instance 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(OutputStream outputStream, OutputStream errorOutputStream, InputStream inputStream) Constructs a newPumpStreamHandler
.If the same
OutputStream
instance 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
.
-
PumpStreamHandler
private PumpStreamHandler(ThreadFactory threadFactory, OutputStream outputStream, OutputStream errorOutputStream, InputStream inputStream) Constructs a newPumpStreamHandler
.If the same
OutputStream
instance 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 Details
-
createProcessErrorPump
Create the pump to handle error output.- Parameters:
is
- theInputStream
.os
- theOutputStream
.
-
createProcessOutputPump
Create the pump to handle process 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 from.os
- 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 from.os
- the output stream to copy into.closeWhenExhausted
- close the output stream when the input stream is exhausted.- Returns:
- the stream pumper thread.
-
createSystemInPump
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.
-
getErr
Gets the error stream.- Returns:
OutputStream
.
-
getOut
Gets the output stream.- Returns:
OutputStream
.
-
getStopTimeout
Duration getStopTimeout() -
setProcessErrorStream
Sets theInputStream
from which to read the standard error of the process.- Specified by:
setProcessErrorStream
in interfaceExecuteStreamHandler
- Parameters:
is
- theInputStream
.
-
setProcessInputStream
Sets theOutputStream
by means of which input can be sent to the process.- Specified by:
setProcessInputStream
in interfaceExecuteStreamHandler
- Parameters:
os
- theOutputStream
.
-
setProcessOutputStream
Sets theInputStream
from which to read the standard output of the process.- Specified by:
setProcessOutputStream
in interfaceExecuteStreamHandler
- Parameters:
is
- theInputStream
.
-
setStopTimeout
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.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 theThread
s.- Specified by:
start
in interfaceExecuteStreamHandler
-
start
Starts the givenThread
. -
stop
Stops pumping the streams. When a timeout is specified 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.
-
stop
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.timeout
- the time in ms to wait to join.
-
stopThread
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.
-
setStopTimeout(Duration)
.