Class NonBlockingReaderImpl

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

    public class NonBlockingReaderImpl
    extends NonBlockingReader
    This class wraps a regular reader 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.
    Since:
    2.7
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int READ_EXPIRED  
      • Fields inherited from class java.io.Reader

        lock
    • Constructor Summary

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

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int available()
      Returns an estimate of the number of characters available to be read without blocking.
      void clear()  
      void close()
      Closes this reader and marks it as closed.
      protected int read​(long timeout, boolean isPeek)
      Attempts to read a character from the input stream for a specific period of time.
      int readBuffered​(char[] b, int off, int len, long timeout)  
      boolean ready()  
      void shutdown()
      Shuts down the thread that is handling blocking I/O.
      • Methods inherited from class java.io.Reader

        mark, markSupported, nullReader, read, read, reset, skip, transferTo
      • Methods inherited from class java.lang.Object

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

      • NonBlockingReaderImpl

        public NonBlockingReaderImpl​(java.lang.String name,
                                     java.io.Reader 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 reader name
        in - The reader to wrap
    • Method Detail

      • shutdown

        public void shutdown()
        Description copied from class: NonBlockingReader
        Shuts down the thread that is handling blocking I/O.

        This method terminates the background thread that is used to handle blocking I/O operations. This allows the application to clean up resources and prevent thread leaks when the reader is no longer needed.

        Note that if the thread is currently blocked waiting for I/O, it will not actually shut down until the I/O is received or the thread is interrupted. In some implementations, this method may interrupt the thread to force it to shut down immediately.

        After calling this method, the reader should not be used anymore, as subsequent read operations may fail or block indefinitely.

        Overrides:
        shutdown in class NonBlockingReader
      • close

        public void close()
                   throws java.io.IOException
        Description copied from class: NonBlockingReader
        Closes this reader and marks it as closed.

        Subsequent read operations behavior depends on the PROP_CLOSE_MODE setting:

        • "strict" - Throw ClosedException (default in JLine 4.x)
        • "warn" - Log a warning but continue (default in JLine 3.x)
        • "lenient" - Silently allow access
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class NonBlockingReader
        Throws:
        java.io.IOException - if an I/O error occurs
      • ready

        public boolean ready()
                      throws java.io.IOException
        Overrides:
        ready in class java.io.Reader
        Throws:
        java.io.IOException
      • readBuffered

        public int readBuffered​(char[] b,
                                int off,
                                int len,
                                long timeout)
                         throws java.io.IOException
        Specified by:
        readBuffered in class NonBlockingReader
        Throws:
        java.io.IOException
      • read

        protected int read​(long timeout,
                           boolean isPeek)
                    throws java.io.IOException
        Attempts to read a character from the input stream for a specific period of time.
        Specified by:
        read in class NonBlockingReader
        Parameters:
        timeout - The amount of time to wait for the character
        isPeek - trueif the character read must not be consumed
        Returns:
        The character read, -1 if EOF is reached, or -2 if the read timed out.
        Throws:
        java.io.IOException - if anything wrong happens
      • available

        public int available()
        Returns an estimate of the number of characters available to be read without blocking.

        Returns 1 if a character has already been buffered by the background pump thread, or if the underlying reader reports ready(). Returns 0 at EOF, when the pump is actively reading (to avoid contention on the underlying reader's lock), or when the underlying reader is not ready. Because Reader.ready() returns a boolean, the result is always either 0 or 1 ? callers should not rely on this for a precise character count, only as a "data may be available" signal consistent with the InputStream.available() contract.

        Overrides:
        available in class NonBlockingReader
        Returns:
        1 if data is likely available, 0 otherwise
      • clear

        public void clear()
                   throws java.io.IOException
        Throws:
        java.io.IOException