Class CharReadBuffer
java.lang.Object
org.apache.commons.geometry.io.core.internal.CharReadBuffer
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.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate char[]
Character buffer.private int
The number of valid elements in the buffer.private static final int
Default initial buffer capacity.private static final int
Constant indicating that the end of the input has been reached.private int
The index of the head element in the buffer.private static final double
Log 2 constant.private final int
Minimum number of characters to request for each read.private boolean
True when the end of reader content is reached.private final Reader
Underlying reader instance. -
Constructor Summary
ConstructorsConstructorDescriptionCharReadBuffer
(Reader reader) 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 TypeMethodDescriptionint
charAt
(int index) Get the character at the given buffer index or -1 if the index is past the end of the content.private void
charsAppended
(int n) Method called to indicate that characters have been appended to the end of the read buffer.private void
charsPushed
(int n) Method called to indicate that characters have been pushed to the front of the read buffer.private void
charsRemoved
(int n) Method called to indicate that characters have been removed from the front of the read buffer.private void
ensureCapacity
(int capacity) Ensure that the current buffer has at leastcapacity
number of elements.boolean
Return true if more characters are available from the read buffer.int
makeAvailable
(int n) Attempt to make at leastn
characters available in the buffer, reading characters from the underlying reader as needed.int
peek()
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.private void
pushCharInternal
(char ch) Internal method to push a single character back onto the read buffer.void
pushString
(String str) Push a string back onto the read buffer.int
read()
Remove and return the next character in the buffer.private void
readChars
(int n) Read characters from the underlying character stream into the internal buffer.readString
(int len) Remove and return a string from the buffer.int
skip
(int n) Skipn
characters from the stream.
-
Field Details
-
EOF
private static final int EOFConstant indicating that the end of the input has been reached.- See Also:
-
DEFAULT_INITIAL_CAPACITY
private static final int DEFAULT_INITIAL_CAPACITYDefault initial buffer capacity.- See Also:
-
LOG2
private static final double LOG2Log 2 constant. -
reader
Underlying reader instance. -
buffer
private char[] bufferCharacter buffer. -
head
private int headThe index of the head element in the buffer. -
count
private int countThe number of valid elements in the buffer. -
reachedEof
private boolean reachedEofTrue when the end of reader content is reached. -
minRead
private final int minReadMinimum number of characters to request for each read.
-
-
Constructor Details
-
CharReadBuffer
Construct a new instance that buffers characters from the given reader.- Parameters:
reader
- underlying reader instance- Throws:
NullPointerException
- ifreader
is null
-
CharReadBuffer
Construct a new instance that buffers characters from the given reader.- Parameters:
reader
- underlying reader instanceinitialCapacity
- the initial capacity of the internal buffer; the buffer is resized as needed- Throws:
NullPointerException
- ifreader
is nullIllegalArgumentException
- ifinitialCapacity
is less than one.
-
CharReadBuffer
Construct a new instance that buffers characters from the given reader.- Parameters:
reader
- underlying reader instanceinitialCapacity
- the initial capacity of the internal buffer; the buffer is resized as neededminRead
- 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
- ifreader
is nullIllegalArgumentException
- ifinitialCapacity
orminRead
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 leastn
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
Remove and return a string from the buffer. The length of the string will be the number of characters available in the buffer up tolen
. 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
- iflen
is less than 0UncheckedIOException
- 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
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 tolen
. 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
- iflen
is less than 0UncheckedIOException
- 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) Skipn
characters from the stream. Characters are first skipped from the buffer and then from the underlying reader usingReader.skip(long)
if needed.- Parameters:
n
- number of character to skip- Returns:
- the number of characters skipped
- Throws:
IllegalArgumentException
- ifn
is negativeUncheckedIOException
- 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 byread()
orpeek()
.- Parameters:
ch
- character to push onto the read buffer
-
pushString
Push a string back onto the read buffer. The first character of the string will be the next character returned byread()
orpeek()
.- Parameters:
str
- string to push onto the read buffer
-
pushCharInternal
private void pushCharInternal(char ch) Internal method to push a single character back onto the read buffer. The buffer capacity is not checked.- Parameters:
ch
- character to push onto the read buffer
-
readChars
private void readChars(int n) Read characters from the underlying character stream into the internal buffer.- Parameters:
n
- minimum number of characters requested to be placed in the buffer- Throws:
UncheckedIOException
- if an I/O error occurs
-
charsRemoved
private void charsRemoved(int n) Method called to indicate that characters have been removed from the front of the read buffer.- Parameters:
n
- number of characters removed
-
charsPushed
private void charsPushed(int n) Method called to indicate that characters have been pushed to the front of the read buffer.- Parameters:
n
- number of characters pushed
-
charsAppended
private void charsAppended(int n) Method called to indicate that characters have been appended to the end of the read buffer.- Parameters:
n
- number of characters appended
-
ensureCapacity
private void ensureCapacity(int capacity) Ensure that the current buffer has at leastcapacity
number of elements. The number of content elements in the buffer is not changed.- Parameters:
capacity
- the minimum required capacity of the buffer
-