protected final Out out;
private StringBuffer log = loggingEnabled ? new StringBuffer(16 * 1024) : null;
private String newLine = "\r\n";
+ private Stream in_next = null;
- public static boolean loggingEnabled = System.getProperty("ibex.io.stream.logEnabled", "true") != null;
+ public Stream appendStream(Stream in_next) {
+ if (this.in_next != null) return this.in_next.appendStream(in_next);
+ this.in_next = in_next;
+ return this;
+ }
+
+ public static boolean loggingEnabled = "true".equals(System.getProperty("ibex.io.stream.logEnabled", "false"));
+
+ 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) break;
+ out.out.write(buf, 0, numread);
+ }
+ if (close) out.close();
+ } catch (IOException ioe) { throw new StreamException(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) return;
+ out.append(buf, 0, numread);
+ }
+ //} catch (IOException ioe) { throw new StreamException(ioe); }
+ }
+ public static int countLines(Stream s) {
+ int ret = 0;
+ while(s.readln() != null) ret++;
+ 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) { throw new StreamException(e); }
+ 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 {
blocker.put(Thread.currentThread(), this);
logWrite(s);
+ logWrite(newLine);
out.write(s);
out.write(newLine);
flush();
public void unread(String s) { in.unread(s); }
- public void close() { try { in.close(); } finally { out.close(); } }
+ 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); }
public void write(String s) { try { w.write(s); } catch (IOException e) { ioe(e); } }
}
- private static class In extends InputStream {
- public Reader reader = new InputStreamReader(this);
- private final InputStream orig;
+ private class In extends InputStream {
+ public final Reader reader = new InputStreamReader(this);
+ private /*final*/ InputStream orig;
public In(InputStream in) { orig = in; }
char[] cbuf = new char[8192];
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(); } 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) { cend = cstart; throw new EOF(); }
+ if (cend == -1) {
+ 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);
+ }
}
return peek ? cbuf[cstart] : cbuf[cstart++];
} catch (IOException e) { return (char)ioe(e); } }
}
}
}
+
+ public static interface Transformer {
+ public Stream transform(Stream in);
+ }
+
}