Class CharReadBuffer

java.lang.Object
org.apache.commons.geometry.io.core.internal.CharReadBuffer

public class CharReadBuffer extends Object
Class used to buffer characters read from an underlying Reader. Characters can be consumed from the buffer, examined without being consumed, and pushed back onto the buffer. The internal bufer is resized as needed.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Construct a new instance that buffers characters from the given reader.
    CharReadBuffer(Reader reader, int initialCapacity)
    Construct a new instance that buffers characters from the given reader.
    CharReadBuffer(Reader reader, int initialCapacity, int minRead)
    Construct a new instance that buffers characters from the given reader.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    charAt(int index)
    Get the character at the given buffer index or -1 if the index is past the end of the content.
    boolean
    Return true if more characters are available from the read buffer.
    int
    Attempt to make at least n characters available in the buffer, reading characters from the underlying reader as needed.
    int
    Return the next character in the buffer without removing it.
    peekString(int len)
    Return a string from the buffer without removing it.
    void
    push(char ch)
    Push a character back onto the read buffer.
    void
    Push a string back onto the read buffer.
    int
    Remove and return the next character in the buffer.
    readString(int len)
    Remove and return a string from the buffer.
    int
    skip(int n)
    Skip n characters from the stream.

    Methods inherited from class Object

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

    • CharReadBuffer

      public CharReadBuffer(Reader reader)
      Construct a new instance that buffers characters from the given reader.
      Parameters:
      reader - underlying reader instance
      Throws:
      NullPointerException - if reader is null
    • CharReadBuffer

      public CharReadBuffer(Reader reader, int initialCapacity)
      Construct a new instance that buffers characters from the given reader.
      Parameters:
      reader - underlying reader instance
      initialCapacity - the initial capacity of the internal buffer; the buffer is resized as needed
      Throws:
      NullPointerException - if reader is null
      IllegalArgumentException - if initialCapacity is less than one.
    • CharReadBuffer

      public CharReadBuffer(Reader reader, int initialCapacity, int minRead)
      Construct a new instance that buffers characters from the given reader.
      Parameters:
      reader - underlying reader instance
      initialCapacity - the initial capacity of the internal buffer; the buffer is resized as needed
      minRead - the minimum number of characters to request from the reader when fetching more characters into the buffer; this can be used to limit the number of calls made to the reader
      Throws:
      NullPointerException - if reader is null
      IllegalArgumentException - if initialCapacity or minRead are less than one.
  • Method Details

    • hasMoreCharacters

      public boolean hasMoreCharacters()
      Return true if more characters are available from the read buffer.
      Returns:
      true if more characters are available from the read buffer
      Throws:
      UncheckedIOException - if an I/O error occurs
    • makeAvailable

      public int makeAvailable(int n)
      Attempt to make at least n characters available in the buffer, reading characters from the underlying reader as needed. The number of characters available is returned.
      Parameters:
      n - number of characters requested to be available
      Returns:
      number of characters available for immediate use in the buffer
      Throws:
      UncheckedIOException - if an I/O error occurs
    • read

      public int read()
      Remove and return the next character in the buffer.
      Returns:
      the next character in the buffer or -1 if the end of the content has been reached
      Throws:
      UncheckedIOException - if an I/O error occurs
      See Also:
    • readString

      public String readString(int len)
      Remove and return a string from the buffer. The length of the string will be the number of characters available in the buffer up to len. Null is returned if no more characters are available.
      Parameters:
      len - requested length of the string
      Returns:
      a string from the read buffer or null if no more characters are available
      Throws:
      IllegalArgumentException - if len is less than 0
      UncheckedIOException - if an I/O error occurs
      See Also:
    • peek

      public int peek()
      Return the next character in the buffer without removing it.
      Returns:
      the next character in the buffer or -1 if the end of the content has been reached
      Throws:
      UncheckedIOException - if an I/O error occurs
      See Also:
    • peekString

      public String peekString(int len)
      Return a string from the buffer without removing it. The length of the string will be the number of characters available in the buffer up to len. Null is returned if no more characters are available.
      Parameters:
      len - requested length of the string
      Returns:
      a string from the read buffer or null if no more characters are available
      Throws:
      IllegalArgumentException - if len is less than 0
      UncheckedIOException - if an I/O error occurs
      See Also:
    • charAt

      public int charAt(int index)
      Get the character at the given buffer index or -1 if the index is past the end of the content. The character is not removed from the buffer.
      Parameters:
      index - index of the character to receive relative to the buffer start
      Returns:
      the character at the given index of -1 if the character is past the end of the stream content
      Throws:
      UncheckedIOException - if an I/O exception occurs
    • skip

      public int skip(int n)
      Skip n characters from the stream. Characters are first skipped from the buffer and then from the underlying reader using Reader.skip(long) if needed.
      Parameters:
      n - number of character to skip
      Returns:
      the number of characters skipped
      Throws:
      IllegalArgumentException - if n is negative
      UncheckedIOException - if an I/O error occurs
    • push

      public void push(char ch)
      Push a character back onto the read buffer. The argument will be the next character returned by read() or peek().
      Parameters:
      ch - character to push onto the read buffer
    • pushString

      public void pushString(String str)
      Push a string back onto the read buffer. The first character of the string will be the next character returned by read() or peek().
      Parameters:
      str - string to push onto the read buffer