Class BufferImpl

java.lang.Object
org.jline.reader.impl.BufferImpl
All Implemented Interfaces:
Buffer

public class BufferImpl extends Object implements Buffer
Default implementation of the Buffer interface.

This class provides a mutable buffer for storing and manipulating the text being edited in the LineReader. It maintains the text content and the current cursor position, and provides methods for text insertion, deletion, and cursor movement.

Key features include:

  • Efficient text insertion and deletion with a gap buffer implementation
  • Support for Unicode characters beyond the Basic Multilingual Plane
  • Cursor movement in both character and line coordinates
  • Copy and paste operations
  • Secure clearing of buffer contents

The buffer uses a gap buffer data structure for efficient editing operations, which provides good performance for the typical editing patterns in a line editor.

Since:
2.0
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new buffer with the default size (64).
    BufferImpl(int size)
    Creates a new buffer with the specified size.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    atChar(int i)
    Returns the character at the specified position in the buffer.
    boolean
    Issue a backspace.
    int
    backspace(int num)
    Issue num backspaces.
    boolean
    Clears the buffer content.
    Creates a copy of this buffer.
    void
    Copies the content and cursor position from another buffer.
    int
    Returns the character at the current cursor position.
    boolean
    currChar(int ch)
    Replaces the character at the current cursor position.
    int
    Returns the current cursor position in the buffer.
    boolean
    cursor(int position)
    Move the cursor position to the specified absolute index.
    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)
    Move the cursor where 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)
    Write the specific character into the buffer, setting the cursor position ahead one.
    void
    write(int c, boolean overTyping)
    Write the specific character into the buffer, setting the cursor position ahead one.
    void
    Insert the specified chars into the buffer, setting the cursor to the end of the insertion point.
    void
    write(CharSequence str, boolean overTyping)
    Writes a string at the current cursor position and advances the cursor.
    void
    Clear any internal buffer.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • BufferImpl

      public BufferImpl()
      Creates a new buffer with the default size (64).
    • BufferImpl

      public BufferImpl(int size)
      Creates a new buffer with the specified size.
      Parameters:
      size - the initial size of the buffer
  • Method Details

    • copy

      public BufferImpl copy()
      Description copied from interface: Buffer
      Creates a copy of this buffer.
      Specified by:
      copy in interface Buffer
      Returns:
      a new buffer with the same content and cursor position
    • cursor

      public int cursor()
      Description copied from interface: Buffer
      Returns the current cursor position in the buffer.
      Specified by:
      cursor in interface Buffer
      Returns:
      the current cursor position (0-based index)
    • length

      public int length()
      Description copied from interface: Buffer
      Returns the length of the buffer.
      Specified by:
      length in interface Buffer
      Returns:
      the number of characters in the buffer
    • currChar

      public boolean currChar(int ch)
      Description copied from interface: Buffer
      Replaces the character at the current cursor position.
      Specified by:
      currChar in interface Buffer
      Parameters:
      ch - the character to set at the current position
      Returns:
      true if the buffer was modified
    • currChar

      public int currChar()
      Description copied from interface: Buffer
      Returns the character at the current cursor position.
      Specified by:
      currChar in interface Buffer
      Returns:
      the character at the cursor position, or -1 if the cursor is at the end of the buffer
    • prevChar

      public int prevChar()
      Description copied from interface: Buffer
      Returns the character before the current cursor position.
      Specified by:
      prevChar in interface Buffer
      Returns:
      the character before the cursor position, or -1 if the cursor is at the beginning of the buffer
    • nextChar

      public int nextChar()
      Description copied from interface: Buffer
      Returns the character after the current cursor position.
      Specified by:
      nextChar in interface Buffer
      Returns:
      the character after the cursor position, or -1 if the cursor is at the end of the buffer
    • atChar

      public int atChar(int i)
      Description copied from interface: Buffer
      Returns the character at the specified position in the buffer.
      Specified by:
      atChar in interface Buffer
      Parameters:
      i - the position to check
      Returns:
      the character at the specified position, or -1 if the position is invalid
    • write

      public void write(int c)
      Write the specific character into the buffer, setting the cursor position ahead one.
      Specified by:
      write in interface Buffer
      Parameters:
      c - the character to insert
    • write

      public void write(int c, boolean overTyping)
      Write the specific character into the buffer, setting the cursor position ahead one. The text may overwrite or insert based on the current setting of overTyping.
      Specified by:
      write in interface Buffer
      Parameters:
      c - the character to insert
      overTyping - if true, overwrites the character at the current position
    • write

      public void write(CharSequence str)
      Insert the specified chars into the buffer, setting the cursor to the end of the insertion point.
      Specified by:
      write in interface Buffer
      Parameters:
      str - the string to write
    • write

      public void write(CharSequence str, boolean overTyping)
      Description copied from interface: Buffer
      Writes a string at the current cursor position and advances the cursor.
      Specified by:
      write in interface Buffer
      Parameters:
      str - the string to write
      overTyping - if true, overwrites characters at the current position
    • clear

      public boolean clear()
      Description copied from interface: Buffer
      Clears the buffer content.
      Specified by:
      clear in interface Buffer
      Returns:
      true if the buffer was modified
    • substring

      public String substring(int start)
      Description copied from interface: Buffer
      Returns a substring of the buffer from the specified start position to the end.
      Specified by:
      substring in interface Buffer
      Parameters:
      start - the start index, inclusive
      Returns:
      the substring
    • substring

      public String substring(int start, int end)
      Description copied from interface: Buffer
      Returns a substring of the buffer from the specified start position to the specified end position.
      Specified by:
      substring in interface Buffer
      Parameters:
      start - the start index, inclusive
      end - the end index, exclusive
      Returns:
      the substring
    • upToCursor

      public String upToCursor()
      Description copied from interface: Buffer
      Returns a substring of the buffer from the beginning to the current cursor position.
      Specified by:
      upToCursor in interface Buffer
      Returns:
      the substring
    • cursor

      public boolean cursor(int position)
      Move the cursor position to the specified absolute index.
      Specified by:
      cursor in interface Buffer
      Parameters:
      position - the position to move the cursor to
      Returns:
      true if the cursor was moved, false if the position was invalid
    • move

      public int move(int num)
      Move the cursor where characters.
      Specified by:
      move in interface Buffer
      Parameters:
      num - If less than 0, move abs(where) to the left, otherwise move where to the right.
      Returns:
      The number of spaces we moved
    • up

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

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

      public boolean moveXY(int dx, int dy)
      Description copied from interface: Buffer
      Moves the cursor by the specified number of columns and rows. This is used for multi-line editing.
      Specified by:
      moveXY in interface Buffer
      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
    • backspace

      public int backspace(int num)
      Issue num backspaces.
      Specified by:
      backspace in interface Buffer
      Parameters:
      num - the number of characters to delete
      Returns:
      the number of characters backed up
    • backspace

      public boolean backspace()
      Issue a backspace.
      Specified by:
      backspace in interface Buffer
      Returns:
      true if successful
    • delete

      public int delete(int num)
      Description copied from interface: Buffer
      Deletes multiple characters starting at the cursor position.
      Specified by:
      delete in interface Buffer
      Parameters:
      num - the number of characters to delete
      Returns:
      the number of characters actually deleted
    • delete

      public boolean delete()
      Description copied from interface: Buffer
      Deletes the character at the cursor position.
      Specified by:
      delete in interface Buffer
      Returns:
      true if the buffer was modified
    • toString

      public String toString()
      Specified by:
      toString in interface Buffer
      Overrides:
      toString in class Object
    • copyFrom

      public void copyFrom(Buffer buf)
      Description copied from interface: Buffer
      Copies the content and cursor position from another buffer.
      Specified by:
      copyFrom in interface Buffer
      Parameters:
      buf - the buffer to copy from
    • zeroOut

      public void zeroOut()
      Description copied from interface: Buffer
      Clear any internal buffer.
      Specified by:
      zeroOut in interface Buffer