Class 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
    • 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 stream
        type - an optional way to identify each portion being read to stream listeners. Useful when listeners are shared. Can be null.
        listeners - input stream listeners
      • InputStreamConsumer

        public InputStreamConsumer​(InputStream input,
                                   int chunkSize,
                                   String type,
                                   IInputStreamListener... listeners)
        Constructor.
        Parameters:
        input - input stream
        chunkSize - 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 be null.
        listeners - input stream listeners
    • Method Detail

      • run

        public void run()
        Specified by:
        run in interface Runnable
        Overrides:
        run in class Thread
      • getStreamListeners

        public IInputStreamListener[] getStreamListeners()
        Returns stream listeners.
        Returns:
        the listeners
      • getType

        public String getType()
        Gets the stream type or null if 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 stream
        type - an optional way to identify each portion being read to stream listeners. Useful when listeners are shared. Can be null.
        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 stream
        chunkSize - 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 be null.
        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 stream
        type - an optional way to identify each portion being read to stream listeners. Useful when listeners are shared. Can be null.
        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 stream
        chunkSize - 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 be null.
        listeners - input stream listeners