Package com.seeq.utilities.process
Class StreamGobbler
java.lang.Object
java.lang.Thread
com.seeq.utilities.process.StreamGobbler
- All Implemented Interfaces:
Runnable
Consumes lines of text that emerge from an InputStream object on a separate thread from the caller, which is
necessary for proper handling of spawned processes.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
Callback interface.Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
-
Field Summary
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
-
Constructor Summary
ConstructorsConstructorDescriptionStreamGobbler
(InputStream inputStream) Allocates a StreamGobbler object and pointed to the specified input stream. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Clears the internal text buffer.Returns the callback that is used when a line of text is received on the stream.getText()
Returns the gobbled text.boolean
True if all output and error content should be captured in a string and accessible via the getText() accessor.void
run()
Gobbles text from the specified inputStream.void
setCaptureTextEnabled
(boolean captureTextEnabled) Controls whether output and error content should be captured in a string and accessible via the getText() accessor.void
setLineReceivedCallback
(StreamGobbler.LineReceivedCallback lineReceivedCallback) Sets the callback that is used when a line of text is received on the stream.void
waitFor()
Waits for the input stream to close and all gobbled text to be collected.void
waitFor
(long timeout) Waits for the input stream to close and all gobbled text to be collected.Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield
-
Constructor Details
-
StreamGobbler
Allocates a StreamGobbler object and pointed to the specified input stream.- Parameters:
inputStream
- The input stream to be "gobbled"
-
-
Method Details
-
run
public void run()Gobbles text from the specified inputStream. This function should not be called directly, use start() instead so that a thread is spawned. -
isCaptureTextEnabled
public boolean isCaptureTextEnabled()True if all output and error content should be captured in a string and accessible via the getText() accessor.- Returns:
- true if output and error content is being captured
-
setCaptureTextEnabled
public void setCaptureTextEnabled(boolean captureTextEnabled) Controls whether output and error content should be captured in a string and accessible via the getText() accessor.- Parameters:
captureTextEnabled
- true if output and error content should be captured
-
getText
Returns the gobbled text. Note that this is ALL text received since the last call to clearText().- Returns:
- the gobbled text
-
clearText
public void clearText()Clears the internal text buffer. -
getLineReceivedCallback
Returns the callback that is used when a line of text is received on the stream.- Returns:
- the callback that is used when a line of text is received on the stream
-
setLineReceivedCallback
Sets the callback that is used when a line of text is received on the stream.- Parameters:
lineReceivedCallback
- the callback that is used when a line of text is received on the stream
-
waitFor
Waits for the input stream to close and all gobbled text to be collected.- Throws:
InterruptedException
- If the thread was interrupted by an exception.
-
waitFor
Waits for the input stream to close and all gobbled text to be collected.- Parameters:
timeout
- time to wait (in milliseconds)- Throws:
InterruptedException
- If the thread was interrupted by an exception.
-