Class InputReader

java.lang.Object
java.io.Reader
org.aesh.terminal.utils.InputReader
All Implemented Interfaces:
Closeable, AutoCloseable, Readable

public class InputReader extends Reader
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 Details

  • 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

      public static InputReader asReader(Connection connection)
      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

      public static InputReader asReader(Connection connection, int capacity)
      Create an InputReader with a custom queue capacity and wire it to the given connection's stdin handler.
      Parameters:
      connection - the connection to read from
      capacity - 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

      public void push(CharSequence csq)
      Push a character sequence into the reader.
      Parameters:
      csq - the character sequence to push
    • read

      public int read(int timeout) throws IOException
      Read a single character with a timeout.
      Parameters:
      timeout - the timeout in milliseconds
      Returns:
      the character as an int, or -2 if no input within timeout
      Throws:
      IOException - if the reader has been closed
    • readCodePoint

      public OptionalInt readCodePoint(long timeout, TimeUnit unit) throws IOException
      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 wait
      unit - the time unit
      Returns:
      the code point, or OptionalInt.empty() on timeout
      Throws:
      IOException - if the reader has been closed
    • ready

      public boolean ready() throws IOException
      Overrides:
      ready in class Reader
      Throws:
      IOException
    • read

      public int read(char[] cbuf, int off, int len) throws IOException
      Reads chars into a portion of a char array. Blocks on the first char, then drains remaining available chars without blocking.
      Specified by:
      read in class Reader
      Throws:
      IOException
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in class Reader