- public final Reader reader = new InputStreamReader(this);
- 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(); // 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();
- 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<cend; i++)
- if (cbuf[i] == '\n') {
- // this should (in theory) handle CR, LF,
- // CRLF, and LFCR properly, assuming that the
- // file consistently uses the same ending
- // throughout.
- int begin = cstart;
- int len = i-cstart;
- cstart = i+1;
- if (cbuf[begin] == '\r') { begin++; len--; }
- while (len > 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() {
+ cbr.unbuffer(unreader);
+ 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); }
+ }