public class LookAheadReader
extends java.lang.Object
Apart from the convenience features, this class differs from other readers in one important philosophical aspect. The orthodox approach for character read functions is to return integers instead of characters, where -1 means end of file. Here the approach is different. We give the consumer the opportunity to check first that a character is available, but the read functions return characters. So the typical sequence is check then read instead of read then check then cast. This saves one step. Attempting operations that wrongly presume the existence of an unconsumed character will result in a RuntimeException.
Modifier and Type | Field and Description |
---|---|
int |
_bufferSize |
RWTextPosition.View |
_textPosView
To allow the client access to text positions
|
Constructor and Description |
---|
LookAheadReader(java.io.Reader reader,
int bufferSize) |
Modifier and Type | Method and Description |
---|---|
void |
advance()
Advances read position.
|
void |
advanceN(int n)
Advances read position.
|
int |
advancePastWhitespace()
Advances past all whitespace.
|
void |
close()
Closes internal reader.
|
boolean |
consumedAll()
Determines whether the client has read or advances past all the clients.
|
boolean |
consumeUntil(char[] endChars,
java.lang.StringBuilder buf,
boolean consumeEndToken)
Advances text to before or just after any of the specified end characters or end of text.
|
boolean |
consumeUntil(java.lang.String endToken,
java.lang.StringBuilder buf,
boolean consumeEndToken)
Advances until just before or just after the end token occurs in the remaining
text or until end of text is reached.
|
boolean |
haveChar()
Indicates whether there is another character that can be read by client.
|
boolean |
haveNChars(int n)
Indicates whether there are another n characters that can be read by the client.
|
boolean |
isOpen() |
char |
peep()
Looks at next unconsumed character, without consuming it.
|
char |
read()
Reads next unconsumed character, thereby consuming it.
|
boolean |
startsWith(char ch)
Determines whether the remaining text starts with character ch.
|
boolean |
startsWith(java.lang.String s)
Determines whether the remaining text to be read starts with string s.
|
boolean |
startsWith(java.lang.String s,
boolean consume)
Determines whether the remaining text to be read starts with string s.
|
void |
tryNChars(int n)
Attempts to ensure that the buffer contains n characters.
|
public final int _bufferSize
public final RWTextPosition.View _textPosView
public void close() throws java.io.IOException
java.io.IOException
public boolean isOpen()
public boolean haveChar() throws java.io.IOException
java.io.IOException
public boolean haveNChars(int n) throws java.io.IOException
n
- The number of characters to check. Must not be bigger than the size
of the buffer this class instance was constructed with.java.io.IOException
public char read() throws java.io.IOException
java.io.IOException
public char peep() throws java.io.IOException
java.io.IOException
public void advance() throws java.io.IOException
java.io.IOException
public void advanceN(int n) throws java.io.IOException
java.io.IOException
public boolean consumedAll() throws java.io.IOException
java.io.IOException
public boolean startsWith(java.lang.String s, boolean consume) throws java.io.IOException
s
- The string for which we are determining whether the text starts with itconsume
- If true and the remaining text starts with s, then we advance past sjava.io.IOException
public boolean startsWith(java.lang.String s) throws java.io.IOException
s
- The string for which we are determining whether the text starts with itjava.io.IOException
public boolean startsWith(char ch) throws java.io.IOException
ch
- The character for which we are determining whether the rest of the
text starts with it.java.io.IOException
public boolean consumeUntil(char[] endChars, java.lang.StringBuilder buf, boolean consumeEndToken) throws java.io.IOException
endChars
- The characterbuf
- If this is non-null, the characters before the end character or
end of text are written in there. Does not place the end character in there.consumeEndToken
- If true, advances past the end character, if any (but doesn't put it
into buf).java.io.IOException
public boolean consumeUntil(java.lang.String endToken, java.lang.StringBuilder buf, boolean consumeEndToken) throws java.io.IOException
endToken
- What we are looking for in the text.buf
- if non-null, writes all characters consumed before end token (if it exists)
or end of text into it.consumeEndToken
- If true, advances past the end token as well (but never puts
the end token into buf).java.io.IOException
public void tryNChars(int n) throws java.io.IOException
n
- The number of characters we aim to have in the buffer.java.io.IOException
public int advancePastWhitespace() throws java.io.IOException
java.io.IOException