Interface Connection

All Superinterfaces:
Appendable, AutoCloseable
All Known Implementing Classes:
AbstractConnection

public interface Connection extends Appendable, AutoCloseable
Represent a connection to either a local/direct/remote Terminal.

This interface provides core I/O operations: input/output handlers, encoding, lifecycle (open/close), and terminal state (attributes, size).

Advanced terminal features (queries, capability detection, semantic output, mode toggles) are accessible via terminal(), which returns a TerminalFeatures object.

Author:
Ståle W. Pedersen
  • Method Details

    • device

      Device device()
      Get the device associated with this connection.
      Returns:
      type of terminal
    • size

      Size size()
      Get the current terminal size.
      Returns:
      terminal size
    • stdinHandler

      Consumer<int[]> stdinHandler()
      Get the standard input handler.
      Returns:
      the stdin handler that processes input as code point arrays
    • setStdinHandler

      void setStdinHandler(Consumer<int[]> handler)
      Set the standard input handler.
      Parameters:
      handler - the handler to process input as code point arrays
    • stdoutHandler

      Consumer<int[]> stdoutHandler()
      Handler that's called for all output.
      Returns:
      output handler
    • write

      default Connection write(String s)
      Write a string to the output handler. When the stdout handler is an Encoder, this uses a fast path that encodes the String directly to bytes without an intermediate int[] allocation.
      Parameters:
      s - string
      Returns:
      this connection
    • put

      boolean put(Capability capability, Object... params)
      Specify terminal settings.
      Parameters:
      capability - capability
      params - parameters
      Returns:
      true if the terminal accepted the settings
    • signalHandler

      Consumer<Signal> signalHandler()
      Get the signal handler.
      Returns:
      handler that's called when a Signal is sent to the terminal
    • setSignalHandler

      void setSignalHandler(Consumer<Signal> handler)
      Specify the signal handler.
      Parameters:
      handler - signal handler
    • sizeHandler

      Consumer<Size> sizeHandler()
      Get the size handler.
      Returns:
      handler that's called when the terminal changes size
    • setSizeHandler

      void setSizeHandler(Consumer<Size> handler)
      Specify size handler that's called when the terminal changes size.
      Parameters:
      handler - the size change handler
    • closeHandler

      Consumer<Void> closeHandler()
      Get the close handler.
      Returns:
      handler that's called when the input stream is closed
    • setCloseHandler

      void setCloseHandler(Consumer<Void> closeHandler)
      Specify handler that's called when the input stream is closed.
      Parameters:
      closeHandler - handler
    • setThemeChangeHandler

      default void setThemeChangeHandler(Consumer<TerminalTheme> handler)
      Set a handler to be called when the terminal's theme changes.
      Parameters:
      handler - the handler to invoke with the new theme, or null to remove
    • themeChangeHandler

      default Consumer<TerminalTheme> themeChangeHandler()
      Get the current theme change handler.
      Returns:
      the theme change handler, or null if not set
    • openBlocking

      void openBlocking()
      Start reading from the input stream using the current thread. The current thread will be blocked while reading/waiting to read from the stream.
    • openNonBlocking

      void openNonBlocking()
      Start reading from the input stream in a separate thread. The current thread will continue.
    • reading

      default boolean reading()
      Check if the connection is actively reading from the input stream.
      Returns:
      true if the connection is actively reading input
    • close

      void close()
      Stop reading from the input stream. The stream will be closed and cleanup methods will be called. Eg for terminals they will be restored to their original settings.
      Specified by:
      close in interface AutoCloseable
    • close

      default void close(int exit)
      Close the connection with an exit code.
      Parameters:
      exit - the exit code
    • attributes

      Attributes attributes()
      Get the current terminal attributes.
      Returns:
      the terminal attributes
    • setAttributes

      void setAttributes(Attributes attr)
      Set the terminal attributes.
      Parameters:
      attr - the attributes to set
    • enterRawMode

      default Attributes enterRawMode()
      Enter raw mode for the terminal.

      In raw mode, input is not line-buffered, echo is disabled, and special character processing is turned off.

      Returns:
      the previous terminal attributes (to restore later)
    • supportsAnsi

      boolean supportsAnsi()
      Check if this terminal supports ANSI escape sequences.
      Returns:
      true if ANSI is supported, false otherwise
    • inputEncoding

      Charset inputEncoding()
      Get the input character encoding.
      Returns:
      the charset used for input encoding
    • outputEncoding

      Charset outputEncoding()
      Get the output character encoding.
      Returns:
      the charset used for output encoding
    • append

      default Appendable append(char c) throws IOException
      Specified by:
      append in interface Appendable
      Throws:
      IOException
    • append

      default Appendable append(CharSequence csq) throws IOException
      Specified by:
      append in interface Appendable
      Throws:
      IOException
    • append

      default Appendable append(CharSequence csq, int start, int end) throws IOException
      Specified by:
      append in interface Appendable
      Throws:
      IOException
    • terminal

      default TerminalFeatures terminal()
      Access advanced terminal features: queries, capability detection, semantic output, and mode management.
      Returns:
      the terminal features for this connection
    • asWriter

      default Writer asWriter()
      Return a Writer view of this connection. Writes to the returned Writer are forwarded to this connection's output.
      Returns:
      a Writer that delegates to this connection
    • asPrintWriter

      default PrintWriter asPrintWriter()
      Return a PrintWriter view of this connection. Useful for formatted output (printf, println, etc.).
      Returns:
      a PrintWriter that delegates to this connection