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
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()No-op.final ParserReaderdelete()Trims off the last character in the marking buffer.final ParserReaderdelete(int count) Trims off the specified number of last characters in the marking buffer.final StringReturns the contents of the reusable character buffer as a string, and resets the buffer for next usage.final StringgetMarked(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 voidmark()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 intpeek()Peeks the next character in the stream.final intSame aspeek()but skips over any whitespace characters.final intread()Reads a single character.intread(char[] cbuf, int off, int len) Subclasses can override this method to provide additional filtering.final Stringread(int num) Read the specified number of characters off the stream.final intSame asread()but detects and combines extended unicode characters (characters above 0x10000).final intSame asread()but skips over any whitespace characters.final ParserReaderreplace(char c) Replace the last read character in the buffer with the specified character.final ParserReaderreplace(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
-
close
No-op.Input readers are closed in the
ParserPipeclass.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein classReader- Throws:
IOException- If a problem occurred trying to read from the reader.
-
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.
-
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.
-
getPosition
Description copied from interface:PositionableReturns the current position in a reader or input stream.- Specified by:
getPositionin interfacePositionable- Returns:
- The current position in a reader or input stream.
-
mark
Start buffering the calls to read() so that the text can be gathered from the mark point on callinggetFromMarked(). -
parseNumberString
Reads a numeric string from the specified reader.- Returns:
- The parsed number string.
- Throws:
IOException- Thrown by underlying stream.
-
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
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:
readin 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.
-
read
Subclasses can override this method to provide additional filtering.Default implementation simply calls the same method on the underlying reader.
- Specified by:
readin classReader- Throws:
IOException
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-