package org.jruby.util;
import java.io.IOException;
import java.io.InputStream;
public class InputStreamMarkCursor {
private InputStream in;
private int i = 0;
private int markSize;
private int actualReadTotal;
private byte[] buf;
private int endPoint = 0;
public InputStreamMarkCursor(InputStream in, int markSize) {
this.in = in;
this.markSize = markSize;
buf = new byte[0];
}
public int read() throws IOException {
if (buf.length == 0 || i > buf.length) {
if (buf.length != 0) {
in.reset();
}
buf = new byte[buf.length + markSize];
in.mark(buf.length + markSize);
actualReadTotal = in.read(buf, 0, buf.length);
}
return i >= actualReadTotal ? -1 : buf[i++];
}
public void endPoint(int delta) {
endPoint = i + delta;
}
public void rewind() {
i--;
}
public void finish() throws IOException {
in.reset();
buf = new byte[endPoint];
in.read(buf, 0, endPoint);
}
public void reset() throws IOException {
in.reset();
}
}