massive bugfixes in stream library
authoradam <adam@megacz.com>
Thu, 5 Jul 2007 21:47:42 +0000 (21:47 +0000)
committeradam <adam@megacz.com>
Thu, 5 Jul 2007 21:47:42 +0000 (21:47 +0000)
darcs-hash:20070705214742-5007d-aff4d4553eaa6e3ad99a416d108cf513a00d6801.gz

src/org/ibex/io/ByteBufInputStream.java
src/org/ibex/io/CharBufReader.java
src/org/ibex/io/Stream.java

index 7581c88..bede9d2 100644 (file)
@@ -9,7 +9,7 @@ import java.io.*;
 abstract class ByteBufInputStream extends InputStream {
 
     private InputStream is;
 abstract class ByteBufInputStream extends InputStream {
 
     private InputStream is;
-    public  InputStream next;
+    public  Stream next = null;
     private byte[] buf = new byte[8192];
     private int start = 0;
     private int end = 0;
     private byte[] buf = new byte[8192];
     private int start = 0;
     private int end = 0;
@@ -18,17 +18,29 @@ abstract class ByteBufInputStream extends InputStream {
         this.is = is;
     }
 
         this.is = is;
     }
 
+    public void appendStream(Stream next) {
+        if (this.next == null) this.next = next;
+        else this.next.appendStream(next);
+    }
     private int bufSize() { if (end==start) { end = start = 0; } return end-start; }
     private int fillBufIfEmpty() {
         try {
             if (bufSize() > 0) return bufSize();
     private int bufSize() { if (end==start) { end = start = 0; } return end-start; }
     private int fillBufIfEmpty() {
         try {
             if (bufSize() > 0) return bufSize();
+            if (is == null) return -1;
+            if (prereading) return -1;
             start = 0;
             do {
                 end = is.read(buf, 0, buf.length);
             start = 0;
             do {
                 end = is.read(buf, 0, buf.length);
-                if (end == -1 && next != null) {
+                if (end == -1) {
                     is.close();
                     is.close();
-                    is = next;
-                    continue;
+                    is = null;
+                    if (next != null) {
+                        // FIXME: append to a stream that has already run out?
+                        is = next.getInputStream();
+                        next = null;
+                        start = end = 0;
+                        continue;
+                    }
                 }
             } while(end==0);
             if (end == -1) { end = 0; return -1; }
                 }
             } while(end==0);
             if (end == -1) { end = 0; return -1; }
index 225fd86..89fbc63 100644 (file)
@@ -83,6 +83,8 @@ class CharBufReader extends Reader {
         if (bufSize()<=0) return;
         try {
             writer.write(buf, start, end-start);
         if (bufSize()<=0) return;
         try {
             writer.write(buf, start, end-start);
+            writer.flush();
+            start = end = 0;
         } catch (IOException e) {
             Stream.ioe(e);
         }
         } catch (IOException e) {
             Stream.ioe(e);
         }
index ea43ba7..ab52f8a 100644 (file)
@@ -25,9 +25,8 @@ public class Stream {
     protected final In in;
     protected final Out out;
     private         String newLine = "\r\n";
     protected final In in;
     protected final Out out;
     private         String newLine = "\r\n";
-    private         Stream in_next = null;
 
 
-    public  Stream(byte[] b, int off, int len) { this.in = new Stream.In(new ByteArrayInputStream(b, off, len)); this.out=null; }
+    public  Stream(byte[] b, int off, int len)       { this.in = new Stream.In(new ByteArrayInputStream(b, off, len)); this.out=null; }
     public  Stream(InputStream in)                   { this.in = new Stream.In(in); this.out = null; }
     public  Stream(                OutputStream out) { this.in = null;              this.out = new Stream.Out(out); }
     public  Stream(InputStream in, OutputStream out) { this.in = new Stream.In(in); this.out = new Stream.Out(out); }
     public  Stream(InputStream in)                   { this.in = new Stream.In(in); this.out = null; }
     public  Stream(                OutputStream out) { this.in = null;              this.out = new Stream.Out(out); }
     public  Stream(InputStream in, OutputStream out) { this.in = new Stream.In(in); this.out = new Stream.Out(out); }
@@ -49,10 +48,12 @@ public class Stream {
     public void   print(String s)                  { out.write(s); flush(); }
     public void   println(String s)                { print(s); print(newLine); }
     public void   flush()                          { if (out != null) out.flushWriter(); }
     public void   print(String s)                  { out.write(s); flush(); }
     public void   println(String s)                { print(s); print(newLine); }
     public void   flush()                          { if (out != null) out.flushWriter(); }
+    public void writeBytes(byte[] b, int off, int len) { try { out.write(b, off, len); } catch (IOException e) { ioe(e); } }
     public int    read(byte[] b, int off, int len) { flush(); return in.readBytes(b, off, len); }
     public int    read(char[] c, int off, int len) { flush(); return in.readChars(c, off, len); }
     public void   close()                          { try { if (in!=null) in.close(); } finally { if (out!=null) out.close(); } }
     public void   setNewline(String s)             { newLine = s; }
     public int    read(byte[] b, int off, int len) { flush(); return in.readBytes(b, off, len); }
     public int    read(char[] c, int off, int len) { flush(); return in.readChars(c, off, len); }
     public void   close()                          { try { if (in!=null) in.close(); } finally { if (out!=null) out.close(); } }
     public void   setNewline(String s)             { newLine = s; }
+    public InputStream getInputStream() { return in; }
 
     private static class Out extends BufferedOutputStream {
         private Writer writer = new OutputStreamWriter(this);
 
     private static class Out extends BufferedOutputStream {
         private Writer writer = new OutputStreamWriter(this);
@@ -91,6 +92,7 @@ public class Stream {
         public In(InputStream in) {
             bbis = new ByteBufInputStream(in) {
                     public void preread() {
         public In(InputStream in) {
             bbis = new ByteBufInputStream(in) {
                     public void preread() {
+                        cbr.unbuffer(unreader);
                         try {
                             if (!cbr.ready()) return;
                         } catch (IOException e) { ioe(e); }
                         try {
                             if (!cbr.ready()) return;
                         } catch (IOException e) { ioe(e); }
@@ -108,31 +110,22 @@ public class Stream {
             unreader = new OutputStreamWriter(new UnReaderStream(bbis));
             cbr = new CharBufReader(new InputStreamReader(bbis));
         }
             unreader = new OutputStreamWriter(new UnReaderStream(bbis));
             cbr = new CharBufReader(new InputStreamReader(bbis));
         }
-
     }
 
     }
 
-    // Utilities: append() and transcribe() //////////////////////////////////////////////////////////////////////////////
+    // Utilities: append() and transcribe() ///////////////////////////////////////////////////////
 
     public Stream append(String in_next) { return appendStream(new Stream(in_next)); }
 
     public Stream append(String in_next) { return appendStream(new Stream(in_next)); }
-    public Stream appendStream(Stream in_next) {
-        if (this.in_next != null)
-            this.in_next.appendStream(in_next);
-        else
-            this.in_next = in_next;
-        return this;
-    }
+    public Stream appendStream(Stream in_next) { in.bbis.appendStream(in_next); return this; }
 
     public void transcribe(Stream out) { transcribe(out, false); }
     public void transcribe(Stream out, boolean close) {
 
     public void transcribe(Stream out) { transcribe(out, false); }
     public void transcribe(Stream out, boolean close) {
-        try {
-            byte[] buf = new byte[1024];
-            while(true) {
-                int numread = in.read(buf, 0, buf.length);
-                if (numread==-1) { in.close(); break; }
-                out.out.write(buf, 0, numread);
-            }
-           if (close) out.close();
-        } catch (IOException ioe) { ioe(ioe); }
+        byte[] buf = new byte[1024];
+        while(true) {
+            int numread = in.read(buf, 0, buf.length);
+            if (numread==-1) { in.close(); break; }
+            out.writeBytes(buf, 0, numread);
+        }
+        if (close) out.close();
     }
 
     public void transcribe(StringBuffer out) {
     }
 
     public void transcribe(StringBuffer out) {
@@ -145,13 +138,20 @@ public class Stream {
     }
 
     public static int countLines(Stream s) {
     }
 
     public static int countLines(Stream s) {
-        System.out.println("counting lines...");
         int ret = 0;
         while(s.readln() != null) ret++;
         s.close();
         return ret;
     }
 
         int ret = 0;
         while(s.readln() != null) ret++;
         s.close();
         return ret;
     }
 
+    // FIXME: ugly
+    public static int countBytes(Stream s) {
+        int ret = 0;
+        while(s.in.read() != -1) ret++;
+        s.close();
+        return ret;
+    }
+
     // Exceptions //////////////////////////////////////////////////////////////////////////////
 
     static int ioe(IOException e) {
     // Exceptions //////////////////////////////////////////////////////////////////////////////
 
     static int ioe(IOException e) {