/Users/lyon/j4p/src/classUtils/pack/util/PushbackReader.java
|
1 package classUtils.pack.util;
2
3 import java.io.*;
4 import java.util.*;
5
6 public class PushbackReader extends FilterReader {
7
8 private ArrayList buf;
9
10 public PushbackReader(Reader r) {
11 this(r,1);
12 }
13
14 public PushbackReader(Reader r, int windowSize) {
15 super(r);
16 this.buf=new ArrayList();
17 }
18
19 public int read() throws IOException {
20 int size=buf.size();
21 if (size==0) return super.read();
22 else {
23 return ((Integer)buf.remove(buf.size()-1)).intValue();
24 }
25 }
26
27 public void unread(int c) throws IOException {
28 buf.add(new Integer(c));
29 }
30
31 private static void readAll(Reader r) throws IOException {
32 int c;
33 while((c=r.read())!=-1) System.out.print((char)c);
34 System.out.println();
35 }
36
37 public static void main(String args[]) throws Exception {
38 PushbackReader r = new PushbackReader(new StringReader("ciao"));
39 readAll(r);
40 r.unread('a');
41 r.unread('i');
42 r.unread('c');
43
44 //r.unread('o');
45 readAll(r);
46 }
47 }