- java.lang.Object
-
- java.io.InputStream
-
- org.jline.utils.NonBlockingInputStream
-
- org.jline.utils.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.
-
-
Field Summary
-
Fields inherited from class org.jline.utils.NonBlockingInputStream
closed, EOF, READ_EXPIRED
-
-
Constructor Summary
Constructors Constructor Description NonBlockingInputStreamImpl(java.lang.String name, java.io.InputStream in)Creates aNonBlockingInputStreamout of a normal blocking stream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes this input stream and marks it as closed.intread(long timeout, boolean isPeek)Attempts to read a byte from the input stream for a specific period of time.voidsetPollFunction(java.util.function.IntUnaryOperator pollFn)Sets a poll function that checks the underlying file descriptor for input readiness using an OS-level mechanism such aspoll(2).voidshutdown()Shuts down the thread that is handling blocking I/O if any.-
Methods inherited from class org.jline.utils.NonBlockingInputStream
checkClosed, peek, read, read, read, readBuffered, readBuffered, readBuffered
-
-
-
-
Constructor Detail
-
NonBlockingInputStreamImpl
public NonBlockingInputStreamImpl(java.lang.String name, java.io.InputStream in)Creates aNonBlockingInputStreamout of a normal blocking stream. Note that this call also spawns 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 stream namein- The stream to wrap
-
-
Method Detail
-
setPollFunction
public void setPollFunction(java.util.function.IntUnaryOperator pollFn)
Description copied from class:NonBlockingInputStreamSets a poll function that checks the underlying file descriptor for input readiness using an OS-level mechanism such aspoll(2).When set, the pump thread uses short-timeout polls instead of an indefinite blocking read, allowing it to be cancelled promptly when a consumer's timed read expires. This prevents the pump from stealing keystrokes from subprocesses that share the same tty fd (see #2219).
Must be called before the first read. The default implementation is a no-op; subclasses that support poll-based reads should override.
- Overrides:
setPollFunctionin classNonBlockingInputStream- Parameters:
pollFn-(timeoutMs) ? poll result: positive if data is ready, 0 on timeout, negative on error. The fd must be captured by the caller. Passnullto disable.
-
shutdown
public void shutdown()
Description copied from class:NonBlockingInputStreamShuts 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.- Overrides:
shutdownin classNonBlockingInputStream
-
close
public void close() throws java.io.IOExceptionDescription copied from class:NonBlockingInputStreamCloses this input stream 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 interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classNonBlockingInputStream- Throws:
java.io.IOException- if an I/O error occurs
-
read
public int read(long timeout, boolean isPeek) throws java.io.IOExceptionAttempts to read a byte from the input stream for a specific period of time.- Specified by:
readin classNonBlockingInputStream- Parameters:
timeout- The amount of time to wait for the characterisPeek-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
-
-