Class NonBlockingInputStream
- All Implemented Interfaces:
Closeable, AutoCloseable
- Direct Known Subclasses:
NonBlockingInputStreamImpl, NonBlockingPumpInputStream
The NonBlockingInputStream class extends InputStream to provide non-blocking read operations. Unlike standard input streams, which block indefinitely until data is available or the end of the stream is reached, non-blocking input streams can be configured to return immediately or after a specified timeout if no data is available.
This 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
This abstract class provides the framework for non-blocking input operations, with concrete implementations handling the details of how the non-blocking behavior is achieved (e.g., through NIO, separate threads, or native methods).
Non-blocking input streams are 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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanFlag indicating whether this input stream has been closed.static final intstatic final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidChecks if this input stream has been closed.voidclose()Closes this input stream 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()Reads the next byte of data from the input stream.intread(byte[] b, int off, int len) intread(long timeout) Attempts to read a character from the input stream for a specific period of time.abstract intread(long timeout, boolean isPeek) intreadBuffered(byte[] b) intreadBuffered(byte[] b, int off, int len, long timeout) intreadBuffered(byte[] b, long timeout) voidshutdown()Shuts down the thread that is handling blocking I/O if any.Methods inherited from class InputStream
available, mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, skip, skipNBytes, 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 input stream has been closed. Marked as volatile to ensure visibility across threads.
-
-
Constructor Details
-
NonBlockingInputStream
public NonBlockingInputStream()Default constructor. Initializes close mode based on the current value of the system property.
-
-
Method Details
-
checkClosed
Checks if this input stream has been closed.In JLine 3.x, warn mode is enabled by default: when a closed input stream 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 input stream has been closed and strict mode is enabledIOException
-
read
Reads the next byte of data from the input stream. The value byte is returned as anintin the range0to255. If no byte is available because the end of the stream has been reached, the value-1is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.- Specified by:
readin classInputStream- Returns:
- the next byte of data, or
-1if the end of the stream is reached. - Throws:
IOException- if an I/O error occurs.
-
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 an I/O error occurs.
-
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 an I/O error occurs.
-
read
- Overrides:
readin classInputStream- Throws:
IOException
-
readBuffered
- Throws:
IOException
-
readBuffered
- Throws:
IOException
-
readBuffered
- Throws:
IOException
-
shutdown
public void shutdown()Shuts down the thread that is handling blocking I/O if any. Note that if the thread is currently blocked waiting for I/O it may not actually shut down until the I/O is received. -
read
- Throws:
IOException
-
close
Closes this input stream 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- Overrides:
closein classInputStream- Throws:
IOException- if an I/O error occurs
-