01: /*
02: * Created on 07-Oct-2003
03: */
04: package uk.org.ponder.streamutil.read;
05:
06: import java.io.IOException;
07: import java.io.PushbackReader;
08: import java.io.Reader;
09:
10: /**
11: * A PushbackReader with exactly one byte of pushback, which silently swallows any
12: * EOF exceptions. Instead, EOF is reported by returning the value (char)-1 which
13: * Unicode defines to be nothing useful. EOF may be independently verified by
14: * checking the EOF() method.
15: * @author Bosmon
16: */
17: //TODO: replace Reader with a non-synchronized infrastructure, a la PrintOutputStream.
18: public class LexReader extends PushbackReader {
19: private int read = 0;
20: private boolean EOF = false;
21:
22: /** Creates a LexReader wrapping the supplied Reader */
23: public LexReader(Reader r) {
24: super (r);
25: }
26:
27: /** Checks if end of the wrapped stream has been reached.
28: * @return <code>true</code> if the stream has reached the end of file.
29: */
30: public boolean EOF() {
31: return EOF;
32: }
33:
34: /** Reads a single character from the stream and returns it. If the stream
35: * has reached the end of file, return the value (char)-1.
36: * @return The character that was read.
37: * @throws IOException If a read error other than EOF occurs.
38: */
39: public char get() throws IOException {
40: int c = read();
41: if (c != -1) {
42: ++read;
43: } else {
44: EOF = true;
45: }
46: return (char) c;
47: }
48: }
|