net.percederberg.grammatica.parser
Class ReaderBuffer

java.lang.Object
  extended by net.percederberg.grammatica.parser.ReaderBuffer
All Implemented Interfaces:
java.lang.CharSequence

public class ReaderBuffer
extends java.lang.Object
implements java.lang.CharSequence

A character buffer that automatically reads from an input source stream when needed. This class keeps track of the current position in the buffer and its line and column number in the original input source. It allows unlimited look-ahead of characters in the input, reading and buffering the required data internally. As the position is advanced, the buffer content prior to the current position is subject to removal to make space for reading new content. A few characters before the current position are always kept to enable boundary condition checks.

Since:
1.5

Field Summary
static int BLOCK_SIZE
          The stream reading block size.
 
Constructor Summary
ReaderBuffer(java.io.Reader input)
          Creates a new tokenizer character buffer.
 
Method Summary
 char charAt(int index)
          Returns a character already in the buffer.
 int columnNumber()
          Returns the current column number.
 void dispose()
          Discards all resources used by this buffer.
 int length()
          Returns the current character buffer length.
 int lineNumber()
          Returns the current line number.
 int peek(int offset)
          Returns a character relative to the current position.
 int position()
          Returns the current position in the buffer.
 java.lang.String read(int offset)
          Reads the specified number of characters from the current position.
 java.lang.CharSequence subSequence(int start, int end)
          Returns a character sequence already in the buffer.
 java.lang.String toString()
          Returns the current content of the buffer as a string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

BLOCK_SIZE

public static final int BLOCK_SIZE
The stream reading block size. All reads from the underlying character stream will be made in multiples of this block size. Also the character buffer size will always be a multiple of this factor.

See Also:
Constant Field Values
Constructor Detail

ReaderBuffer

public ReaderBuffer(java.io.Reader input)
Creates a new tokenizer character buffer.

Parameters:
input - the input source character reader
Method Detail

dispose

public void dispose()
Discards all resources used by this buffer. This will also close the source input stream. Disposing a previously disposed buffer has no effect.


position

public int position()
Returns the current position in the buffer.

Returns:
the current position in the buffer

lineNumber

public int lineNumber()
Returns the current line number. This number is the input source line number of the current position.

Returns:
the current position line number

columnNumber

public int columnNumber()
Returns the current column number. This number is the input source column number of the current position.

Returns:
the current position column number

length

public int length()
Returns the current character buffer length. Note that the length may increase (and decrease) as more characters are read from the input source or removed to free up space.

Specified by:
length in interface java.lang.CharSequence

charAt

public char charAt(int index)
            throws java.lang.IndexOutOfBoundsException
Returns a character already in the buffer. Note that this method may behave in unexpected ways when performing operations that modifies the buffer content.

Specified by:
charAt in interface java.lang.CharSequence
Parameters:
index - the char index, 0 <= index < length()
Returns:
the character at the specified index
Throws:
java.lang.IndexOutOfBoundsException - if the index is negative or not less than length()

subSequence

public java.lang.CharSequence subSequence(int start,
                                          int end)
                                   throws java.lang.IndexOutOfBoundsException
Returns a character sequence already in the buffer. Note that this method may behave in unexpected ways when performing operations that modifies the buffer content.

Specified by:
subSequence in interface java.lang.CharSequence
Parameters:
start - the start index, inclusive
end - the end index, exclusive
Returns:
the character sequence specified
Throws:
java.lang.IndexOutOfBoundsException - if one of the indices were negative or not less than (or equal) than length()

toString

public java.lang.String toString()
Returns the current content of the buffer as a string. Note that content before the current position will also be returned.

Specified by:
toString in interface java.lang.CharSequence
Overrides:
toString in class java.lang.Object
Returns:
the current buffer content

peek

public int peek(int offset)
         throws java.io.IOException
Returns a character relative to the current position. This method may read from the input source and may also trim the buffer content prior to the current position. The result of calling this method may therefore be that the buffer length and content have been modified.

The character offset must be positive, but is allowed to span the entire size of the input source stream. Note that the internal buffer must hold all the intermediate characters, which may be wasteful if the offset is too large.

Parameters:
offset - the character offset, from 0 and up
Returns:
the character found as an integer in the range 0 to 65535 (0x00-0xffff), or -1 if the end of the stream was reached
Throws:
java.io.IOException - if an I/O error occurred

read

public java.lang.String read(int offset)
                      throws java.io.IOException
Reads the specified number of characters from the current position. This will also move the current position forward. This method will not attempt to move beyond the end of the input source stream. When reaching the end of file, the returned string might be shorter than requested. Any remaining characters will always be returned before returning null.

Parameters:
offset - the character offset, from 0 and up
Returns:
the string containing the characters read, or null no more characters remain in the buffer
Throws:
java.io.IOException - if an I/O error occurred