massive rewrite of io library
[org.ibex.io.git] / src / org / ibex / io / CharBufReader.java
diff --git a/src/org/ibex/io/CharBufReader.java b/src/org/ibex/io/CharBufReader.java
new file mode 100644 (file)
index 0000000..225fd86
--- /dev/null
@@ -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<end; i++)
+                if (buf[i] == '\n') {
+                    // this should (in theory) handle CR, LF,
+                    // CRLF, and LFCR properly, assuming that the
+                    // file consistently uses the same ending
+                    // throughout.
+                    int begin = start;
+                    int len = i-start;
+                    start = i+1;
+                    if (buf[begin] == '\r') { begin++; len--; }
+                    while (len > 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);
+        }
+    }
+}