From 229e1d6333835aa449e9390a0175f0f89ce014b0 Mon Sep 17 00:00:00 2001 From: adam Date: Mon, 21 Feb 2005 09:26:00 +0000 Subject: [PATCH] added Stream.transcribe(), some fixups darcs-hash:20050221092600-5007d-4cbff43f3c5e4d67c598b785022db32c17ea1d96.gz --- src/org/ibex/io/Stream.java | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/org/ibex/io/Stream.java b/src/org/ibex/io/Stream.java index c1a1424..19363b0 100644 --- a/src/org/ibex/io/Stream.java +++ b/src/org/ibex/io/Stream.java @@ -25,10 +25,34 @@ public class Stream { 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 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); } @@ -126,12 +150,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); } @@ -160,7 +184,7 @@ 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) { @@ -273,4 +297,9 @@ public class Stream { } } } + + public static interface Transformer { + public Stream transform(Stream in); + } + } -- 1.7.10.4