Class CharReadBuffer
- java.lang.Object
-
- org.apache.commons.geometry.io.core.internal.CharReadBuffer
-
public class CharReadBuffer extends java.lang.ObjectClass used to buffer characters read from an underlyingReader. 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
Fields Modifier and Type Field Description private char[]bufferCharacter buffer.private intcountThe number of valid elements in the buffer.private static intDEFAULT_INITIAL_CAPACITYDefault initial buffer capacity.private static intEOFConstant indicating that the end of the input has been reached.private intheadThe index of the head element in the buffer.private static doubleLOG2Log 2 constant.private intminReadMinimum number of characters to request for each read.private booleanreachedEofTrue when the end of reader content is reached.private java.io.ReaderreaderUnderlying reader instance.
-
Constructor Summary
Constructors Constructor Description CharReadBuffer(java.io.Reader reader)Construct a new instance that buffers characters from the given reader.CharReadBuffer(java.io.Reader reader, int initialCapacity)Construct a new instance that buffers characters from the given reader.CharReadBuffer(java.io.Reader reader, int initialCapacity, int minRead)Construct a new instance that buffers characters from the given reader.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intcharAt(int index)Get the character at the given buffer index or -1 if the index is past the end of the content.private voidcharsAppended(int n)Method called to indicate that characters have been appended to the end of the read buffer.private voidcharsPushed(int n)Method called to indicate that characters have been pushed to the front of the read buffer.private voidcharsRemoved(int n)Method called to indicate that characters have been removed from the front of the read buffer.private voidensureCapacity(int capacity)Ensure that the current buffer has at leastcapacitynumber of elements.booleanhasMoreCharacters()Return true if more characters are available from the read buffer.intmakeAvailable(int n)Attempt to make at leastncharacters available in the buffer, reading characters from the underlying reader as needed.intpeek()Return the next character in the buffer without removing it.java.lang.StringpeekString(int len)Return a string from the buffer without removing it.voidpush(char ch)Push a character back onto the read buffer.private voidpushCharInternal(char ch)Internal method to push a single character back onto the read buffer.voidpushString(java.lang.String str)Push a string back onto the read buffer.intread()Remove and return the next character in the buffer.private voidreadChars(int n)Read characters from the underlying character stream into the internal buffer.java.lang.StringreadString(int len)Remove and return a string from the buffer.intskip(int n)Skipncharacters from the stream.
-
-
-
Field Detail
-
EOF
private static final int EOF
Constant indicating that the end of the input has been reached.- See Also:
- Constant Field Values
-
DEFAULT_INITIAL_CAPACITY
private static final int DEFAULT_INITIAL_CAPACITY
Default initial buffer capacity.- See Also:
- Constant Field Values
-
LOG2
private static final double LOG2
Log 2 constant.
-
reader
private final java.io.Reader reader
Underlying reader instance.
-
buffer
private char[] buffer
Character buffer.
-
head
private int head
The index of the head element in the buffer.
-
count
private int count
The number of valid elements in the buffer.
-
reachedEof
private boolean reachedEof
True when the end of reader content is reached.
-
minRead
private final int minRead
Minimum number of characters to request for each read.
-
-
Constructor Detail
-
CharReadBuffer
public CharReadBuffer(java.io.Reader reader)
Construct a new instance that buffers characters from the given reader.- Parameters:
reader- underlying reader instance- Throws:
java.lang.NullPointerException- ifreaderis null
-
CharReadBuffer
public CharReadBuffer(java.io.Reader reader, int initialCapacity)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:
java.lang.NullPointerException- ifreaderis nulljava.lang.IllegalArgumentException- ifinitialCapacityis less than one.
-
CharReadBuffer
public CharReadBuffer(java.io.Reader reader, int initialCapacity, int minRead)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:
java.lang.NullPointerException- ifreaderis nulljava.lang.IllegalArgumentException- ifinitialCapacityorminReadare less than one.
-
-
Method Detail
-
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:
java.io.UncheckedIOException- if an I/O error occurs
-
makeAvailable
public int makeAvailable(int n)
Attempt to make at leastncharacters 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:
java.io.UncheckedIOException- if an I/O error occurs
-
read
public int read()
Remove and return the next character in the buffer.
-
readString
public java.lang.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 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:
java.lang.IllegalArgumentException- iflenis less than 0java.io.UncheckedIOException- if an I/O error occurs- See Also:
peekString(int)
-
peek
public int peek()
Return the next character in the buffer without removing it.
-
peekString
public java.lang.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 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:
java.lang.IllegalArgumentException- iflenis less than 0java.io.UncheckedIOException- if an I/O error occurs- See Also:
readString(int)
-
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
-1if the character is past the end of the stream content - Throws:
java.io.UncheckedIOException- if an I/O exception occurs
-
skip
public int skip(int n)
Skipncharacters 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:
java.lang.IllegalArgumentException- ifnis negativejava.io.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 byread()orpeek().- Parameters:
ch- character to push onto the read buffer
-
pushString
public void pushString(java.lang.String str)
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:
java.io.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 leastcapacitynumber of elements. The number of content elements in the buffer is not changed.- Parameters:
capacity- the minimum required capacity of the buffer
-
-