X-Git-Url: http://git.megacz.com/?p=org.ibex.io.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fio%2FStream.java;h=019a2f4a142086df3d1850ad87648a0684dd99bc;hp=83a81939aaea175f2fb4d7f962cddb9981215332;hb=ce302fede4773b90c042c0ca78fff8d28204f4a7;hpb=64cd1d56deb8a25285b8edd51b2f78b54fae66d3 diff --git a/src/org/ibex/io/Stream.java b/src/org/ibex/io/Stream.java index 83a8193..019a2f4 100644 --- a/src/org/ibex/io/Stream.java +++ b/src/org/ibex/io/Stream.java @@ -1,4 +1,7 @@ -// Copyright 2004 Adam Megacz, see the COPYING file for licensing [LGPL] +// Copyright 2000-2005 the Contributors, as shown in the revision logs. +// Licensed under the Apache Public Source License 2.0 ("the License"). +// You may not use this file except in compliance with the License. + package org.ibex.io; import java.io.*; @@ -14,13 +17,53 @@ public class Stream { protected final Out out; private StringBuffer log = loggingEnabled ? new StringBuffer(16 * 1024) : null; private String newLine = "\r\n"; + private Stream in_next = 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) { + try { + byte[] buf = new byte[1024]; + while(true) { + int numread = in.read(buf, 0, buf.length); + if (numread==-1) return; + out.out.write(buf, 0, numread); + } + } 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 boolean loggingEnabled = System.getProperty("ibex.io.stream.logEnabled", "true") != null; + 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); } @@ -118,12 +161,12 @@ public class Stream { 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); } @@ -136,9 +179,9 @@ public class Stream { 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]; @@ -152,13 +195,20 @@ public class Stream { 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); } } @@ -246,15 +296,21 @@ public class Stream { } finally { flushing = false; } } - Writer unreader = new OutputStreamWriter(new OutputStream() { - public void close() { } - public void write(int i) throws IOException { byte[] b = new byte[1]; b[0] = (byte)i; write(b, 0, 1); } - public void write(byte[] b) throws IOException { write(b, 0, b.length); } - public void write(byte[] b, int p, int l) { - ensureb2(l); - System.arraycopy(b, p, buf, start-l, l); - start -= l; - } - }); + Writer unreader = new OutputStreamWriter(new InOutputStream()); + private class InOutputStream extends OutputStream { + public void close() { } + public void write(int i) throws IOException { byte[] b = new byte[1]; b[0] = (byte)i; write(b, 0, 1); } + public void write(byte[] b) throws IOException { write(b, 0, b.length); } + public void write(byte[] b, int p, int l) { + ensureb2(l); + System.arraycopy(b, p, buf, start-l, l); + start -= l; + } + } } + + public static interface Transformer { + public Stream transform(Stream in); + } + }