Class NonBlockingReader
- All Implemented Interfaces:
Closeable, AutoCloseable, Readable
- Direct Known Subclasses:
NonBlockingPumpReader, NonBlockingReaderImpl
The NonBlockingReader class extends the standard Reader class to provide non-blocking read operations. Unlike standard readers, which block until data is available or the end of the stream is reached, non-blocking readers can be configured to return immediately or after a specified timeout if no data is available.
This class is particularly useful for terminal applications that need to perform other tasks while waiting for user input, or that need to implement features like input timeouts or polling.
The class defines two special return values:
EOF(-1) - Indicates that the end of the stream has been reachedREAD_EXPIRED(-2) - Indicates that the read operation timed out
Implementations of this class typically use a separate thread to handle
blocking I/O operations, allowing the main thread to continue execution.
The shutdown() method can be used to terminate this background
thread when it is no longer needed.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanFlag indicating whether this reader has been closed.static final intstatic final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintprotected voidChecks if this reader has been closed.voidclose()Closes this reader and marks it as closed.intpeek(long timeout) Peeks to see if there is a byte waiting in the input stream without actually consuming the byte.intread()intread(char[] b, int off, int len) This version of read() is very specific to jline's purposes, it will always always return a single byte at a time, rather than filling the entire buffer.intread(long timeout) Attempts to read a character from the input stream for a specific period of time.protected abstract intread(long timeout, boolean isPeek) Attempts to read a character from the input stream for a specific period of time.intreadBuffered(char[] b) abstract intreadBuffered(char[] b, int off, int len, long timeout) intreadBuffered(char[] b, long timeout) voidshutdown()Shuts down the thread that is handling blocking I/O.Methods inherited from class Reader
mark, markSupported, nullReader, of, read, read, readAllAsString, readAllLines, ready, reset, skip, transferTo
-
Field Details
-
EOF
public static final int EOF- See Also:
-
READ_EXPIRED
public static final int READ_EXPIRED- See Also:
-
closed
protected volatile boolean closedFlag indicating whether this reader has been closed. Marked as volatile to ensure visibility across threads.
-
-
Constructor Details
-
NonBlockingReader
public NonBlockingReader()Default constructor. Initializes close mode based on the current value of the system property.
-
-
Method Details
-
checkClosed
Checks if this reader has been closed.In JLine 3.x, warn mode is enabled by default: when a closed reader is accessed, it logs a WARNING instead of throwing an exception. This allows existing code to continue working while alerting developers to the issue.
The behavior can be controlled via the system property
PROP_CLOSE_MODE:"strict"- ThrowClosedException"warn"- Log a warning but continue (default in JLine 3.x)"lenient"- Silently allow access (no warning, no exception)
- Throws:
ClosedException- if this reader has been closed and strict mode is enabledIOException
-
shutdown
public void shutdown()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.
-
read
- Overrides:
readin classReader- Throws:
IOException
-
peek
Peeks to see if there is a byte waiting in the input stream without actually consuming the byte.- Parameters:
timeout- The amount of time to wait, 0 == forever- Returns:
- -1 on eof, -2 if the timeout expired with no available input or the character that was read (without consuming it).
- Throws:
IOException- if anything wrong happens
-
read
Attempts to read a character from the input stream for a specific period of time.- Parameters:
timeout- The amount of time to wait for the character- Returns:
- The character read, -1 if EOF is reached, or -2 if the read timed out.
- Throws:
IOException- if anything wrong happens
-
read
This version of read() is very specific to jline's purposes, it will always always return a single byte at a time, rather than filling the entire buffer.- Specified by:
readin classReader- Parameters:
b- the bufferoff- the offset in the bufferlen- the maximum number of chars to read- Throws:
IOException- if anything wrong happens
-
readBuffered
- Throws:
IOException
-
readBuffered
- Throws:
IOException
-
readBuffered
- Throws:
IOException
-
available
public int available() -
read
Attempts to read a character from the input stream for a specific period of time.- 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
-
close
Closes this reader and marks it as closed.Subsequent read operations behavior depends on the
PROP_CLOSE_MODEsetting:"strict"- ThrowClosedException"warn"- Log a warning but continue (default in JLine 3.x)"lenient"- Silently allow access
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein classReader- Throws:
IOException- if an I/O error occurs
-