Class StreamGobbler

  • All Implemented Interfaces:
    java.lang.Runnable

    public class StreamGobbler
    extends java.lang.Thread

    Captures the output of an InputStream.

    With acknowledgements to Michael C. Daconta, author of "Java Pitfalls, Time Saving Solutions, and Workarounds to Improve Programs." and his article in JavaWorld "When Runtime.exec() Won't". See the ExecRunner class for a reference implementation.
    Author:
    Scott McCrory .
    • Nested Class Summary

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

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

      • Fields inherited from class java.lang.Thread

        MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
    • Constructor Summary

      Constructors 
      Constructor Description
      StreamGobbler()
      Basic constructor for StreamGobbler.
      StreamGobbler​(java.io.InputStream in)
      A simpler constructor for StreamGobbler - defaults to System.out
      StreamGobbler​(java.io.InputStream in, java.io.OutputStream out)
      A more explicit constructor for StreamGobbler where you can tell it exactly where to relay the output to.
      StreamGobbler​(java.io.InputStream in, java.io.PrintWriter pwOut)
      A more explicit constructor for StreamGobbler where you can tell it exactly where to relay the output to.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Object clone()
      We override the clone method here to prevent cloning of our class.
      void quit()
      Tells the StreamGobbler to quit it's operation.
      void run()
      Gobbles up all the stuff coming from the InputStream and sends it to the OutputStream specified during object construction.
      • Methods inherited from class java.lang.Thread

        activeCount, checkAccess, 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, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • StreamGobbler

        public StreamGobbler()
        Basic constructor for StreamGobbler.
      • StreamGobbler

        public StreamGobbler​(java.io.InputStream in)
        A simpler constructor for StreamGobbler - defaults to System.out
        Parameters:
        in - InputStream
      • StreamGobbler

        public StreamGobbler​(java.io.InputStream in,
                             java.io.OutputStream out)
        A more explicit constructor for StreamGobbler where you can tell it exactly where to relay the output to. Creation date: (9/23/2001 8:48:01 PM)
        Parameters:
        in - InputStream
        out - OutputStream
      • StreamGobbler

        public StreamGobbler​(java.io.InputStream in,
                             java.io.PrintWriter pwOut)
        A more explicit constructor for StreamGobbler where you can tell it exactly where to relay the output to. Creation date: (9/23/2001 8:48:01 PM)
        Parameters:
        in - InputStream
        pwOut - PrintWriter
    • Method Detail

      • clone

        public final java.lang.Object clone()
                                     throws java.lang.CloneNotSupportedException
        We override the clone method here to prevent cloning of our class.
        Overrides:
        clone in class java.lang.Thread
        Returns:
        Nothing ever really returned since we throw a CloneNotSupportedException
        Throws:
        java.lang.CloneNotSupportedException - To indicate cloning is not allowed
      • quit

        public void quit()
        Tells the StreamGobbler to quit it's operation. This is safer than using stop() since it uses a semaphore checked in the main wait loop instead of possibly forcing semaphores to untimely unlock.
      • run

        public void run()
        Gobbles up all the stuff coming from the InputStream and sends it to the OutputStream specified during object construction.
        Specified by:
        run in interface java.lang.Runnable
        Overrides:
        run in class java.lang.Thread