allow a digest in Stream
[org.ibex.io.git] / src / org / ibex / io / ByteBufInputStream.java
index 7581c88..064b7bd 100644 (file)
@@ -4,31 +4,47 @@
 
 package org.ibex.io;
 import java.io.*;
+import org.ibex.crypto.*;
 
 /** package-private class */
 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;
+    public  Digest digest = null;
     
     public ByteBufInputStream(InputStream 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();
+            if (is == null) return -1;
+            if (prereading) return -1;
             start = 0;
             do {
                 end = is.read(buf, 0, buf.length);
-                if (end == -1 && next != null) {
+                if (end == -1) {
                     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;
+                    }
+                } else {
+                    if (digest != null) digest.update(buf, 0, end);
                 }
             } while(end==0);
             if (end == -1) { end = 0; return -1; }