public class ProcessStreamGobbler
extends java.lang.Object
Process in separate threads,
so that the OS will not block out the JVM, as it could otherwise happen if lines are read from these streams sequentially in one thread.
This class is only useful if the external process's output is either not interesting at all for the user, or if the output should be read only after the process has terminated. Otherwise the streams should be read directly without using this class.
The gobble(long, TimeUnit) call returns either when both stdout and stderr streams deliver a null,
or if the timeout is reached. @TODO check if relying on the null from both streams is
as robust as Process.waitFor().
| Constructor and Description |
|---|
ProcessStreamGobbler(java.lang.Process proc,
java.util.concurrent.ThreadFactory tf,
boolean storeStreamContent) |
| Modifier and Type | Method and Description |
|---|---|
java.util.List<java.lang.String> |
getStderr()
Returns the stderr data read, or null if storeStreamContent==false
|
java.util.List<java.lang.String> |
getStdout()
Returns the stdout data read, or null if storeStreamContent==false
|
boolean |
gobble(long timeout,
java.util.concurrent.TimeUnit unit)
Starts fetching process output from stdout/stderr, storing it internally
so that it can later be read via
getStdout() and getStderr(). |
void |
gobbleAsync()
Starts fetching process output from stdout/stderr, storing it internally
so that it can later be read via
getStdout() and getStderr(). |
boolean |
hasStreamReadErrors() |
boolean |
hasTerminated() |
void |
setDebug(boolean DEBUG) |
public ProcessStreamGobbler(java.lang.Process proc,
java.util.concurrent.ThreadFactory tf,
boolean storeStreamContent)
proc - The process whose streams we want to read.tf - ThreadFactory to be used to create the two threads that read the stdout and stderr streamsstoreStreamContent - If true, then the stdout and stderr content will be stored
and can be read using getStdout() and getStderr()public void gobbleAsync()
getStdout() and getStderr().
Use this method if you do not want to wait for the process to end.
The status can anyway be checked using hasTerminated().
public boolean gobble(long timeout,
java.util.concurrent.TimeUnit unit)
throws java.lang.InterruptedException
getStdout() and getStderr().
Use this method if you want to wait for the process to end, limited by a timeout.
timeout - maximum time to wait for the process to finishunit - unit for the timeouttrue if returning because the process ended,
otherwise false if the timeout applied (in which case the streams will continue to be read though).java.lang.InterruptedExceptionpublic java.util.List<java.lang.String> getStdout()
public java.util.List<java.lang.String> getStderr()
public boolean hasStreamReadErrors()
java.lang.IllegalStateException - if called before gobble(long, TimeUnit) or gobbleAsync().public boolean hasTerminated()
public void setDebug(boolean DEBUG)