NextableInputStream, step 2
authorNiki <david.roulet@uclouvain.be>
Wed, 24 Apr 2019 14:29:50 +0000 (16:29 +0200)
committerNiki <david.roulet@uclouvain.be>
Wed, 24 Apr 2019 14:29:50 +0000 (16:29 +0200)
src/be/nikiroo/utils/NextableInputStream.java
src/be/nikiroo/utils/NextableInputStreamStep.java [new file with mode: 0755]
src/be/nikiroo/utils/test_code/NextableInputStreamTest.java
src/be/nikiroo/utils/test_code/Test.java

index 0bcbe14ec89a96a26cf56933ef94aef3214cd1a4..6f3afc2b261fe439f65c79dbce20602be3bfdd53 100644 (file)
@@ -2,8 +2,13 @@ package be.nikiroo.utils;
 
 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;
@@ -14,6 +19,25 @@ public class NextableInputStream extends InputStream {
                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();
@@ -40,27 +64,56 @@ public class NextableInputStream extends InputStream {
                }
 
                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;
+                               }
+                       }
+               }
+       }
 }
diff --git a/src/be/nikiroo/utils/NextableInputStreamStep.java b/src/be/nikiroo/utils/NextableInputStreamStep.java
new file mode 100755 (executable)
index 0000000..ab22562
--- /dev/null
@@ -0,0 +1,52 @@
+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;
+       }
+}
index 1b0064df249726c006d7f7d5c06ba793374d2a0f..51ff128e5dc97e2ff104a6fb40da84c4eb2a10d9 100644 (file)
@@ -5,6 +5,7 @@ import java.io.InputStream;
 
 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;
 
@@ -29,5 +30,66 @@ public class NextableInputStreamTest extends 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]);
+                               }
+                       }
+               });
        }
 }
index f25c02db8573a723761819a1b5f403a1344f2c31..01766ac031535b33f04886ba6405dd2e6e4688ec 100644 (file)
@@ -33,6 +33,7 @@ public class Test extends TestLauncher {
                addSeries(new StringUtilsTest(args));
                addSeries(new TempFilesTest(args));
                addSeries(new CryptUtilsTest(args));
+               addSeries(new NextableInputStreamTest(args));
 
                // TODO: test cache and downloader
                Cache cache = null;