X-Git-Url: http://git.megacz.com/?p=org.ibex.io.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fio%2FCharBufReader.java;fp=src%2Forg%2Fibex%2Fio%2FCharBufReader.java;h=225fd860719197e4bed2c29ee0967d11cea4e4b2;hp=0000000000000000000000000000000000000000;hb=3e1c83f8d1d65b96a3e24c3aa2a042c21f005111;hpb=146c5fbaa3c5b80ad4da96fed82ea701fac92c6c diff --git a/src/org/ibex/io/CharBufReader.java b/src/org/ibex/io/CharBufReader.java new file mode 100644 index 0000000..225fd86 --- /dev/null +++ b/src/org/ibex/io/CharBufReader.java @@ -0,0 +1,90 @@ +// Copyright 2000-2007 the Contributors, as shown in the revision logs. +// Licensed under the Apache Public Source License 2.0 ("the License"). +// You may not use this file except in compliance with the License. + +package org.ibex.io; +import java.io.*; + +/** package-private class */ +class CharBufReader extends Reader { + + private Reader reader; + private char[] buf = new char[8192]; + private int start = 0; + private int end = 0; + + public CharBufReader(Reader r) { this.reader = r; } + + public boolean ready() throws IOException { return bufSize()>0; } + private int bufSize() { if (end==start) { end=start=0; } return end-start; } + private int fillBufIfEmpty() { + try { + if (bufSize() > 0) return bufSize(); + start = 0; + do { + end = reader.read(buf, 0, buf.length); + } while(end==0); + if (end == -1) { end = 0; return -1; } + return end; + } catch (IOException e) { Stream.ioe(e); return -1; } + } + + public int available() { return end-start; } + public void close() { + try { + if (reader != null) reader.close(); + } catch (IOException e) { Stream.ioe(e); } + } + + public int read(char[] c, int pos, int len) { + if (fillBufIfEmpty() == -1) return -1; + if (len > end - start) len = end - start; + System.arraycopy(buf, start, c, pos, len); + start += len; + return len; + } + + public char getc(boolean peek) { + if (fillBufIfEmpty() <= 0) throw new Stream.EOF(); + return peek ? buf[start] : buf[start++]; + } + + private StringBuffer lineReaderStringBuffer = new StringBuffer(); + public String readln() { + lineReaderStringBuffer.setLength(0); + boolean readsome = false; + OUT: while(true) { + if (fillBufIfEmpty() <= 0) break; + readsome = true; + for(int i=start; i 0 && buf[begin+len-1] == '\r') { len--; } + lineReaderStringBuffer.append(buf, begin, len); + break OUT; + } + lineReaderStringBuffer.append(buf, start, end-start); + start = end = 0; + } + if (!readsome) return null; + String ret = lineReaderStringBuffer.toString(); + lineReaderStringBuffer.setLength(0); + return ret; + } + + public void unbuffer(Writer writer) { + if (bufSize()<=0) return; + try { + writer.write(buf, start, end-start); + } catch (IOException e) { + Stream.ioe(e); + } + } +}