Package org.jline.utils
Class NonBlockingInputStreamImpl
java.lang.Object
java.io.InputStream
org.jline.utils.NonBlockingInputStream
org.jline.utils.NonBlockingInputStreamImpl
- All Implemented Interfaces:
Closeable
,AutoCloseable
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
FieldsModifier and TypeFieldDescriptionprivate int
private IOException
private InputStream
private String
private Thread
private long
private boolean
Fields inherited from class org.jline.utils.NonBlockingInputStream
EOF, READ_EXPIRED
-
Constructor Summary
ConstructorsConstructorDescriptionNonBlockingInputStreamImpl
(String name, InputStream in) Creates aNonBlockingReader
out of a normal blocking reader. -
Method Summary
Methods inherited from class org.jline.utils.NonBlockingInputStream
peek, read, read, read, readBuffered
Methods inherited from class java.io.InputStream
available, mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, skip, skipNBytes, transferTo
-
Field Details
-
in
-
b
private int b -
name
-
threadIsReading
private boolean threadIsReading -
exception
-
threadDelay
private long threadDelay -
thread
-
-
Constructor Details
-
NonBlockingInputStreamImpl
Creates aNonBlockingReader
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. Theshutdown()
method must be called in order to shut this thread down.- Parameters:
name
- The stream namein
- The reader to wrap
-
-
Method Details
-
startReadingThreadIfNeeded
private void startReadingThreadIfNeeded() -
shutdown
public void shutdown()Shuts down the thread that is handling blocking I/O. Note that if the thread is currently blocked waiting for I/O it will not actually shut down until the I/O is received.- Overrides:
shutdown
in classNonBlockingInputStream
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- Throws:
IOException
-
read
Attempts to read a byte from the input stream for a specific period of time.- Specified by:
read
in classNonBlockingInputStream
- Parameters:
timeout
- The amount of time to wait for the characterisPeek
-true
if the byte read must not be consumed- Returns:
- The byte read, -1 if EOF is reached, or -2 if the read timed out.
- Throws:
IOException
- if anything wrong happens
-
run
private void run()
-