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.
10 import java.util.zip.*;
11 import org.ibex.util.*;
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
19 // - append (daisy-chaining)
20 // - automatically closes input streams when end reached
22 /** plays the role of InputStream, OutputStream, Reader and Writer, with logging and unchecked exceptions */
25 protected final In in;
26 protected final Out out;
27 private String newLine = "\r\n";
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(); }
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(); }
43 // Main API //////////////////////////////////////////////////////////////////////////////
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; }
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;
71 } finally { flushing = false; }
73 } catch (IOException e) { ioe(e); }
77 private class In extends InputStream {
78 private ByteBufInputStream bbis;
79 private CharBufReader cbr;
81 private Writer unreader;
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); }
92 public In(InputStream in) {
93 bbis = new ByteBufInputStream(in) {
94 public void preread() {
95 cbr.unbuffer(unreader);
97 if (!cbr.ready()) return;
98 } catch (IOException e) { ioe(e); }
99 char[] c = new char[20];
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); }
110 unreader = new OutputStreamWriter(new UnReaderStream(bbis));
111 cbr = new CharBufReader(new InputStreamReader(bbis));
115 // Utilities: append() and transcribe() ///////////////////////////////////////////////////////
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; }
120 public void transcribe(Stream out) { transcribe(out, false); }
121 public void transcribe(Stream out, boolean close) {
122 byte[] buf = new byte[1024];
124 int numread = in.read(buf, 0, buf.length);
125 if (numread==-1) { in.close(); break; }
126 out.writeBytes(buf, 0, numread);
128 if (close) out.close();
131 public void transcribe(StringBuffer out) {
132 char[] buf = new char[1024];
134 int numread = in.readChars(buf, 0, buf.length);
135 if (numread==-1) { in.close(); return; }
136 out.append(buf, 0, numread);
140 public static int countLines(Stream s) {
142 while(s.readln() != null) ret++;
148 public static int countBytes(Stream s) {
150 while(s.in.read() != -1) ret++;
155 // Exceptions //////////////////////////////////////////////////////////////////////////////
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);
162 public static class StreamException extends RuntimeException {
163 public StreamException(Exception e) { super(e); }
164 public StreamException(String s) { super(s); }
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); } }