Package org.jline.utils
Class NonBlockingReaderImpl
- java.lang.Object
-
- java.io.Reader
-
- org.jline.utils.NonBlockingReader
-
- org.jline.utils.NonBlockingReaderImpl
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,java.lang.Readable
public class NonBlockingReaderImpl extends NonBlockingReader
This class wraps a regular reader 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.
- Since:
- 2.7
-
-
Field Summary
Fields Modifier and Type Field Description private int
ch
private java.io.IOException
exception
private java.io.Reader
in
private java.lang.String
name
static int
READ_EXPIRED
private java.lang.Thread
thread
private long
threadDelay
private boolean
threadIsReading
-
Fields inherited from class org.jline.utils.NonBlockingReader
EOF
-
-
Constructor Summary
Constructors Constructor Description NonBlockingReaderImpl(java.lang.String name, java.io.Reader in)
Creates aNonBlockingReader
out of a normal blocking reader.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
void
close()
protected int
read(long timeout, boolean isPeek)
Attempts to read a character from the input stream for a specific period of time.int
readBuffered(char[] b, int off, int len, long timeout)
boolean
ready()
private void
run()
void
shutdown()
Shuts down the thread that is handling blocking I/O.private void
startReadingThreadIfNeeded()
-
Methods inherited from class org.jline.utils.NonBlockingReader
available, peek, read, read, read, readBuffered, readBuffered
-
-
-
-
Field Detail
-
READ_EXPIRED
public static final int READ_EXPIRED
- See Also:
- Constant Field Values
-
in
private java.io.Reader in
-
ch
private int ch
-
name
private java.lang.String name
-
threadIsReading
private boolean threadIsReading
-
exception
private java.io.IOException exception
-
threadDelay
private long threadDelay
-
thread
private java.lang.Thread thread
-
-
Constructor Detail
-
NonBlockingReaderImpl
public NonBlockingReaderImpl(java.lang.String name, java.io.Reader in)
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 reader namein
- The reader to wrap
-
-
Method Detail
-
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 classNonBlockingReader
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in classjava.io.Reader
- Throws:
java.io.IOException
-
ready
public boolean ready() throws java.io.IOException
- Overrides:
ready
in classjava.io.Reader
- Throws:
java.io.IOException
-
readBuffered
public int readBuffered(char[] b, int off, int len, long timeout) throws java.io.IOException
- Specified by:
readBuffered
in classNonBlockingReader
- Throws:
java.io.IOException
-
read
protected int read(long timeout, boolean isPeek) throws java.io.IOException
Attempts to read a character from the input stream for a specific period of time.- Specified by:
read
in classNonBlockingReader
- Parameters:
timeout
- The amount of time to wait for the characterisPeek
-true
if the character read must not be consumed- Returns:
- The character read, -1 if EOF is reached, or -2 if the read timed out.
- Throws:
java.io.IOException
- if anything wrong happens
-
run
private void run()
-
clear
public void clear() throws java.io.IOException
- Throws:
java.io.IOException
-
-