From: adam Date: Thu, 5 Jul 2007 04:23:42 +0000 (+0000) Subject: massive rewrite of io library X-Git-Url: http://git.megacz.com/?p=org.ibex.io.git;a=commitdiff_plain;h=3e1c83f8d1d65b96a3e24c3aa2a042c21f005111 massive rewrite of io library darcs-hash:20070705042342-5007d-b1d4446207cc88f406dbb360a11c31cd2e7fcdb4.gz --- diff --git a/src/org/ibex/io/ByteBufInputStream.java b/src/org/ibex/io/ByteBufInputStream.java new file mode 100644 index 0000000..7581c88 --- /dev/null +++ b/src/org/ibex/io/ByteBufInputStream.java @@ -0,0 +1,76 @@ +// 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. + +package org.ibex.io; +import java.io.*; + +/** package-private class */ +abstract class ByteBufInputStream extends InputStream { + + private InputStream is; + public InputStream next; + private byte[] buf = new byte[8192]; + private int start = 0; + private int end = 0; + + public ByteBufInputStream(InputStream is) { + this.is = is; + } + + private int bufSize() { if (end==start) { end = start = 0; } return end-start; } + private int fillBufIfEmpty() { + try { + if (bufSize() > 0) return bufSize(); + start = 0; + do { + end = is.read(buf, 0, buf.length); + if (end == -1 && next != null) { + is.close(); + is = next; + continue; + } + } while(end==0); + if (end == -1) { end = 0; return -1; } + return end; + } catch (IOException e) { Stream.ioe(e); return -1; } + } + + public int available() { return end-start; } + public void close() { + try { + if (is != null) is.close(); + if (next != null) next.close(); + } catch (IOException e) { Stream.ioe(e); } + } + + private boolean prereading = false; + public abstract void preread(); + public int read() { byte[] b = new byte[0]; if (read(b, 0, 1)<=0) return -1; return b[0] & 0xff; } + public int read(byte[] c, int pos, int len) { + if (prereading) return -1; + prereading = true; + try { + preread(); + } finally { prereading = false; } + if (fillBufIfEmpty() == -1) return -1; + if (len > end - start) len = end - start; + System.arraycopy(buf, start, c, pos, len); + start += len; + return len; + } + + public void pushback(byte[] b, int off, int len) { + if (len <= 0) return; + if (start-len < 0) { + /* FIXME, this allocates too often */ + byte[] newbuf = new byte[len+(end-start)]; + System.arraycopy(buf, start, newbuf, len, (end-start)); + buf = newbuf; + end = len + (end-start); + start = len; + } + System.arraycopy(b, off, buf, start-len, len); + start -= len; + } +} diff --git a/src/org/ibex/io/CharBufReader.java b/src/org/ibex/io/CharBufReader.java new file mode 100644 index 0000000..225fd86 --- /dev/null +++ b/src/org/ibex/io/CharBufReader.java @@ -0,0 +1,90 @@ +// 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. + +package org.ibex.io; +import java.io.*; + +/** package-private class */ +class CharBufReader extends Reader { + + private Reader reader; + private char[] buf = new char[8192]; + private int start = 0; + private int end = 0; + + public CharBufReader(Reader r) { this.reader = r; } + + public boolean ready() throws IOException { return bufSize()>0; } + private int bufSize() { if (end==start) { end=start=0; } return end-start; } + private int fillBufIfEmpty() { + try { + if (bufSize() > 0) return bufSize(); + start = 0; + do { + end = reader.read(buf, 0, buf.length); + } while(end==0); + if (end == -1) { end = 0; return -1; } + return end; + } catch (IOException e) { Stream.ioe(e); return -1; } + } + + public int available() { return end-start; } + public void close() { + try { + if (reader != null) reader.close(); + } catch (IOException e) { Stream.ioe(e); } + } + + public int read(char[] c, int pos, int len) { + if (fillBufIfEmpty() == -1) return -1; + if (len > end - start) len = end - start; + System.arraycopy(buf, start, c, pos, len); + start += len; + return len; + } + + public char getc(boolean peek) { + if (fillBufIfEmpty() <= 0) throw new Stream.EOF(); + return peek ? buf[start] : buf[start++]; + } + + private StringBuffer lineReaderStringBuffer = new StringBuffer(); + public String readln() { + lineReaderStringBuffer.setLength(0); + boolean readsome = false; + OUT: while(true) { + if (fillBufIfEmpty() <= 0) break; + readsome = true; + for(int i=start; i 0 && buf[begin+len-1] == '\r') { len--; } + lineReaderStringBuffer.append(buf, begin, len); + break OUT; + } + lineReaderStringBuffer.append(buf, start, end-start); + start = end = 0; + } + if (!readsome) return null; + String ret = lineReaderStringBuffer.toString(); + lineReaderStringBuffer.setLength(0); + return ret; + } + + public void unbuffer(Writer writer) { + if (bufSize()<=0) return; + try { + writer.write(buf, start, end-start); + } catch (IOException e) { + Stream.ioe(e); + } + } +} diff --git a/src/org/ibex/io/Stream.java b/src/org/ibex/io/Stream.java index 80e252b..ea43ba7 100644 --- a/src/org/ibex/io/Stream.java +++ b/src/org/ibex/io/Stream.java @@ -50,8 +50,7 @@ public class Stream { 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); } + public int read(char[] c, int off, int len) { flush(); return in.readChars(c, off, len); } public void close() { try { if (in!=null) in.close(); } finally { if (out!=null) out.close(); } } public void setNewline(String s) { newLine = s; } @@ -61,147 +60,55 @@ public class Stream { 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); } } + private boolean flushing = false; public void flush() { - writer.flush(); - super.flush(); + if (flushing) return; + try { + flushing = true; + try { + writer.flush(); + } finally { flushing = false; } + super.flush(); + } catch (IOException e) { ioe(e); } } } private class In extends InputStream { - 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; } - - char[] cbuf = new char[8192]; - int cstart = 0; - int cend = 0; - - byte[] buf = new byte[8192]; - int start = 0; - int end = 0; - - boolean flushing = false; - - public int available() { return flushing ? 0 : (end - start); } - public void close() { try { - 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 { - if (cstart == cend) { - cstart = 0; - cend = reader.read(cbuf, 0, cbuf.length); - if (cend == -1) { - cend = cstart; - if (in_next == null) throw new EOF(); - return getc(peek); - } - } - return peek ? cbuf[cstart] : cbuf[cstart++]; - } catch (IOException e) { return (char)ioe(e); } } - - public String readln() { try { - while(true) { - for(int i=cstart; i 0 && cbuf[begin+len-1] == '\r') { len--; } - return new String(cbuf, begin, len); + private ByteBufInputStream bbis; + private CharBufReader cbr; + public Reader reader; + private Writer unreader; + + public char getc(boolean peek) { return cbr.getc(peek); } + public String readln() { return cbr.readln(); } + public int read() { return bbis.read(); } + public int read(byte[] b) { try { return bbis.read(b); } catch (IOException e) { ioe(e); return 0; } } + public int read(byte[] b, int off, int len) { return bbis.read(b, off, len); } + public void close() { try { cbr.close(); } catch (Exception e) { Log.error(this, e); } } + public int readBytes(byte[] b, int off, int len) { return bbis.read(b, off, len); } + public int readChars(char[] c, int off, int len) { return cbr.read(c, off, len); } + + public In(InputStream in) { + bbis = new ByteBufInputStream(in) { + public void preread() { + try { + if (!cbr.ready()) return; + } catch (IOException e) { ioe(e); } + char[] c = new char[20]; + while(true) { + try { + if (!cbr.ready()) break; + int numread = cbr.read(c, 0, c.length); + if (numread == -1) break; + unreader.write(c, 0, numread); + } catch (IOException e) { ioe(e); } + } } - 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; - return ret; - } - cend += numread; - } - } catch (IOException e) { ioe(e); return null; } } - - public int read(char[] c, int pos, int len) { try { - if (cstart == cend) { - cstart = 0; - cend = reader.read(cbuf, 0, cbuf.length); - if (cend == -1) { reader.close(); cend = cstart; return -1; } - } - if (len > cend - cstart) len = cend - cstart; - System.arraycopy(cbuf, cstart, c, pos, len); - cstart += len; - return len; - } catch (IOException e) { ioe(e); return -1; } } - - public int readBytes(byte[] b, int pos, int len) { flushchars(); return read(b, pos, len); } - public int read() { byte[] b = new byte[1]; if (read(b, 0, 1) == -1) return -1; return (int)b[0]; } - public int read(byte[] b, int pos, int len) { try { - if (start == end) { - start = 0; - end = orig.read(buf, 0, buf.length); - 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); - start += len; - return len; - } catch (IOException e) { ioe(e); return -1; } } - - private void growc(int s){char[] cbuf2=new char[cbuf.length+s*2];System.arraycopy(cbuf,0,cbuf2,0,cbuf.length);cbuf=cbuf2; } - private void shiftc() { - char[] cbuf2 = new char[cbuf.length]; - System.arraycopy(cbuf, cstart, cbuf2, 0, cend-cstart); - cend -= cstart; - cstart = 0; - cbuf = cbuf2; - } - private void ensurec(int space) { if (cend-cstart+space>cbuf.length) growc(space); if (cend+space>cbuf.length) shiftc(); } - - private void growb(int s) { byte[] buf2 = new byte[buf.length+s*2]; System.arraycopy(buf,0,buf2,0,buf.length); buf=buf2; } - private void shiftb() { System.arraycopy(buf, start, buf, 0, end-start); end -= start; start = 0; } - private void ensureb(int space) { if (end-start+space>buf.length) growb(space); if (end+space>buf.length) shiftb(); } - private void ensureb2(int space) { if (end-start+space>buf.length) growb(space); if (startcstart) - unreader.write(cbuf, cstart, cend-cstart); - cstart = cend = 0; - unreader.flush(); - } catch (IOException e) { ioe(e); - } finally { flushing = false; } + }; + unreader = new OutputStreamWriter(new UnReaderStream(bbis)); + cbr = new CharBufReader(new InputStreamReader(bbis)); } - 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); } - public void write(byte[] b, int p, int l) { - ensureb2(l); - System.arraycopy(b, p, buf, start-l, l); - start -= l; - } - } } // Utilities: append() and transcribe() ////////////////////////////////////////////////////////////////////////////// @@ -231,13 +138,14 @@ public class Stream { public void transcribe(StringBuffer out) { char[] buf = new char[1024]; while(true) { - int numread = in.read(buf, 0, buf.length); + int numread = in.readChars(buf, 0, buf.length); if (numread==-1) { in.close(); return; } out.append(buf, 0, numread); } } public static int countLines(Stream s) { + System.out.println("counting lines..."); int ret = 0; while(s.readln() != null) ret++; s.close(); diff --git a/src/org/ibex/io/UnReaderStream.java b/src/org/ibex/io/UnReaderStream.java new file mode 100644 index 0000000..ae1777e --- /dev/null +++ b/src/org/ibex/io/UnReaderStream.java @@ -0,0 +1,23 @@ +// 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. + +package org.ibex.io; + +import java.io.*; + +/** package-private helper */ +class UnReaderStream extends OutputStream { + + private final ByteBufInputStream bbis; + public UnReaderStream(ByteBufInputStream bbis) { + this.bbis = bbis; + } + + 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) { + bbis.pushback(b, p, l); + } +}