Class StreamGobbler

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class StreamGobbler
    extends java.io.InputStream
    A StreamGobbler is an InputStream that uses an internal worker thread to constantly consume input from another InputStream. It uses a buffer to store the consumed data. The buffer size is automatically adjusted, if needed.

    This class is sometimes very convenient - if you wrap a session's STDOUT and STDERR InputStreams with instances of this class, then you don't have to bother about the shared window of STDOUT and STDERR in the low level SSH-2 protocol, since all arriving data will be immediatelly consumed by the worker threads. Also, as a side effect, the streams will be buffered (e.g., single byte read() operations are faster).

    Other SSH for Java libraries include this functionality by default in their STDOUT and STDERR InputStream implementations, however, please be aware that this approach has also a downside:

    If you do not call the StreamGobbler's read() method often enough and the peer is constantly sending huge amounts of data, then you will sooner or later encounter a low memory situation due to the aggregated data (well, it also depends on the Java heap size). Joe Average will like this class anyway - a paranoid programmer would never use such an approach.

    The term "StreamGobbler" was taken from an article called "When Runtime.exec() won't", see http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html.

    Version:
    $Id: StreamGobbler.java,v 1.1 2007/10/15 12:49:56 cplattne Exp $
    • Constructor Summary

      Constructors 
      Constructor Description
      StreamGobbler​(java.io.InputStream is)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int available()  
      void close()  
      int read()  
      int read​(byte[] b)  
      int read​(byte[] b, int off, int len)  
      • Methods inherited from class java.io.InputStream

        mark, markSupported, nullInputStream, readAllBytes, readNBytes, readNBytes, reset, skip, transferTo
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • is

        private java.io.InputStream is
      • synchronizer

        private final java.lang.Object synchronizer
      • isEOF

        private boolean isEOF
      • isClosed

        private boolean isClosed
      • exception

        private java.io.IOException exception
      • buffer

        private byte[] buffer
      • read_pos

        private int read_pos
      • write_pos

        private int write_pos
    • Constructor Detail

      • StreamGobbler

        public StreamGobbler​(java.io.InputStream is)
    • Method Detail

      • read

        public int read()
                 throws java.io.IOException
        Specified by:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • available

        public int available()
                      throws java.io.IOException
        Overrides:
        available in class java.io.InputStream
        Throws:
        java.io.IOException
      • read

        public int read​(byte[] b)
                 throws java.io.IOException
        Overrides:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.InputStream
        Throws:
        java.io.IOException
      • read

        public int read​(byte[] b,
                        int off,
                        int len)
                 throws java.io.IOException
        Overrides:
        read in class java.io.InputStream
        Throws:
        java.io.IOException