From: adam Date: Thu, 5 Jul 2007 02:13:09 +0000 (+0000) Subject: mostly intert changes, cleanup of Stream X-Git-Url: http://git.megacz.com/?p=org.ibex.io.git;a=commitdiff_plain;h=146c5fbaa3c5b80ad4da96fed82ea701fac92c6c mostly intert changes, cleanup of Stream darcs-hash:20070705021309-5007d-c574fbccb08f9ef9686a35246ac7d6468c90f9e4.gz --- diff --git a/src/org/ibex/io/Stream.java b/src/org/ibex/io/Stream.java index 04aef23..80e252b 100644 --- a/src/org/ibex/io/Stream.java +++ b/src/org/ibex/io/Stream.java @@ -1,4 +1,4 @@ -// Copyright 2000-2005 the Contributors, as shown in the revision logs. +// Copyright 2000-2007 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. @@ -10,6 +10,15 @@ import java.util.*; import java.util.zip.*; import org.ibex.util.*; +// Features: +// - automatically flush writer before reading on linked read/write pairs +// - no checked exceptions thrown +// - unified write(char), print(char), and write(byte) +// - unreading/peeking +// - transcribe +// - append (daisy-chaining) +// - automatically closes input streams when end reached + /** plays the role of InputStream, OutputStream, Reader and Writer, with logging and unchecked exceptions */ public class Stream { @@ -18,46 +27,6 @@ public class Stream { private String newLine = "\r\n"; private Stream in_next = null; - public Stream append(String in_next) { return appendStream(new Stream(in_next)); } - public Stream appendStream(Stream in_next) { - if (this.in_next != null) - this.in_next.appendStream(in_next); - else - this.in_next = in_next; - return this; - } - - 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) { in.close(); break; } - out.out.write(buf, 0, numread); - } - if (close) out.close(); - } catch (IOException ioe) { ioe(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) { in.close(); return; } - out.append(buf, 0, numread); - } - //} catch (IOException ioe) { ioe(ioe); } - } - - public static int countLines(Stream s) { - int ret = 0; - while(s.readln() != null) ret++; - s.close(); - 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); } @@ -72,77 +41,35 @@ public class Stream { try { this.out = new Stream.Out(s.getOutputStream()); } catch (IOException e) { ioe(e); throw new Error(); } } - static int ioe(IOException e) { - if (e instanceof SocketException && e.toString().indexOf("Connection reset")!=-1) - throw new Closed(e.getMessage()); - throw new StreamException(e); - } - public static class StreamException extends RuntimeException { - public StreamException(Exception e) { super(e); } - public StreamException(String s) { super(s); } - } - public static class EOF extends StreamException { public EOF() { super("End of stream"); } } - public static class Closed extends StreamException { public Closed(String s) { super(s); } } - - - public char peekc() { - flush(); - return in.getc(true); - } - public char getc() { - flush(); - char ret = in.getc(false); - return ret; - } - public String readln() { - flush(); - String s = in.readln(); - return s; - } - - public void print(String s) { - out.write(s); - flush(); - } - public void println(String s) { - out.write(s); - out.write(newLine); - flush(); - } - public void flush() { - if (out != null) try { out.w.flush(); } catch(IOException e) { ioe(e); } - } - public int read(byte[] b, int off, int len) { - flush(); - int ret = in.readBytes(b, off, len); - nnl = false; - return ret; - } - public int read(char[] c, int off, int len) { - flush(); - int ret = in.read(c, off, len); - return ret; - } + // Main API ////////////////////////////////////////////////////////////////////////////// + public char peekc() { flush(); return in.getc(true); } + public char getc() { flush(); return in.getc(false); } + public String readln() { flush(); return in.readln(); } + public void print(String s) { out.write(s); flush(); } + public void println(String s) { print(s); print(newLine); } + public void flush() { if (out != null) out.flushWriter(); } + public int read(byte[] b, int off, int len) { flush(); return in.readBytes(b, off, len); } + public int read(char[] c, int off, int len) { flush(); return in.read(c, off, len); } public void unread(String s) { in.unread(s); } - - /** should not throw exceptions */ 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 */ - private boolean nnl = false; - private static class Out extends BufferedOutputStream { - private Writer w = new BufferedWriter(new OutputStreamWriter(this)); + private Writer writer = new OutputStreamWriter(this); public Out(OutputStream out) { super(out); } - public void close() { try { super.close(); } catch (Exception e) { Log.error(this, e); } } - public void write(String s) { try { w.write(s); } catch (IOException e) { ioe(e); } } + public void close() { try { super.close(); } catch (Exception e) { Log.error(this, e); } } + public void write(String s) { try { writer.write(s); } catch (IOException e) { ioe(e); } } + private void flushWriter() { try { writer.flush(); } catch (IOException e) { ioe(e); } } + public void flush() { + writer.flush(); + super.flush(); + } } private class In extends InputStream { - public final Reader reader = new InputStreamReader(this); + public final Reader reader = new InputStreamReader(this); + private final Writer unreader = new OutputStreamWriter(new UnReaderStream()); private /*final*/ InputStream orig; public In(InputStream in) { orig = in; } @@ -158,8 +85,8 @@ public class Stream { public int available() { return flushing ? 0 : (end - start); } public void close() { try { - if (orig!=null) orig.close(); - if (in_next != null) in_next.close(); // FIXME: correct? + if (orig!=null) orig.close(); + if (in_next != null) in_next.close(); } catch (Exception e) { Log.error(this, e); } } public char getc(boolean peek) { try { @@ -167,7 +94,6 @@ public class Stream { cstart = 0; cend = reader.read(cbuf, 0, cbuf.length); if (cend == -1) { - reader.close(); cend = cstart; if (in_next == null) throw new EOF(); return getc(peek); @@ -266,8 +192,7 @@ public class Stream { } finally { flushing = false; } } - Writer unreader = new OutputStreamWriter(new InOutputStream()); - private class InOutputStream extends OutputStream { + private class UnReaderStream 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); } @@ -279,4 +204,57 @@ public class Stream { } } + // Utilities: append() and transcribe() ////////////////////////////////////////////////////////////////////////////// + + public Stream append(String in_next) { return appendStream(new Stream(in_next)); } + public Stream appendStream(Stream in_next) { + if (this.in_next != null) + this.in_next.appendStream(in_next); + else + this.in_next = in_next; + return this; + } + + 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) { in.close(); break; } + out.out.write(buf, 0, numread); + } + if (close) out.close(); + } catch (IOException ioe) { ioe(ioe); } + } + + public void transcribe(StringBuffer out) { + char[] buf = new char[1024]; + while(true) { + int numread = in.read(buf, 0, buf.length); + if (numread==-1) { in.close(); return; } + out.append(buf, 0, numread); + } + } + + public static int countLines(Stream s) { + int ret = 0; + while(s.readln() != null) ret++; + s.close(); + return ret; + } + + // Exceptions ////////////////////////////////////////////////////////////////////////////// + + static int ioe(IOException e) { + if (e instanceof SocketException && e.toString().indexOf("Connection reset")!=-1) + throw new Closed(e.getMessage()); + throw new StreamException(e); + } + public static class StreamException extends RuntimeException { + public StreamException(Exception e) { super(e); } + public StreamException(String s) { super(s); } + } + public static class EOF extends StreamException { public EOF() { super("End of stream"); } } + public static class Closed extends StreamException { public Closed(String s) { super(s); } } }