Before implementing a simple parser it is useful to implement a wrapper around input streams. The PeekStream class wraps an InputStream and provides methods to peek and read the next character.
The class definition starts like this:
The first method peeks at the next character and returns its value, but leaves it available for the next read operation. This works using the instance variable pushback as a single character buffer. (The stream methods mark and reset could be used instead of a pushback buffer, but not all InputStreams support the mark method.)
The second method peeks at the next character and determines if it is expected. The boolean value indicates if the next character is expected or not.
The third method simple reads the next character and returns it. If peek has been called, the character will be in the pushback buffer. Otherwise, the character is obtained from the wrapped stream.
The fourth method reads the next character and determines if it is expected. An error is thrown if the character is not as expected.
The fifth method reads and discards blank characters. This method could be extended to support comments, if required.
The PeekStream class includes the methods defined above. As you can see, these are all very simple support methods, but they make it easier to implement a simple parser.