massive bugfixes in stream library
[org.ibex.io.git] / src / org / ibex / io / Stream.java
1 // Copyright 2000-2007 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.io;
6
7 import java.io.*;
8 import java.net.*;
9 import java.util.*;
10 import java.util.zip.*;
11 import org.ibex.util.*;
12
13 // Features:
14 //   - automatically flush writer before reading on linked read/write pairs
15 //   - no checked exceptions thrown
16 //   - unified write(char), print(char), and write(byte)
17 //   - unreading/peeking
18 //   - transcribe
19 //   - append (daisy-chaining)
20 //   - automatically closes input streams when end reached
21
22 /** plays the role of InputStream, OutputStream, Reader and Writer, with logging and unchecked exceptions */
23 public class Stream {
24
25     protected final In in;
26     protected final Out out;
27     private         String newLine = "\r\n";
28
29     public  Stream(byte[] b, int off, int len)       { this.in = new Stream.In(new ByteArrayInputStream(b, off, len)); this.out=null; }
30     public  Stream(InputStream in)                   { this.in = new Stream.In(in); this.out = null; }
31     public  Stream(                OutputStream out) { this.in = null;              this.out = new Stream.Out(out); }
32     public  Stream(InputStream in, OutputStream out) { this.in = new Stream.In(in); this.out = new Stream.Out(out); }
33     public  Stream(String s)                         { this(new ByteArrayInputStream(s.getBytes())); }
34     public  Stream(File f)                           {
35         try { this.in = new Stream.In(new FileInputStream(f)); } catch (IOException e) { ioe(e); throw new Error(); }
36         this.out = null;
37     }
38     public  Stream(Socket s) {
39         try { this.in = new Stream.In(s.getInputStream());    } catch (IOException e) { ioe(e); throw new Error(); }
40         try { this.out = new Stream.Out(s.getOutputStream()); } catch (IOException e) { ioe(e); throw new Error(); }
41     }
42
43     // Main API //////////////////////////////////////////////////////////////////////////////
44
45     public char   peekc()                          { flush(); return in.getc(true); }
46     public char   getc()                           { flush(); return in.getc(false); }
47     public String readln()                         { flush(); return in.readln(); }
48     public void   print(String s)                  { out.write(s); flush(); }
49     public void   println(String s)                { print(s); print(newLine); }
50     public void   flush()                          { if (out != null) out.flushWriter(); }
51     public void writeBytes(byte[] b, int off, int len) { try { out.write(b, off, len); } catch (IOException e) { ioe(e); } }
52     public int    read(byte[] b, int off, int len) { flush(); return in.readBytes(b, off, len); }
53     public int    read(char[] c, int off, int len) { flush(); return in.readChars(c, off, len); }
54     public void   close()                          { try { if (in!=null) in.close(); } finally { if (out!=null) out.close(); } }
55     public void   setNewline(String s)             { newLine = s; }
56     public InputStream getInputStream() { return in; }
57
58     private static class Out extends BufferedOutputStream {
59         private Writer writer = new OutputStreamWriter(this);
60         public Out(OutputStream out) { super(out); }
61         public  void close() { try { super.close(); } catch (Exception e) { Log.error(this, e); } }
62         public  void write(String s) { try { writer.write(s); } catch (IOException e) { ioe(e); } }
63         private void flushWriter() { try { writer.flush(); } catch (IOException e) { ioe(e); } }
64         private boolean flushing = false;
65         public  void flush() {
66             if (flushing) return;
67             try {
68                 flushing = true;
69                 try {
70                     writer.flush();
71                 } finally { flushing = false; }
72                 super.flush();
73             } catch (IOException e) { ioe(e); }
74         }
75     }
76
77     private class In extends InputStream {
78         private ByteBufInputStream bbis;
79         private CharBufReader cbr;
80         public  Reader reader;
81         private Writer unreader;
82
83         public char getc(boolean peek) { return cbr.getc(peek); }
84         public String readln() { return cbr.readln(); }
85         public int read() { return bbis.read(); }
86         public int read(byte[] b) { try { return bbis.read(b); } catch (IOException e) { ioe(e); return 0; } }
87         public int read(byte[] b, int off, int len) { return bbis.read(b, off, len); }
88         public void close() { try { cbr.close(); } catch (Exception e) { Log.error(this, e); } }
89         public int readBytes(byte[] b, int off, int len) { return bbis.read(b, off, len); }
90         public int readChars(char[] c, int off, int len) { return cbr.read(c, off, len); }
91
92         public In(InputStream in) {
93             bbis = new ByteBufInputStream(in) {
94                     public void preread() {
95                         cbr.unbuffer(unreader);
96                         try {
97                             if (!cbr.ready()) return;
98                         } catch (IOException e) { ioe(e); }
99                         char[] c = new char[20];
100                         while(true) {
101                             try {
102                                 if (!cbr.ready()) break;
103                                 int numread = cbr.read(c, 0, c.length);
104                                 if (numread == -1) break;
105                                 unreader.write(c, 0, numread);
106                             } catch (IOException e) { ioe(e); }
107                         }
108                     }
109                 };
110             unreader = new OutputStreamWriter(new UnReaderStream(bbis));
111             cbr = new CharBufReader(new InputStreamReader(bbis));
112         }
113     }
114
115     // Utilities: append() and transcribe() ///////////////////////////////////////////////////////
116
117     public Stream append(String in_next) { return appendStream(new Stream(in_next)); }
118     public Stream appendStream(Stream in_next) { in.bbis.appendStream(in_next); return this; }
119
120     public void transcribe(Stream out) { transcribe(out, false); }
121     public void transcribe(Stream out, boolean close) {
122         byte[] buf = new byte[1024];
123         while(true) {
124             int numread = in.read(buf, 0, buf.length);
125             if (numread==-1) { in.close(); break; }
126             out.writeBytes(buf, 0, numread);
127         }
128         if (close) out.close();
129     }
130
131     public void transcribe(StringBuffer out) {
132         char[] buf = new char[1024];
133         while(true) {
134             int numread = in.readChars(buf, 0, buf.length);
135             if (numread==-1) { in.close(); return; }
136             out.append(buf, 0, numread);
137         }
138     }
139
140     public static int countLines(Stream s) {
141         int ret = 0;
142         while(s.readln() != null) ret++;
143         s.close();
144         return ret;
145     }
146
147     // FIXME: ugly
148     public static int countBytes(Stream s) {
149         int ret = 0;
150         while(s.in.read() != -1) ret++;
151         s.close();
152         return ret;
153     }
154
155     // Exceptions //////////////////////////////////////////////////////////////////////////////
156
157     static int ioe(IOException e) {
158         if (e instanceof SocketException && e.toString().indexOf("Connection reset")!=-1)
159             throw new Closed(e.getMessage());
160         throw new StreamException(e);
161     }
162     public static class StreamException extends RuntimeException {
163         public StreamException(Exception e) { super(e); }
164         public StreamException(String s)    { super(s); }
165     }
166     public static class EOF             extends StreamException  { public EOF() { super("End of stream"); } }
167     public static class Closed          extends StreamException  { public Closed(String s) { super(s); } }
168 }