private byte[] buf = new byte[8192];
private int start = 0;
private int end = 0;
+ public int limit = -1;
public Digest digest = null;
public ByteBufInputStream(InputStream is) {
if (this.next == null) this.next = next;
else this.next.appendStream(next);
}
+ public void setLimit(int limit) { this.limit = limit; }
private int bufSize() { if (end==start) { end = start = 0; } return end-start; }
private int fillBufIfEmpty() {
try {
if (prereading) return -1;
start = 0;
do {
- end = is.read(buf, 0, buf.length);
+ if (limit == 0) end = -1;
+ else if (limit == -1) end = is.read(buf, 0, buf.length);
+ else end = is.read(buf, 0, Math.min(buf.length, limit));
if (end == -1) {
is.close();
is = null;
continue;
}
} else {
+ if (limit >= 0) limit = Math.max(0, limit-end);
if (digest != null) digest.update(buf, 0, end);
}
} while(end==0);
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 skip(int len) {
+ // FIXME ugly
+ return read(null, 0, len);
+ }
public int read(byte[] c, int pos, int len) {
if (prereading) return -1;
prereading = true;
} finally { prereading = false; }
if (fillBufIfEmpty() == -1) return -1;
if (len > end - start) len = end - start;
- System.arraycopy(buf, start, c, pos, len);
+ if (c!=null) System.arraycopy(buf, start, c, pos, len);
start += len;
return len;
}