Interface Buffer

All Known Implementing Classes:
BufferImpl

public interface Buffer
Represents the editable text buffer in the LineReader.

The Buffer interface provides methods for manipulating the text that the user is currently editing in the LineReader. It supports operations such as cursor movement, text insertion and deletion, and content retrieval.

The buffer maintains a current cursor position that indicates where text will be inserted or deleted. Many of the methods in this interface operate relative to this cursor position.

The default implementation is BufferImpl.

See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    atChar(int i)
    Returns the character at the specified position in the buffer.
    boolean
    Deletes the character before the cursor position.
    int
    backspace(int num)
    Deletes multiple characters before the cursor position.
    boolean
    Clears the buffer content.
    Creates a copy of this buffer.
    void
    copyFrom(Buffer buffer)
    Copies the content and cursor position from another buffer.
    int
    Returns the character at the current cursor position.
    boolean
    currChar(int c)
    Replaces the character at the current cursor position.
    int
    Returns the current cursor position in the buffer.
    boolean
    cursor(int position)
    Moves the cursor to the specified position.
    boolean
    Deletes the character at the cursor position.
    int
    delete(int num)
    Deletes multiple characters starting at the cursor position.
    boolean
    Moves the cursor down one line while maintaining the same column position if possible.
    int
    Returns the length of the buffer.
    int
    move(int num)
    Moves the cursor by the specified number of characters.
    boolean
    moveXY(int dx, int dy)
    Moves the cursor by the specified number of columns and rows.
    int
    Returns the character after the current cursor position.
    int
    Returns the character before the current cursor position.
    substring(int start)
    Returns a substring of the buffer from the specified start position to the end.
    substring(int start, int end)
    Returns a substring of the buffer from the specified start position to the specified end position.
     
    boolean
    up()
    Moves the cursor up one line while maintaining the same column position if possible.
    Returns a substring of the buffer from the beginning to the current cursor position.
    void
    write(int c)
    Writes a character at the current cursor position and advances the cursor.
    void
    write(int c, boolean overTyping)
    Writes a character at the current cursor position and advances the cursor.
    void
    Writes a string at the current cursor position and advances the cursor.
    void
    write(CharSequence str, boolean overTyping)
    Writes a string at the current cursor position and advances the cursor.
    void
    Clear any internal buffer.
  • Method Details

    • cursor

      int cursor()
      Returns the current cursor position in the buffer.
      Returns:
      the current cursor position (0-based index)
    • atChar

      int atChar(int i)
      Returns the character at the specified position in the buffer.
      Parameters:
      i - the position to check
      Returns:
      the character at the specified position, or -1 if the position is invalid
    • length

      int length()
      Returns the length of the buffer.
      Returns:
      the number of characters in the buffer
    • currChar

      int currChar()
      Returns the character at the current cursor position.
      Returns:
      the character at the cursor position, or -1 if the cursor is at the end of the buffer
    • prevChar

      int prevChar()
      Returns the character before the current cursor position.
      Returns:
      the character before the cursor position, or -1 if the cursor is at the beginning of the buffer
    • nextChar

      int nextChar()
      Returns the character after the current cursor position.
      Returns:
      the character after the cursor position, or -1 if the cursor is at the end of the buffer
    • cursor

      boolean cursor(int position)
      Moves the cursor to the specified position.
      Parameters:
      position - the position to move the cursor to
      Returns:
      true if the cursor was moved, false if the position was invalid
    • move

      int move(int num)
      Moves the cursor by the specified number of characters. Positive values move right, negative values move left.
      Parameters:
      num - the number of characters to move
      Returns:
      the number of positions actually moved
    • up

      boolean up()
      Moves the cursor up one line while maintaining the same column position if possible. This is used for multi-line editing.
      Returns:
      true if the cursor was moved, false if it was already at the first line
    • down

      boolean down()
      Moves the cursor down one line while maintaining the same column position if possible. This is used for multi-line editing.
      Returns:
      true if the cursor was moved, false if it was already at the last line
    • moveXY

      boolean moveXY(int dx, int dy)
      Moves the cursor by the specified number of columns and rows. This is used for multi-line editing.
      Parameters:
      dx - the number of columns to move (positive for right, negative for left)
      dy - the number of rows to move (positive for down, negative for up)
      Returns:
      true if the cursor was moved, false otherwise
    • clear

      boolean clear()
      Clears the buffer content.
      Returns:
      true if the buffer was modified
    • currChar

      boolean currChar(int c)
      Replaces the character at the current cursor position.
      Parameters:
      c - the character to set at the current position
      Returns:
      true if the buffer was modified
    • write

      void write(int c)
      Writes a character at the current cursor position and advances the cursor.
      Parameters:
      c - the character to write
    • write

      void write(int c, boolean overTyping)
      Writes a character at the current cursor position and advances the cursor.
      Parameters:
      c - the character to write
      overTyping - if true, overwrites the character at the current position
    • write

      void write(CharSequence str)
      Writes a string at the current cursor position and advances the cursor.
      Parameters:
      str - the string to write
    • write

      void write(CharSequence str, boolean overTyping)
      Writes a string at the current cursor position and advances the cursor.
      Parameters:
      str - the string to write
      overTyping - if true, overwrites characters at the current position
    • backspace

      boolean backspace()
      Deletes the character before the cursor position.
      Returns:
      true if the buffer was modified
    • backspace

      int backspace(int num)
      Deletes multiple characters before the cursor position.
      Parameters:
      num - the number of characters to delete
      Returns:
      the number of characters actually deleted
    • delete

      boolean delete()
      Deletes the character at the cursor position.
      Returns:
      true if the buffer was modified
    • delete

      int delete(int num)
      Deletes multiple characters starting at the cursor position.
      Parameters:
      num - the number of characters to delete
      Returns:
      the number of characters actually deleted
    • substring

      String substring(int start)
      Returns a substring of the buffer from the specified start position to the end.
      Parameters:
      start - the start index, inclusive
      Returns:
      the substring
    • substring

      String substring(int start, int end)
      Returns a substring of the buffer from the specified start position to the specified end position.
      Parameters:
      start - the start index, inclusive
      end - the end index, exclusive
      Returns:
      the substring
    • upToCursor

      String upToCursor()
      Returns a substring of the buffer from the beginning to the current cursor position.
      Returns:
      the substring
    • toString

      String toString()
      Overrides:
      toString in class Object
    • copy

      Buffer copy()
      Creates a copy of this buffer.
      Returns:
      a new buffer with the same content and cursor position
    • copyFrom

      void copyFrom(Buffer buffer)
      Copies the content and cursor position from another buffer.
      Parameters:
      buffer - the buffer to copy from
    • zeroOut

      void zeroOut()
      Clear any internal buffer.