1 public class PushbackInputStream {
2 // A pushback input stream lets you throw things
3 // back into the stream to be read again. Read
4 // characters from a normal input stream, but keep
5 // them in a ring buffer also. Then, when unread()
6 // is called, throw characters from the ring buffer
7 // onto a stack. During read() first take characters
8 // off the stack if they are present, otherwise get
9 // them from the normal input stream.
11 private FileInputStream in;
23 public PushbackInputStream(FileInputStream fis) {
39 // get next value from stack or if empty
40 // then from the input stream
48 // put whatever it is in the ring buffer
51 // keep ring buffer index
57 // user gets what they want
62 public void unread(int v) {
65 // the unread stack can only get so high
67 System.printString("PushbackInputStream: max reached");
71 // put it on the unread stack
72 stack[top] = ring[index];