remember to put newlines in the log
[org.ibex.io.git] / src / org / ibex / io / Stream.java
index b31c9a6..558e0ca 100644 (file)
@@ -1,4 +1,7 @@
-// Copyright 2004 Adam Megacz, see the COPYING file for licensing [LGPL]
+// Copyright 2000-2005 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.*;
@@ -14,13 +17,53 @@ public class Stream {
     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 appendStream(Stream in_next) {
+        if (this.in_next != null) return this.in_next.appendStream(in_next);
+        this.in_next = in_next;
+        return this;
+    }
 
-    public static boolean loggingEnabled = System.getProperty("ibex.io.stream.logEnabled", "true") != null;
+    public static boolean loggingEnabled = "true".equals(System.getProperty("ibex.io.stream.logEnabled", "false"));
+
+    public void transcribe(Stream out) {
+        try {
+            byte[] buf = new byte[1024];
+            while(true) {
+                int numread = in.read(buf, 0, buf.length);
+                if (numread==-1) return;
+                out.out.write(buf, 0, numread);
+            }
+        } catch (IOException ioe) { throw new StreamException(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) return;
+                out.append(buf, 0, numread);
+            }
+            //} catch (IOException ioe) { throw new StreamException(ioe); }
+    }
+
+    public static int countLines(Stream s) {
+        int ret = 0;
+        while(s.readln() != null) ret++;
+        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(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(String s)                         { this(new ByteArrayInputStream(s.getBytes())); }
+    public  Stream(File f)                           {
+        try { this.in = new Stream.In(new FileInputStream(f)); } catch (IOException e) { throw new StreamException(e); }
+        this.out = null;
+    }
     public  Stream(Socket s) {
         try { this.in = new Stream.In(s.getInputStream());    } catch (IOException e) { throw new StreamException(e); }
         try { this.out = new Stream.Out(s.getOutputStream()); } catch (IOException e) { throw new StreamException(e); }
@@ -82,6 +125,7 @@ public class Stream {
         try {
             blocker.put(Thread.currentThread(), this);
             logWrite(s);
+            logWrite(newLine);
             out.write(s);
             out.write(newLine);
             flush();
@@ -118,12 +162,12 @@ public class Stream {
 
     public void   unread(String s)                 { in.unread(s); }
 
-    public void   close()                          { try { in.close(); } finally { out.close(); } }
+    public void   close()                          { try { if (in!=null) in.close(); } finally { if (out!=null) out.close(); } }
     public void   setNewline(String s)             { newLine = s; }
 
 
     /** dumps the connection log into a file */
-    public  String dumpLog()         { String ret = log.toString(); log = new StringBuffer(16 * 1024); return ret; }
+    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); }
@@ -136,9 +180,9 @@ public class Stream {
         public void write(String s) { try { w.write(s); } catch (IOException e) { ioe(e); } }
     }
 
-    private static class In extends InputStream {
-        public Reader reader = new InputStreamReader(this);
-        private final InputStream orig;
+    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];
@@ -152,13 +196,20 @@ public class Stream {
         boolean flushing = false;
 
         public int available() { return flushing ? 0 : (end - start); }
-        public void close() { try { orig.close(); } catch (Exception e) { Log.error(this, e); } }
+        public void close() { try { if (orig!=null) orig.close(); } 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) { cend = cstart; throw new EOF(); }
+                if (cend == -1) {
+                    cend = cstart;
+                    if (in_next == null) throw new EOF();
+                    // FIXME: sketchy
+                    orig = in_next.in.orig;
+                    in_next = in_next.in_next;
+                    return getc(peek);
+                }
             }
             return peek ? cbuf[cstart] : cbuf[cstart++];
         } catch (IOException e) { return (char)ioe(e); } }
@@ -258,4 +309,9 @@ public class Stream {
            }
        }
     }
+
+    public static interface Transformer {
+        public Stream transform(Stream in);
+    }
+
 }