improve exception handling
[org.ibex.io.git] / src / org / ibex / io / Stream.java
index 019a2f4..8bfa8bc 100644 (file)
@@ -25,17 +25,20 @@ public class Stream {
         return this;
     }
 
-    public static boolean loggingEnabled = "true".equals(System.getProperty("ibex.io.stream.logEnabled", "false"));
+    //public static boolean loggingEnabled = "true".equals(System.getProperty("ibex.io.stream.logEnabled", "false"));
+    public static boolean loggingEnabled = true;
 
-    public void transcribe(Stream out) {
+    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) return;
+                if (numread==-1) break;
                 out.out.write(buf, 0, numread);
             }
-        } catch (IOException ioe) { throw new StreamException(ioe); }
+           if (close) out.close();
+        } catch (IOException ioe) { ioe(ioe); }
     }
 
     public void transcribe(StringBuffer out) {
@@ -46,7 +49,7 @@ public class Stream {
                 if (numread==-1) return;
                 out.append(buf, 0, numread);
             }
-            //} catch (IOException ioe) { throw new StreamException(ioe); }
+            //} catch (IOException ioe) { ioe(ioe); }
     }
 
     public static int countLines(Stream s) {
@@ -61,15 +64,19 @@ public class Stream {
     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); }
+        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); }
@@ -125,6 +132,7 @@ public class Stream {
         try {
             blocker.put(Thread.currentThread(), this);
             logWrite(s);
+            logWrite(newLine);
             out.write(s);
             out.write(newLine);
             flush();
@@ -161,6 +169,7 @@ public class Stream {
 
     public void   unread(String s)                 { in.unread(s); }
 
+    /** 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; }