Package com.norconex.commons.lang.io
Class InputStreamConsumer
- java.lang.Object
-
- java.lang.Thread
-
- com.norconex.commons.lang.io.InputStreamConsumer
-
- All Implemented Interfaces:
Runnable
public class InputStreamConsumer extends Thread
A stream consumer will read all it can from a stream in its own thread. This is often required by some processes/operating systems in order to prevent application freeze. For example, this is a way to capture the STDOUT and STDERR from a process. This class also allows to "listen" to what is being read. Listeners should not be considered thread-safe. If you share a listener between threads, make sure to have unique types to identify each one or the content being streamed from different threads can easily be mixed up in the order sent and is likely not a desired behavior.- Since:
- 1.13.0
- Author:
- Pascal Essiembre
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_CHUNK_SIZE-
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
-
-
Constructor Summary
Constructors Constructor Description InputStreamConsumer(InputStream input)Constructor.InputStreamConsumer(InputStream input, int chunkSize, String type, IInputStreamListener... listeners)Constructor.InputStreamConsumer(InputStream input, String type, IInputStreamListener... listeners)Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidafterStreaming()Invoked just after steaming ended, before the thread dies.protected voidbeforeStreaming()Invoked just before steaming begins, in a new thread.static voidconsume(InputStream input)Convenience method for creasing a consumer instance and starting it.static voidconsume(InputStream input, int chunkSize, String type, IInputStreamListener... listeners)Convenience method for creasing a consumer instance, starting it, and waiting for it to complete.static voidconsume(InputStream input, String type, IInputStreamListener... listeners)Convenience method for creasing a consumer instance and starting it.static voidconsumeAndWait(InputStream input)Convenience method for creasing a consumer instance, starting it, and waiting for it to complete.static voidconsumeAndWait(InputStream input, int chunkSize, String type, IInputStreamListener... listeners)Convenience method for creasing a consumer instance, starting it, and waiting for it to complete.static voidconsumeAndWait(InputStream input, String type, IInputStreamListener... listeners)Convenience method for creasing a consumer instance and starting it.IInputStreamListener[]getStreamListeners()Returns stream listeners.StringgetType()Gets the stream type ornullif no type was set.voidrun()voidstartAndWait()Starts this consumer thread and wait for it to complete before returning.-
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
-
-
-
-
Field Detail
-
DEFAULT_CHUNK_SIZE
public static final int DEFAULT_CHUNK_SIZE
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
InputStreamConsumer
public InputStreamConsumer(InputStream input)
Constructor.- Parameters:
input- input stream
-
InputStreamConsumer
public InputStreamConsumer(InputStream input, String type, IInputStreamListener... listeners)
Constructor.- Parameters:
input- input streamtype- an optional way to identify each portion being read to stream listeners. Useful when listeners are shared. Can benull.listeners- input stream listeners
-
InputStreamConsumer
public InputStreamConsumer(InputStream input, int chunkSize, String type, IInputStreamListener... listeners)
Constructor.- Parameters:
input- input streamchunkSize- how many bytes to read at once before (will also be the maximum byte array size sent to listeners)type- an optional way to identify each portion being read to stream listeners. Useful when listeners are shared. Can benull.listeners- input stream listeners
-
-
Method Detail
-
getStreamListeners
public IInputStreamListener[] getStreamListeners()
Returns stream listeners.- Returns:
- the listeners
-
getType
public String getType()
Gets the stream type ornullif no type was set.- Returns:
- the type
-
beforeStreaming
protected void beforeStreaming()
Invoked just before steaming begins, in a new thread. Default implementation does nothing. This method is for implementors.
-
afterStreaming
protected void afterStreaming()
Invoked just after steaming ended, before the thread dies. Default implementation does nothing. This method is for implementors.
-
startAndWait
public void startAndWait()
Starts this consumer thread and wait for it to complete before returning.- Throws:
StreamException- if streaming is interrupted while waiting
-
consume
public static void consume(InputStream input)
Convenience method for creasing a consumer instance and starting it.- Parameters:
input- input stream to consume.
-
consume
public static void consume(InputStream input, String type, IInputStreamListener... listeners)
Convenience method for creasing a consumer instance and starting it.- Parameters:
input- input streamtype- an optional way to identify each portion being read to stream listeners. Useful when listeners are shared. Can benull.listeners- input stream listeners
-
consume
public static void consume(InputStream input, int chunkSize, String type, IInputStreamListener... listeners)
Convenience method for creasing a consumer instance, starting it, and waiting for it to complete.- Parameters:
input- input streamchunkSize- how many bytes to read at once before (will also be the maximum byte array size sent to listeners)type- an optional way to identify each portion being read to stream listeners. Useful when listeners are shared. Can benull.listeners- input stream listeners
-
consumeAndWait
public static void consumeAndWait(InputStream input)
Convenience method for creasing a consumer instance, starting it, and waiting for it to complete.- Parameters:
input- input stream to consume.
-
consumeAndWait
public static void consumeAndWait(InputStream input, String type, IInputStreamListener... listeners)
Convenience method for creasing a consumer instance and starting it.- Parameters:
input- input streamtype- an optional way to identify each portion being read to stream listeners. Useful when listeners are shared. Can benull.listeners- input stream listeners
-
consumeAndWait
public static void consumeAndWait(InputStream input, int chunkSize, String type, IInputStreamListener... listeners)
Convenience method for creasing a consumer instance, starting it, and waiting for it to complete.- Parameters:
input- input streamchunkSize- how many bytes to read at once before (will also be the maximum byte array size sent to listeners)type- an optional way to identify each portion being read to stream listeners. Useful when listeners are shared. Can benull.listeners- input stream listeners
-
-