1 // Copyright 2000-2007 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
8 /** package-private class */
9 abstract class ByteBufInputStream extends InputStream {
11 private InputStream is;
12 public Stream next = null;
13 private byte[] buf = new byte[8192];
14 private int start = 0;
17 public ByteBufInputStream(InputStream is) {
21 public void appendStream(Stream next) {
22 if (this.next == null) this.next = next;
23 else this.next.appendStream(next);
25 private int bufSize() { if (end==start) { end = start = 0; } return end-start; }
26 private int fillBufIfEmpty() {
28 if (bufSize() > 0) return bufSize();
29 if (is == null) return -1;
30 if (prereading) return -1;
33 end = is.read(buf, 0, buf.length);
38 // FIXME: append to a stream that has already run out?
39 is = next.getInputStream();
46 if (end == -1) { end = 0; return -1; }
48 } catch (IOException e) { Stream.ioe(e); return -1; }
51 public int available() { return end-start; }
54 if (is != null) is.close();
55 if (next != null) next.close();
56 } catch (IOException e) { Stream.ioe(e); }
59 private boolean prereading = false;
60 public abstract void preread();
61 public int read() { byte[] b = new byte[0]; if (read(b, 0, 1)<=0) return -1; return b[0] & 0xff; }
62 public int read(byte[] c, int pos, int len) {
63 if (prereading) return -1;
67 } finally { prereading = false; }
68 if (fillBufIfEmpty() == -1) return -1;
69 if (len > end - start) len = end - start;
70 System.arraycopy(buf, start, c, pos, len);
75 public void pushback(byte[] b, int off, int len) {
78 /* FIXME, this allocates too often */
79 byte[] newbuf = new byte[len+(end-start)];
80 System.arraycopy(buf, start, newbuf, len, (end-start));
82 end = len + (end-start);
85 System.arraycopy(b, off, buf, start-len, len);