Package org.aesh.terminal.utils
Class InputReader
java.lang.Object
java.io.Reader
org.aesh.terminal.utils.InputReader
- All Implemented Interfaces:
Closeable,AutoCloseable,Readable
A
Reader adapter that bridges a terminal's push-style input
to Java's pull-style Reader.
Incoming code points are converted to chars via the push methods,
with supplementary code points (above U+FFFF) split into surrogate pairs.
Usage example:
InputReader reader = InputReader.asReader(connection);
// now use reader.read(...) or reader.readCodePoint(...)
- Author:
- Ståle W. Pedersen
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreate an InputReader with the default queue capacity (4096).InputReader(int capacity) Create an InputReader with a custom queue capacity. -
Method Summary
Modifier and TypeMethodDescriptionstatic InputReaderasReader(Connection connection) Create an InputReader and wire it to the given connection's stdin handler.static InputReaderasReader(Connection connection, int capacity) Create an InputReader with a custom queue capacity and wire it to the given connection's stdin handler.voidclose()voidpush(char ch) Push a single char into the reader.voidpush(int codePoint) Push a code point into the reader.voidpush(CharSequence csq) Push a character sequence into the reader.intread(char[] cbuf, int off, int len) Reads chars into a portion of a char array.intread(int timeout) Read a single character with a timeout.readCodePoint(long timeout, TimeUnit unit) Read a single code point with a timeout.booleanready()Methods inherited from class java.io.Reader
mark, markSupported, nullReader, read, read, read, reset, skip, transferTo
-
Field Details
-
EOF
public static final int EOF- See Also:
-
TIMEOUT
public static final int TIMEOUT- See Also:
-
-
Constructor Details
-
InputReader
public InputReader()Create an InputReader with the default queue capacity (4096). -
InputReader
public InputReader(int capacity) Create an InputReader with a custom queue capacity.- Parameters:
capacity- the maximum number of chars to buffer
-
-
Method Details
-
asReader
Create an InputReader and wire it to the given connection's stdin handler.- Parameters:
connection- the connection to read from- Returns:
- a new InputReader already receiving input from the connection
-
asReader
Create an InputReader with a custom queue capacity and wire it to the given connection's stdin handler.- Parameters:
connection- the connection to read fromcapacity- the maximum number of chars to buffer- Returns:
- a new InputReader already receiving input from the connection
-
push
public void push(char ch) Push a single char into the reader.- Parameters:
ch- the character to push
-
push
public void push(int codePoint) Push a code point into the reader. Supplementary code points (above U+FFFF) are split into surrogate pairs.- Parameters:
codePoint- the Unicode code point to push
-
push
Push a character sequence into the reader.- Parameters:
csq- the character sequence to push
-
read
Read a single character with a timeout.- Parameters:
timeout- the timeout in milliseconds- Returns:
- the character as an int, or
-2if no input within timeout - Throws:
IOException- if the reader has been closed
-
readCodePoint
Read a single code point with a timeout. If the first char is a high surrogate, the next char is also consumed to form the complete code point.- Parameters:
timeout- the maximum time to waitunit- the time unit- Returns:
- the code point, or
OptionalInt.empty()on timeout - Throws:
IOException- if the reader has been closed
-
ready
- Overrides:
readyin classReader- Throws:
IOException
-
read
Reads chars into a portion of a char array. Blocks on the first char, then drains remaining available chars without blocking.- Specified by:
readin classReader- Throws:
IOException
-
close
public void close()
-