From fb36cd8efcf7e222502a59f33423b1efefd370e2 Mon Sep 17 00:00:00 2001 From: adam Date: Thu, 5 Jul 2007 01:44:17 +0000 Subject: [PATCH] quick fix; may be buggy darcs-hash:20070705014417-5007d-9787bcad2f0fb9eb9de65f3b268ce37582aed482.gz --- src/org/ibex/io/Stream.java | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/org/ibex/io/Stream.java b/src/org/ibex/io/Stream.java index f18d2ba..d4e0c06 100644 --- a/src/org/ibex/io/Stream.java +++ b/src/org/ibex/io/Stream.java @@ -17,12 +17,14 @@ 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; + 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) return this.in_next.appendStream(in_next); - this.in_next = in_next; + if (this.in_next != null) + this.in_next.appendStream(in_next); + else + this.in_next = in_next; return this; } @@ -35,7 +37,7 @@ public class Stream { byte[] buf = new byte[1024]; while(true) { int numread = in.read(buf, 0, buf.length); - if (numread==-1) break; + if (numread==-1) { in.close(); break; } out.out.write(buf, 0, numread); } if (close) out.close(); @@ -47,7 +49,7 @@ public class Stream { char[] buf = new char[1024]; while(true) { int numread = in.read(buf, 0, buf.length); - if (numread==-1) return; + if (numread==-1) { in.close(); return; } out.append(buf, 0, numread); } //} catch (IOException ioe) { ioe(ioe); } @@ -56,10 +58,11 @@ public class Stream { 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(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); } @@ -205,18 +208,19 @@ public class Stream { boolean flushing = false; public int available() { return flushing ? 0 : (end - start); } - public void close() { try { if (orig!=null) orig.close(); } catch (Exception e) { Log.error(this, e); } } + public void close() { try { + if (orig!=null) orig.close(); + if (in_next != null) in_next.close(); // FIXME: correct? + } 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) { + reader.close(); 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); } } @@ -241,6 +245,7 @@ public class Stream { ensurec(256); int numread = reader.read(cbuf, cend, cbuf.length - cend); if (numread == -1) { + reader.close(); if (cstart == cend) return null; String ret = new String(cbuf, cstart, cend-cstart); cstart = cend = 0; @@ -254,7 +259,7 @@ public class Stream { if (cstart == cend) { cstart = 0; cend = reader.read(cbuf, 0, cbuf.length); - if (cend == -1) { cend = cstart; return -1; } + if (cend == -1) { reader.close(); cend = cstart; return -1; } } if (len > cend - cstart) len = cend - cstart; System.arraycopy(cbuf, cstart, c, pos, len); @@ -268,7 +273,12 @@ public class Stream { if (start == end) { start = 0; end = orig.read(buf, 0, buf.length); - if (end == -1) { end = start; return -1; } + if (end == -1) { + orig.close(); + end = start; + if (in_next==null) return -1; + return in_next.read(b, pos, len); + } } if (len > end - start) len = end - start; System.arraycopy(buf, start, b, pos, len); @@ -298,8 +308,9 @@ public class Stream { private void flushchars() { try { flushing = true; - for(; reader.ready(); reader.read(cbuf, cend++, 1)) ensurec(1); - unreader.write(cbuf, cstart, cend); + for(; reader.ready(); reader.read(cbuf, cend++, 1)) ensurec(10); + if (cend>cstart) + unreader.write(cbuf, cstart, cend-cstart); cstart = cend = 0; unreader.flush(); } catch (IOException e) { ioe(e); -- 1.7.10.4