Class NonBlockingInputStreamImpl

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

    public class NonBlockingInputStreamImpl
    extends NonBlockingInputStream
    This class wraps a regular input stream and allows it to appear as if it is non-blocking; that is, reads can be performed against it that timeout if no data is seen for a period of time. This effect is achieved by having a separate thread perform all non-blocking read requests and then waiting on the thread to complete.

    VERY IMPORTANT NOTES

    • This class is not thread safe. It expects at most one reader.
    • The shutdown() method must be called in order to shut down the thread that handles blocking I/O.
    • Constructor Summary

      Constructors 
      Constructor Description
      NonBlockingInputStreamImpl​(java.lang.String name, java.io.InputStream in)
      Creates a NonBlockingReader out of a normal blocking reader.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      int read​(long timeout, boolean isPeek)
      Attempts to read a byte from the input stream for a specific period of time.
      void shutdown()
      Shuts down the thread that is handling blocking I/O.
      • Methods inherited from class java.io.InputStream

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

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

      • NonBlockingInputStreamImpl

        public NonBlockingInputStreamImpl​(java.lang.String name,
                                          java.io.InputStream in)
        Creates a NonBlockingReader out of a normal blocking reader. Note that this call also spawn a separate thread to perform the blocking I/O on behalf of the thread that is using this class. The shutdown() method must be called in order to shut this thread down.
        Parameters:
        name - The stream name
        in - The reader to wrap
    • Method Detail

      • shutdown

        public void shutdown()
        Shuts down the thread that is handling blocking I/O. Note that if the thread is currently blocked waiting for I/O it will not actually shut down until the I/O is received.
        Overrides:
        shutdown in class NonBlockingInputStream
      • 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​(long timeout,
                        boolean isPeek)
                 throws java.io.IOException
        Attempts to read a byte from the input stream for a specific period of time.
        Specified by:
        read in class NonBlockingInputStream
        Parameters:
        timeout - The amount of time to wait for the character
        isPeek - trueif the byte read must not be consumed
        Returns:
        The byte read, -1 if EOF is reached, or -2 if the read timed out.
        Throws:
        java.io.IOException - if anything wrong happens