X-Git-Url: http://git.dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fdolda%2Fjsvc%2Fnext%2FPeekReader.java;fp=src%2Fdolda%2Fjsvc%2Fnext%2FPeekReader.java;h=1c3fce58520b3891d6208472071e0fdb9583ee21;hb=a931ca70cb6b5b230e4d9cb8f2f173e45ad82ed0;hp=0000000000000000000000000000000000000000;hpb=23440c2f2a49aebc65dad176516887464876f3e5;p=jsvc.git diff --git a/src/dolda/jsvc/next/PeekReader.java b/src/dolda/jsvc/next/PeekReader.java new file mode 100644 index 0000000..1c3fce5 --- /dev/null +++ b/src/dolda/jsvc/next/PeekReader.java @@ -0,0 +1,61 @@ +package dolda.jsvc.next; + +import java.io.*; + +public class PeekReader extends Reader { + private final Reader back; + private boolean p = false; + private int la; + + public PeekReader(Reader back) { + this.back = back; + } + + public void close() throws IOException { + back.close(); + } + + public int read() throws IOException { + if(p) { + p = false; + return(la); + } else { + return(back.read()); + } + } + + public int read(char[] b, int off, int len) throws IOException { + int r = 0; + while(r < len) { + int c = read(); + if(c < 0) + return(r); + b[off + r++] = (char)c; + } + return(r); + } + + public boolean ready() throws IOException { + if(p) + return(true); + return(back.ready()); + } + + protected boolean whitespace(char c) { + return(Character.isWhitespace(c)); + } + + public int peek(boolean skipws) throws IOException { + if(p) + return(la); + do { + la = back.read(); + p = true; + } while(skipws && (la >= 0) && whitespace((char)la)); + return(la); + } + + public int peek() throws IOException { + return(peek(false)); + } +}