| 1 | package dolda.jsvc.next; |
| 2 | |
| 3 | import java.io.*; |
| 4 | |
| 5 | public class PeekReader extends Reader { |
| 6 | private final Reader back; |
| 7 | private boolean p = false; |
| 8 | private int la; |
| 9 | |
| 10 | public PeekReader(Reader back) { |
| 11 | this.back = back; |
| 12 | } |
| 13 | |
| 14 | public void close() throws IOException { |
| 15 | back.close(); |
| 16 | } |
| 17 | |
| 18 | public int read() throws IOException { |
| 19 | if(p) { |
| 20 | p = false; |
| 21 | return(la); |
| 22 | } else { |
| 23 | return(back.read()); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | public int read(char[] b, int off, int len) throws IOException { |
| 28 | int r = 0; |
| 29 | while(r < len) { |
| 30 | int c = read(); |
| 31 | if(c < 0) |
| 32 | return(r); |
| 33 | b[off + r++] = (char)c; |
| 34 | } |
| 35 | return(r); |
| 36 | } |
| 37 | |
| 38 | public boolean ready() throws IOException { |
| 39 | if(p) |
| 40 | return(true); |
| 41 | return(back.ready()); |
| 42 | } |
| 43 | |
| 44 | protected boolean whitespace(char c) { |
| 45 | return(Character.isWhitespace(c)); |
| 46 | } |
| 47 | |
| 48 | public int peek(boolean skipws) throws IOException { |
| 49 | if(p) |
| 50 | return(la); |
| 51 | do { |
| 52 | la = back.read(); |
| 53 | p = true; |
| 54 | } while(skipws && (la >= 0) && whitespace((char)la)); |
| 55 | return(la); |
| 56 | } |
| 57 | |
| 58 | public int peek() throws IOException { |
| 59 | return(peek(false)); |
| 60 | } |
| 61 | } |