fix "limit" and "skip" logic in io (still not very good, though)
[org.ibex.io.git] / src / org / ibex / io / Stream.java
index ea43ba7..70c90f8 100644 (file)
@@ -9,6 +9,7 @@ import java.net.*;
 import java.util.*;
 import java.util.zip.*;
 import org.ibex.util.*;
+import org.ibex.crypto.*;
 
 // Features:
 //   - automatically flush writer before reading on linked read/write pairs
@@ -25,9 +26,8 @@ public class Stream {
     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); }
@@ -43,16 +43,22 @@ public class Stream {
 
     // Main API //////////////////////////////////////////////////////////////////////////////
 
+    public void   setLimit(int limit)              { in.setLimit(limit); }
     public char   peekc()                          { flush(); return in.getc(true); }
     public char   getc()                           { flush(); return in.getc(false); }
     public String readln()                         { flush(); return in.readln(); }
     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 int    skip(int len) { return in.skip(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; }
+
+    public void   setInputDigest(Digest d) { in.bbis.digest = d; }
 
     private static class Out extends BufferedOutputStream {
         private Writer writer = new OutputStreamWriter(this);
@@ -81,16 +87,19 @@ public class Stream {
 
         public char getc(boolean peek) { return cbr.getc(peek); }
         public String readln() { return cbr.readln(); }
+        public int skip(int len) { return bbis.skip(len); }
         public int read() { return bbis.read(); }
         public int read(byte[] b) { try { return bbis.read(b); } catch (IOException e) { ioe(e); return 0; } }
         public int read(byte[] b, int off, int len) { return bbis.read(b, off, len); }
         public void close() { try { cbr.close(); } catch (Exception e) { Log.error(this, e); } }
         public int readBytes(byte[] b, int off, int len) { return bbis.read(b, off, len); }
         public int readChars(char[] c, int off, int len) { return cbr.read(c, off, len); }
+        public void setLimit(int len) { bbis.setLimit(len); }
 
         public In(InputStream in) {
             bbis = new ByteBufInputStream(in) {
                     public void preread() {
+                        cbr.unbuffer(unreader);
                         try {
                             if (!cbr.ready()) return;
                         } catch (IOException e) { ioe(e); }
@@ -108,31 +117,22 @@ public class Stream {
             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 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) {
-        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) {
@@ -145,13 +145,20 @@ public class Stream {
     }
 
     public static int countLines(Stream s) {
-        System.out.println("counting lines...");
         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) {