Class NonBlockingReaderImpl
- All Implemented Interfaces:
Closeable, AutoCloseable, Readable
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
FieldsFields inherited from class NonBlockingReader
closed, EOF -
Constructor Summary
ConstructorsConstructorDescriptionNonBlockingReaderImpl(String name, Reader in) Creates aNonBlockingReaderout of a normal blocking reader. -
Method Summary
Modifier and TypeMethodDescriptionintReturns an estimate of the number of characters available to be read without blocking.voidclear()voidclose()Closes this reader and marks it as closed.protected intread(long timeout, boolean isPeek) Attempts to read a character from the input stream for a specific period of time.intreadBuffered(char[] b, int off, int len, long timeout) booleanready()voidshutdown()Shuts down the thread that is handling blocking I/O.Methods inherited from class NonBlockingReader
checkClosed, peek, read, read, read, readBuffered, readBufferedMethods inherited from class Reader
mark, markSupported, nullReader, of, read, read, readAllAsString, readAllLines, reset, skip, transferTo
-
Field Details
-
READ_EXPIRED
public static final int READ_EXPIRED- See Also:
-
-
Constructor Details
-
NonBlockingReaderImpl
Creates aNonBlockingReaderout 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. Theshutdown()method must be called in order to shut this thread down.- Parameters:
name- The reader namein- The reader to wrap
-
-
Method Details
-
shutdown
public void shutdown()Description copied from class:NonBlockingReaderShuts 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:
shutdownin classNonBlockingReader
-
close
Description copied from class:NonBlockingReaderCloses this reader and marks it as closed.Subsequent read operations behavior depends on the
PROP_CLOSE_MODEsetting:"strict"- ThrowClosedException(default in JLine 4.x)"warn"- Log a warning but continue (default in JLine 3.x)"lenient"- Silently allow access
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classNonBlockingReader- Throws:
IOException- if an I/O error occurs
-
ready
- Overrides:
readyin classReader- Throws:
IOException
-
readBuffered
- Specified by:
readBufferedin classNonBlockingReader- Throws:
IOException
-
read
Attempts to read a character from the input stream for a specific period of time.- Specified by:
readin classNonBlockingReader- Parameters:
timeout- The amount of time to wait for the characterisPeek-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:
IOException- if anything wrong happens
-
available
public int available()Returns an estimate of the number of characters available to be read without blocking.Returns
1if a character has already been buffered by the background pump thread, or if the underlying reader reportsready(). Returns0at EOF, when the pump is actively reading (to avoid contention on the underlying reader's lock), or when the underlying reader is not ready. BecauseReader.ready()returns aboolean, the result is always either0or1? callers should not rely on this for a precise character count, only as a "data may be available" signal consistent with theInputStream.available()contract.- Overrides:
availablein classNonBlockingReader- Returns:
1if data is likely available,0otherwise
-
clear
- Throws:
IOException
-