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