abstract class ByteBufInputStream extends InputStream {
private InputStream is;
- public InputStream next;
+ public Stream next = null;
private byte[] buf = new byte[8192];
private int start = 0;
private int end = 0;
this.is = is;
}
+ public void appendStream(Stream next) {
+ if (this.next == null) this.next = next;
+ else this.next.appendStream(next);
+ }
private int bufSize() { if (end==start) { end = start = 0; } return end-start; }
private int fillBufIfEmpty() {
try {
if (bufSize() > 0) return bufSize();
+ if (is == null) return -1;
+ if (prereading) return -1;
start = 0;
do {
end = is.read(buf, 0, buf.length);
- if (end == -1 && next != null) {
+ if (end == -1) {
is.close();
- is = next;
- continue;
+ is = null;
+ if (next != null) {
+ // FIXME: append to a stream that has already run out?
+ is = next.getInputStream();
+ next = null;
+ start = end = 0;
+ continue;
+ }
}
} while(end==0);
if (end == -1) { end = 0; return -1; }