- 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 (start<space) unshiftb(); }
- private void unshiftb() {
- System.arraycopy(buf,start,buf,buf.length-(end-start),end-start);start=buf.length-(end-start);end=buf.length; }
-
- public void unread(String s) { ensurec(s.length()); s.getChars(0, s.length(), cbuf, cend); cend += s.length(); }
-
- private void flushchars() {
- try {
- flushing = true;
- 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);
- } finally { flushing = false; }