remove blocker nonsense from org.ibex.io
[org.ibex.io.git] / src / org / ibex / io / Stream.java
index c1a1424..1d03c20 100644 (file)
@@ -17,24 +17,70 @@ 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;
+    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) return this.in_next.appendStream(in_next);
-        this.in_next = 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(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) { ioe(e); throw new Error(); }
+        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); }
+        try { this.in = new Stream.In(s.getInputStream());    } catch (IOException e) { ioe(e); throw new Error(); }
+        try { this.out = new Stream.Out(s.getOutputStream()); } catch (IOException e) { ioe(e); throw new Error(); }
     }
 
-    private static int ioe(Exception e) { throw new StreamException(e); }
+    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); }
@@ -43,95 +89,62 @@ public class 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); }
+        flush();
+        return in.getc(true);
     }
     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); }
+        flush();
+        char ret = in.getc(false);
+        log(ret);
+        return ret;
     }   
     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); }
+        flush();
+        String s = in.readln();
+        log(s);
+        log('\n');
+        return s;
     }
     
     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); }
+        logWrite(s);
+        out.write(s);
+        flush();
     }
     public void   println(String s) {
-        Stream old = (Stream)blocker.get(Thread.currentThread());
-        try {
-            blocker.put(Thread.currentThread(), this);
-            logWrite(s);
-            out.write(s);
-            out.write(newLine);
-            flush();
-        } finally { if (old == null) blocker.remove(Thread.currentThread()); else blocker.put(Thread.currentThread(), old); }
+        logWrite(s);
+        logWrite(newLine);
+        out.write(s);
+        out.write(newLine);
+        flush();
     }
     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); }
+        if (out != null) try { out.w.flush(); } catch(IOException e) { ioe(e); }
     }
     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); }
+        flush();
+        int ret = in.readBytes(b, off, len);
+        if (log != null) log("\n[read " + ret + " bytes of binary data ]\n");
+        nnl = false;
+        return ret;
     }
     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); }
+        flush();
+        int ret = in.read(c, off, len);
+        if (log != null && ret != -1) log(new String(c, off, ret));
+        return ret;
     }
 
     public void   unread(String s)                 { in.unread(s); }
 
-    public void   close()                          { try { in.close(); } finally { out.close(); } }
+    /** should not throw exceptions */
+    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); }
@@ -160,18 +173,19 @@ 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(); 
+            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();
-                    // FIXME: sketchy
-                    orig = in_next.in.orig;
-                    in_next = in_next.in_next;
                     return getc(peek);
                 }
             }
@@ -196,6 +210,7 @@ public class Stream {
                 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;
@@ -209,7 +224,7 @@ public class Stream {
             if (cstart == cend) {
                 cstart = 0;
                 cend = reader.read(cbuf, 0, cbuf.length);
-                if (cend == -1) { cend = cstart; return -1; }
+                if (cend == -1) { reader.close(); cend = cstart; return -1; }
             }
             if (len > cend - cstart) len = cend - cstart;
             System.arraycopy(cbuf, cstart, c, pos, len);
@@ -223,7 +238,12 @@ public class Stream {
             if (start == end) {
                 start = 0;
                 end = orig.read(buf, 0, buf.length);
-                if (end == -1) { end = start; return -1; }
+                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);
@@ -253,8 +273,9 @@ public class Stream {
         private void flushchars() {
             try {
                 flushing = true;
-                for(; reader.ready(); reader.read(cbuf, cend++, 1)) ensurec(1);
-                unreader.write(cbuf, cstart, cend);
+                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);
@@ -273,4 +294,9 @@ public class Stream {
            }
        }
     }
+
+    public static interface Transformer {
+        public Stream transform(Stream in);
+    }
+
 }