Class ParserReader
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Readable
,Positionable
- Direct Known Subclasses:
UonReader
PushbackReader
with a pushback buffer of 1 character.
Code is optimized to work with a 1 character buffer.
Additionally keeps track of current line and column number, and provides the ability to set mark points and capture characters from the previous mark point.
Notes:
- This class is not thread safe.
See Also:
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
No-op.final ParserReader
delete()
Trims off the last character in the marking buffer.final ParserReader
delete
(int count) Trims off the specified number of last characters in the marking buffer.final String
Returns the contents of the reusable character buffer as a string, and resets the buffer for next usage.final String
getMarked
(int offsetStart, int offsetEnd) Same asgetMarked()
except allows you to specify offsets into the buffer.Returns the current position in a reader or input stream.final void
mark()
Start buffering the calls to read() so that the text can be gathered from the mark point on callinggetFromMarked()
.Reads a numeric string from the specified reader.final int
peek()
Peeks the next character in the stream.final int
Same aspeek()
but skips over any whitespace characters.final int
read()
Reads a single character.int
read
(char[] cbuf, int off, int len) Subclasses can override this method to provide additional filtering.final String
read
(int num) Read the specified number of characters off the stream.final int
Same asread()
but detects and combines extended unicode characters (characters above 0x10000).final int
Same asread()
but skips over any whitespace characters.final ParserReader
replace
(char c) Replace the last read character in the buffer with the specified character.final ParserReader
replace
(int c, int offset) Replaces the last character in the marking buffer with the specified character.unread()
Pushes the last read character back into the stream.Methods inherited from class java.io.Reader
mark, markSupported, nullReader, read, read, ready, reset, skip, transferTo
-
Field Details
-
r
Wrapped reader
-
-
Constructor Details
-
ParserReader
Constructor.- Parameters:
pipe
- The parser input.- Throws:
IOException
- Thrown by underlying stream.
-
-
Method Details
-
read
Reads a single character.Note that this method does NOT process extended unicode characters (i.e. characters above 0x10000), but rather returns them as two
char s. UsereadCodePoint()
to ensure proper handling of extended unicode.- Overrides:
read
in classReader
- Returns:
- The character read, or -1 if the end of the stream has been reached.
- Throws:
IOException
- If a problem occurred trying to read from the reader.
-
readSkipWs
Same asread()
but skips over any whitespace characters.- Returns:
- The first non-whitespace character, or -1 if the end of stream reached.
- Throws:
IOException
- Thrown by underlying stream.
-
readCodePoint
Same asread()
but detects and combines extended unicode characters (characters above 0x10000).- Returns:
- The character read, or -1 if the end of the stream has been reached.
- Throws:
IOException
- If a problem occurred trying to read from the reader.
-
mark
Start buffering the calls to read() so that the text can be gathered from the mark point on callinggetFromMarked()
. -
peek
Peeks the next character in the stream.This is equivalent to doing a
read()
followed by anunread()
.- Returns:
- The peeked character, or (char)-1 if the end of the stream has been reached.
- Throws:
IOException
- If a problem occurred trying to read from the reader.
-
peekSkipWs
Same aspeek()
but skips over any whitespace characters.This is equivalent to doing a
read()
followed by anunread()
.- Returns:
- The peeked character, or (char)-1 if the end of the stream has been reached.
- Throws:
IOException
- If a problem occurred trying to read from the reader.
-
read
Read the specified number of characters off the stream.- Parameters:
num
- The number of characters to read.- Returns:
- The characters packaged as a String.
- Throws:
IOException
- If a problem occurred trying to read from the reader.
-
unread
Pushes the last read character back into the stream.- Returns:
- This object.
- Throws:
IOException
- If a problem occurred trying to read from the reader.
-
close
No-op.Input readers are closed in the
ParserPipe
class.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in classReader
- Throws:
IOException
- If a problem occurred trying to read from the reader.
-
getMarked
Returns the contents of the reusable character buffer as a string, and resets the buffer for next usage.- Returns:
- The contents of the reusable character buffer as a string.
-
getMarked
Same asgetMarked()
except allows you to specify offsets into the buffer.For example, to return the marked string, but trim the first and last characters, call the following:
getFromMarked(1, -1);
- Parameters:
offsetStart
- The offset of the start position.offsetEnd
- The offset of the end position.- Returns:
- The contents of the reusable character buffer as a string.
-
delete
Trims off the last character in the marking buffer.Useful for removing escape characters from sequences.
- Returns:
- This object.
-
delete
Trims off the specified number of last characters in the marking buffer. Useful for removing escape characters from sequences.- Parameters:
count
- The number of characters to delete.- Returns:
- This object.
-
replace
Replaces the last character in the marking buffer with the specified character.offset must be at least1 for normal characters, and2 for extended unicode characters in order for the replacement to fit into the buffer.- Parameters:
c
- The new character.offset
- The offset.- Returns:
- This object.
- Throws:
IOException
- Thrown by underlying stream.
-
replace
Replace the last read character in the buffer with the specified character.- Parameters:
c
- The new character.- Returns:
- This object.
- Throws:
IOException
- Thrown by underlying stream.
-
parseNumberString
Reads a numeric string from the specified reader.- Returns:
- The parsed number string.
- Throws:
IOException
- Thrown by underlying stream.
-
read
Subclasses can override this method to provide additional filtering.Default implementation simply calls the same method on the underlying reader.
- Specified by:
read
in classReader
- Throws:
IOException
-
getPosition
Description copied from interface:Positionable
Returns the current position in a reader or input stream.- Specified by:
getPosition
in interfacePositionable
- Returns:
- The current position in a reader or input stream.
-