import java.io.IOException;
import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
public class NextableInputStream extends InputStream {
+ private List<NextableInputStreamStep> steps = new ArrayList<NextableInputStreamStep>();
+ private NextableInputStreamStep step = null;
+
private InputStream in;
private boolean eof;
private int pos = 0;
this.in = in;
}
+ public void addStep(NextableInputStreamStep step) {
+ steps.add(step);
+ }
+
+ public boolean next() {
+ if (!hasMoreData() && step != null) {
+ len = step.getResumeLen();
+ pos += step.getSkip();
+ eof = false;
+ step = null;
+
+ checkNexts(false);
+
+ return true;
+ }
+
+ return false;
+ }
+
@Override
public int read() throws IOException {
preRead();
}
int done = 0;
- while (!eof && done < blen) {
+ while (hasMoreData() && done < blen) {
preRead();
- for (int i = pos; i < blen && i < len; i++) {
- b[boff + done] = buffer[i];
- pos++;
- done++;
+ if (hasMoreData()) {
+ for (int i = pos; i < blen && i < len; i++) {
+ b[boff + done] = buffer[i];
+ pos++;
+ done++;
+ }
}
}
return done > 0 ? done : -1;
}
+ @Override
+ public int available() throws IOException {
+ return Math.max(0, len - pos);
+ }
+
private void preRead() throws IOException {
- if (in != null && !eof && pos >= len) {
+ if (!eof && in != null && pos >= len && step == null) {
pos = 0;
len = in.read(buffer);
- // checkNexts();
+ checkNexts(true);
}
if (pos >= len) {
eof = true;
}
}
+
+ private boolean hasMoreData() {
+ return !(eof && pos >= len);
+ }
+
+ private void checkNexts(boolean newBuffer) {
+ if (!eof) {
+ for (NextableInputStreamStep step : steps) {
+ if (newBuffer) {
+ step.clearBuffer();
+ }
+
+ int stopAt = step.stop(buffer, pos, len);
+ if (stopAt >= 0) {
+ this.step = step;
+ len = stopAt;
+ eof = true;
+ break;
+ }
+ }
+ }
+ }
}
--- /dev/null
+package be.nikiroo.utils;
+
+public class NextableInputStreamStep {
+ private int stopAt;
+ private boolean disabled;
+ private int pos;
+ private int resumeLen;
+ private int last = -1;
+ private int skip;
+
+ public NextableInputStreamStep(int byt) {
+ stopAt = byt;
+ }
+
+ // do NOT stop twice on the same item
+ public int stop(byte[] buffer, int pos, int len) {
+ for (int i = pos; i < len; i++) {
+ if (buffer[i] == stopAt) {
+ if (i > this.last) {
+ // we skip the sep
+ this.skip = 1;
+
+ this.pos = pos;
+ this.resumeLen = len;
+ this.last = i;
+ return i;
+ }
+ }
+ }
+
+ return -1;
+ }
+
+ public int getResumeLen() {
+ return resumeLen;
+ }
+
+ public int getSkip() {
+ return skip;
+ }
+
+ public void clearBuffer() {
+ this.last = -1;
+ this.pos = 0;
+ this.skip = 0;
+ this.resumeLen = 0;
+ }
+
+ public boolean isEnabled() {
+ return !disabled;
+ }
+}
import be.nikiroo.utils.IOUtils;
import be.nikiroo.utils.NextableInputStream;
+import be.nikiroo.utils.NextableInputStreamStep;
import be.nikiroo.utils.test.TestCase;
import be.nikiroo.utils.test.TestLauncher;
}
}
});
+
+ addTest(new TestCase("Stop at 12") {
+ @Override
+ public void test() throws Exception {
+ byte[] expected = new byte[] { 42, 12, 0, 127 };
+ InputStream bin = new ByteArrayInputStream(expected);
+ NextableInputStream in = new NextableInputStream(bin);
+ in.addStep(new NextableInputStreamStep(12));
+ byte[] actual = IOUtils.toByteArray(in);
+
+ assertEquals(
+ "The resulting array has not the correct number of items",
+ 1, actual.length);
+ for (int i = 0; i < actual.length; i++) {
+ assertEquals("Item " + i + " (0-based) is not the same",
+ expected[i], actual[i]);
+ }
+ }
+ });
+
+ addTest(new TestCase("Stop at 12, resume, stop again, resume") {
+ @Override
+ public void test() throws Exception {
+ byte[] expected = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
+ NextableInputStream in = new NextableInputStream(
+ new ByteArrayInputStream(expected));
+ in.addStep(new NextableInputStreamStep(12));
+
+ byte[] actual1 = IOUtils.toByteArray(in);
+ byte[] expected1 = new byte[] { 42 };
+ assertEquals(
+ "The FIRST resulting array has not the correct number of items",
+ expected1.length, actual1.length);
+ for (int i = 0; i < actual1.length; i++) {
+ assertEquals("Item " + i + " (0-based) is not the same",
+ expected1[i], actual1[i]);
+ }
+
+ assertEquals("Cannot get SECOND entry", true, in.next());
+ byte[] actual2 = IOUtils.toByteArray(in);
+ byte[] expected2 = new byte[] { 0, 127 };
+ assertEquals(
+ "The SECOND resulting array has not the correct number of items",
+ expected2.length, actual2.length);
+ for (int i = 0; i < actual2.length; i++) {
+ assertEquals("Item " + i + " (0-based) is not the same",
+ expected2[i], actual2[i]);
+ }
+
+ assertEquals("Cannot get next THIRD entry", true, in.next());
+ byte[] actual3 = IOUtils.toByteArray(in);
+ byte[] expected3 = new byte[] { 51, 11 };
+ assertEquals(
+ "The THIRD resulting array has not the correct number of items",
+ expected3.length, actual3.length);
+ for (int i = 0; i < actual3.length; i++) {
+ assertEquals("Item " + i + " (0-based) is not the same",
+ expected3[i], actual3[i]);
+ }
+ }
+ });
}
}