Class StreamGobbler

  • All Implemented Interfaces:
    java.lang.Runnable

    public class StreamGobbler
    extends java.lang.Thread
    A Stream gobbler class to take care of reading from a stream and optionally write out to another stream. Allows for non blocking reads on both stdout and stderr, when invoking a process through Runtime.exec(). Also, the user can specify a callback that is called whenever anything is read from the stream.
    Author:
    Karan Vahi
    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.lang.Thread

        java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private StreamGobblerCallback mCallback
      The callback to be used.
      private java.io.InputStream mIPStream
      The input stream that is to be read from.
      private LogManager mLogger
      The handle to the logging object.
      private java.io.OutputStream mOPStream
      The output stream to which the contents have to be redirected to.
      private java.lang.String mPrompt
      The prompt that is to be written to the output stream.
      private boolean mStarted
      A boolean indicating whether the thread has started or not.
      • Fields inherited from class java.lang.Thread

        MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes the underneath input and output stream that were opened.
      protected void finalize()
      Closes the open connections to the streams whenever this object is destroyed.
      void redirect​(java.io.OutputStream ops, java.lang.String prompt)
      Sets the output stream to which to redirect the contents of the input stream.
      void run()
      The main method of the gobbler, that does all the work.
      • 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
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • mIPStream

        private java.io.InputStream mIPStream
        The input stream that is to be read from.
      • mOPStream

        private java.io.OutputStream mOPStream
        The output stream to which the contents have to be redirected to.
      • mPrompt

        private java.lang.String mPrompt
        The prompt that is to be written to the output stream.
      • mStarted

        private boolean mStarted
        A boolean indicating whether the thread has started or not.
      • mLogger

        private LogManager mLogger
        The handle to the logging object.
    • Constructor Detail

      • StreamGobbler

        public StreamGobbler​(java.io.InputStream is,
                             StreamGobblerCallback callback)
        The overloaded constructor.
        Parameters:
        is - the input stream from which to read from.
        callback - the callback to call when a line is read.
    • Method Detail

      • redirect

        public void redirect​(java.io.OutputStream ops,
                             java.lang.String prompt)
        Sets the output stream to which to redirect the contents of the input stream.
        Parameters:
        ops - the output stream.
        prompt - the prompt for the output stream.
      • run

        public void run()
        The main method of the gobbler, that does all the work.
        Specified by:
        run in interface java.lang.Runnable
        Overrides:
        run in class java.lang.Thread
      • finalize

        protected void finalize()
        Closes the open connections to the streams whenever this object is destroyed.
        Overrides:
        finalize in class java.lang.Object
      • close

        public void close()
        Closes the underneath input and output stream that were opened.