allow a digest in Stream
[org.ibex.io.git] / src / org / ibex / io / Stream.java
index d4e0c06..2cc2e14 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// 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.
 
@@ -9,60 +9,25 @@ 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
+//   - no checked exceptions thrown
+//   - unified write(char), print(char), and write(byte)
+//   - unreading/peeking
+//   - transcribe
+//   - append (daisy-chaining)
+//   - automatically closes input streams when end reached
 
 /** plays the role of InputStream, OutputStream, Reader and Writer, with logging and unchecked exceptions */
 public class Stream {
 
     protected final In in;
     protected final Out out;
-    private         StringBuffer log = loggingEnabled ? new StringBuffer(16 * 1024) : null;
     private         String newLine = "\r\n";
-    private         Stream in_next = null;
-
-    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 static boolean loggingEnabled = "true".equals(System.getProperty("ibex.io.stream.logEnabled", "false"));
-    public static boolean loggingEnabled = true;
-
-    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); }
-    }
-
-    public void transcribe(StringBuffer out) {
-        //try {
-            char[] buf = new char[1024];
-            while(true) {
-                int numread = in.read(buf, 0, buf.length);
-                if (numread==-1) { in.close(); return; }
-                out.append(buf, 0, numread);
-            }
-            //} catch (IOException ioe) { ioe(ioe); }
-    }
 
-    public static int countLines(Stream s) {
-        int ret = 0;
-        while(s.readln() != null) ret++;
-        s.close();
-        return ret;
-    }
-
-    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); }
@@ -76,262 +41,131 @@ public class Stream {
         try { this.out = new Stream.Out(s.getOutputStream()); } catch (IOException e) { ioe(e); throw new Error(); }
     }
 
-    static int ioe(IOException e) {
-        if (e instanceof SocketException && e.toString().indexOf("Connection reset")!=-1)
-            throw new Closed(e.getMessage());
-        throw new StreamException(e);
-    }
-    public static class StreamException extends RuntimeException {
-        public StreamException(Exception e) { super(e); }
-        public StreamException(String s)    { super(s); }
-    }
-    public static class EOF             extends StreamException  { public EOF() { super("End of stream"); } }
-    public static class Closed          extends StreamException  { public Closed(String s) { super(s); } }
-
-
-    private static Hashtable blocker = new Hashtable();
-    public static void kill(Thread thread) {
-        Stream block = (Stream)blocker.get(thread);
-        if (block == null) {
-            Log.warn(Stream.class, "thread " + thread + " is not blocked on a stream");
-        } else {
-            Log.warn(Stream.class, "asynchronously closing " + block);
-            block.close();
-        }
-    }
-
-    public char   peekc()                          {
-        Stream old = (Stream)blocker.get(Thread.currentThread());
-        try {
-            blocker.put(Thread.currentThread(), this);
-            flush(); return in.getc(true);
-        } finally { if (old == null) blocker.remove(Thread.currentThread()); else blocker.put(Thread.currentThread(), old); }
-    }
-    public char   getc()                           {
-        Stream old = (Stream)blocker.get(Thread.currentThread());
-        try {
-            blocker.put(Thread.currentThread(), this);
-            flush(); char ret = in.getc(false); log(ret); return ret;
-        } finally { if (old == null) blocker.remove(Thread.currentThread()); else blocker.put(Thread.currentThread(), old); }
-    }   
-    public String readln()                         {
-        Stream old = (Stream)blocker.get(Thread.currentThread());
-        try {
-            blocker.put(Thread.currentThread(), this);
-            flush(); String s = in.readln(); log(s); log('\n'); return s;
-        } finally { if (old == null) blocker.remove(Thread.currentThread()); else blocker.put(Thread.currentThread(), old); }
-    }
-    
-    public void   print(String s) {
-        Stream old = (Stream)blocker.get(Thread.currentThread());
-        try {
-            blocker.put(Thread.currentThread(), this);
-            logWrite(s);
-            out.write(s);
-            flush();
-        } finally { if (old == null) blocker.remove(Thread.currentThread()); else blocker.put(Thread.currentThread(), old); }
-    }
-    public void   println(String s) {
-        Stream old = (Stream)blocker.get(Thread.currentThread());
-        try {
-            blocker.put(Thread.currentThread(), this);
-            logWrite(s);
-            logWrite(newLine);
-            out.write(s);
-            out.write(newLine);
-            flush();
-        } finally { if (old == null) blocker.remove(Thread.currentThread()); else blocker.put(Thread.currentThread(), old); }
-    }
-    public void   flush()                          {
-        Stream old = (Stream)blocker.get(Thread.currentThread());
-        try {
-            blocker.put(Thread.currentThread(), this);
-            if (out != null) try { out.w.flush(); } catch(IOException e) { ioe(e); }
-        } finally { if (old == null) blocker.remove(Thread.currentThread()); else blocker.put(Thread.currentThread(), old); }
-    }
-    public int    read(byte[] b, int off, int len) {
-        Stream old = (Stream)blocker.get(Thread.currentThread());
-        try {
-            blocker.put(Thread.currentThread(), this);
-            flush();
-            int ret = in.readBytes(b, off, len);
-            if (log != null) log("\n[read " + ret + " bytes of binary data ]\n");
-            nnl = false;
-            return ret;
-        } finally { if (old == null) blocker.remove(Thread.currentThread()); else blocker.put(Thread.currentThread(), old); }
-    }
-    public int    read(char[] c, int off, int len) {
-        Stream old = (Stream)blocker.get(Thread.currentThread());
-        try {
-            blocker.put(Thread.currentThread(), this);
-            flush();
-            int ret = in.read(c, off, len);
-            if (log != null && ret != -1) log(new String(c, off, ret));
-            return ret;
-        } finally { if (old == null) blocker.remove(Thread.currentThread()); else blocker.put(Thread.currentThread(), old); }
-    }
-
-    public void   unread(String s)                 { in.unread(s); }
+    // Main API //////////////////////////////////////////////////////////////////////////////
 
