Class StreamGobbler

  • All Implemented Interfaces:
    Runnable

    public class StreamGobbler
    extends 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 .
    • Constructor Detail

      • StreamGobbler

        public StreamGobbler()
        Basic constructor for StreamGobbler.
      • StreamGobbler

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

        public StreamGobbler​(InputStream in,
                             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​(InputStream in,
                             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

      • 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 Runnable
        Overrides:
        run in class Thread