NextableInputStream, step 2
[nikiroo-utils.git] / src / be / nikiroo / utils / NextableInputStreamStep.java
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;
+       }
+}