-    /** should not throw exceptions */
+    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 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; }
 
-
-    /** dumps the connection log into a file */
-    public  String dumpLog()         { if (log==null) return ""; String ret = log.toString(); log = new StringBuffer(16 * 1024); return ret; }
-    private void  log(String s)      { if(log==null) return; if (!nnl) Log.note("\n[read ] "); Log.note(s + "\n"); nnl=false; if (log != null) log.append(s); }
-    private void  logWrite(String s) { if(log==null) return; if (nnl) Log.note("\n"); Log.note("[write] "+s+"\n"); nnl=false; if (log != null) log.append(s); }
-    private void  log(char c)        { if(log==null) return; if (c == '\r') return; if (!nnl) Log.note("[read ] "); Log.note(c+""); nnl = c != '\n'; if (log != null) log.append(c); }
-    private boolean nnl = false;
+    public void   setInputDigest(Digest d) { in.bbis.digest = d; }
 
     private static class Out extends BufferedOutputStream {
-        private Writer w = new BufferedWriter(new OutputStreamWriter(this));
+        private Writer writer = new OutputStreamWriter(this);
         public Out(OutputStream out) { super(out); }
-        public void close() { try { super.close(); } catch (Exception e) { Log.error(this, e); } }
-        public void write(String s) { try { w.write(s); } catch (IOException e) { ioe(e); } }
+        public  void close() { try { super.close(); } catch (Exception e) { Log.error(this, e); } }
+        public  void write(String s) { try { writer.write(s); } catch (IOException e) { ioe(e); } }
+        private void flushWriter() { try { writer.flush(); } catch (IOException e) { ioe(e); } }
+        private boolean flushing = false;
+        public  void flush() {
+            if (flushing) return;
+            try {
+                flushing = true;
+                try {
+                    writer.flush();
+                } finally { flushing = false; }
+                super.flush();
+            } catch (IOException e) { ioe(e); }
+        }
     }
 
     private class In extends InputStream {
-        public final Reader reader = new InputStreamReader(this);
-        private /*final*/ InputStream orig;
-        public In(InputStream in) { orig = in; }
-
-        char[] cbuf = new char[8192];
-        int cstart = 0;
-        int cend = 0;
-
-        byte[] buf = new byte[8192];
-        int start = 0;
-        int end = 0;
-
-        boolean flushing = false;
-
-        public int available() { return flushing ? 0 : (end - start); }
-        public void close() { try {
-            if (orig!=null) orig.close(); 
-            if (in_next != null) in_next.close();  // FIXME: correct?
-        } catch (Exception e) { Log.error(this, e); } }
-
-        public char getc(boolean peek) { try {
-            if (cstart == cend) {
-                cstart = 0;
-                cend = reader.read(cbuf, 0, cbuf.length);
-                if (cend == -1) {
-                    reader.close();
-                    cend = cstart;
-                    if (in_next == null) throw new EOF();
-                    return getc(peek);
-                }
-            }
-            return peek ? cbuf[cstart] : cbuf[cstart++];
-        } catch (IOException e) { return (char)ioe(e); } }
-
-        public String readln() { try {
-            while(true) {
-                for(int i=cstart; i<cend; i++)
-                    if (cbuf[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 = cstart;
-                        int len = i-cstart;
-                        cstart = i+1;
-                        if (cbuf[begin] == '\r') { begin++; len--; }
-                        while (len > 0 && cbuf[begin+len-1] == '\r') { len--; }
-                        return new String(cbuf, begin, len);
+        private ByteBufInputStream bbis;
+        private CharBufReader cbr;
+        public  Reader reader;
+        private Writer unreader;
+
+        public char getc(boolean peek) { return cbr.getc(peek); }
+        public String readln() { return cbr.readln(); }
+        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 In(InputStream in) {
+            bbis = new ByteBufInputStream(in) {
+                    public void preread() {
+                        cbr.unbuffer(unreader);
+                        try {
+                            if (!cbr.ready()) return;
+                        } catch (IOException e) { ioe(e); }
+                        char[] c = new char[20];
+                        while(true) {
+                            try {
+                                if (!cbr.ready()) break;
+                                int numread = cbr.read(c, 0, c.length);
+                                if (numread == -1) break;
+                                unreader.write(c, 0, numread);
+                            } catch (IOException e) { ioe(e); }
+                        }
                     }
-                ensurec(256);
-                int numread = reader.read(cbuf, cend, cbuf.length - cend);
-                if (numread == -1) {
-                    reader.close();
-                    if (cstart == cend) return null;
-                    String ret = new String(cbuf, cstart, cend-cstart);
-                    cstart = cend = 0;
-                    return ret;
-                }
-                cend += numread;
-            }
-        } catch (IOException e) { ioe(e); return null; } }
+                };
+            unreader = new OutputStreamWriter(new UnReaderStream(bbis));
+            cbr = new CharBufReader(new InputStreamReader(bbis));
+        }
+    }
 
-        public int read(char[] c, int pos, int len) { try {
-            if (cstart == cend) {
-                cstart = 0;
-                cend = reader.read(cbuf, 0, cbuf.length);
-                if (cend == -1) { reader.close(); cend = cstart; return -1; }
-            }
-            if (len > cend - cstart) len = cend - cstart;
-            System.arraycopy(cbuf, cstart, c, pos, len);
-            cstart += len;
-            return len;
-        } catch (IOException e) { ioe(e); return -1; } }
+    // Utilities: append() and transcribe() ///////////////////////////////////////////////////////
 
-        public int readBytes(byte[] b, int pos, int len) { flushchars(); return read(b, pos, len); }
-        public int read() { byte[] b = new byte[1]; if (read(b, 0, 1) == -1) return -1; return (int)b[0]; } 
-        public int read(byte[] b, int pos, int len) { try {
-            if (start == end) {
-                start = 0;
-                end = orig.read(buf, 0, buf.length);
-                if (end == -1) {
-                    orig.close();
-                    end = start;
-                    if (in_next==null) return -1;
-                    return in_next.read(b, pos, len);
-                }
-            }
-            if (len > end - start) len = end - start;
-            System.arraycopy(buf, start, b, pos, len);
-            start += len;
-            return len;
-        } catch (IOException e) { ioe(e); return -1; } }
+    public Stream append(String in_next) { return appendStream(new Stream(in_next)); }
+    public Stream appendStream(Stream in_next) { in.bbis.appendStream(in_next); return this; }
 
-        private void growc(int s){char[] cbuf2=new char[cbuf.length+s*2];System.arraycopy(cbuf,0,cbuf2,0,cbuf.length);cbuf=cbuf2; }
-        private void shiftc() {
-            char[] cbuf2 = new char[cbuf.length];
-            System.arraycopy(cbuf, cstart, cbuf2, 0, cend-cstart);
-            cend -= cstart;
-            cstart = 0;
-            cbuf = cbuf2;
+    public void transcribe(Stream out) { transcribe(out, false); }
+    public void transcribe(Stream out, boolean close) {
+        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);
         }
-        private void ensurec(int space) { if (cend-cstart+space>cbuf.length) growc(space); if (cend+space>cbuf.length) shiftc(); }
-
-        private void growb(int s) { byte[] buf2 = new byte[buf.length+s*2]; System.arraycopy(buf,0,buf2,0,buf.length); buf=buf2; }
-        private void shiftb() { System.arraycopy(buf, start, buf, 0, end-start); end -= start; start = 0; }
-        private void ensureb(int space) { if (end-start+space>buf.length) growb(space); if (end+space>buf.length) shiftb(); }
-        private void ensureb2(int space) { if (end-start+space>buf.length) growb(space); if (start<space) unshiftb(); }
-        private void unshiftb() {
-            System.arraycopy(buf,start,buf,buf.length-(end-start),end-start);start=buf.length-(end-start);end=buf.length; }
-
-        public  void unread(String s) { ensurec(s.length()); s.getChars(0, s.length(), cbuf, cend); cend += s.length(); }
+        if (close) out.close();
+    }
 
-        private void flushchars() {
-            try {
-                flushing = true;
-                for(; reader.ready(); reader.read(cbuf, cend++, 1)) ensurec(10);
-                if (cend>cstart)
-                    unreader.write(cbuf, cstart, cend-cstart);
-                cstart = cend = 0;
-                unreader.flush();
-            } catch (IOException e) { ioe(e);
-            } finally { flushing = false; }
+    public void transcribe(StringBuffer out) {
+        char[] buf = new char[1024];
+        while(true) {
+            int numread = in.readChars(buf, 0, buf.length);
+            if (numread==-1) { in.close(); return; }
+            out.append(buf, 0, numread);
         }
+    }
 
-        Writer unreader = new OutputStreamWriter(new InOutputStream());
-       private class InOutputStream extends OutputStream {
-           public void close() { }
-           public void write(int i) throws IOException { byte[] b = new byte[1]; b[0] = (byte)i; write(b, 0, 1); }
-           public void write(byte[] b) throws IOException { write(b, 0, b.length); }
-           public void write(byte[] b, int p, int l) {
-               ensureb2(l);
-               System.arraycopy(b, p, buf, start-l, l);
-               start -= l;
-           }
-       }
+    public static int countLines(Stream s) {
+        int ret = 0;
+        while(s.readln() != null) ret++;
+        s.close();
+        return ret;
     }
 
-    public static interface Transformer {
-        public Stream transform(Stream in);
+    // 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) {
+        if (e instanceof SocketException && e.toString().indexOf("Connection reset")!=-1)
+            throw new Closed(e.getMessage());
+        throw new StreamException(e);
+    }
+    public static class StreamException extends RuntimeException {
+        public StreamException(Exception e) { super(e); }
+        public StreamException(String s)    { super(s); }
+    }
+    public static class EOF             extends StreamException  { public EOF() { super("End of stream"); } }
+    public static class Closed          extends StreamException  { public Closed(String s) { super(s); } }
 }