X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2FNextableInputStreamStep.java;h=a2ee039717fa033bfd6c938bcafc211587903313;hb=63b46ca9f1703134ef2979b72d474e9c9b8f5737;hp=ab2256266bbfbdd0d76a8359ed0c3de22aed8ed2;hpb=4098af704dfa22ce4a60003940753c28030374fa;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/NextableInputStreamStep.java b/src/be/nikiroo/utils/NextableInputStreamStep.java index ab22562..a2ee039 100755 --- a/src/be/nikiroo/utils/NextableInputStreamStep.java +++ b/src/be/nikiroo/utils/NextableInputStreamStep.java @@ -1,26 +1,58 @@ package be.nikiroo.utils; +import java.io.InputStream; + +/** + * Divide an {@link InputStream} into sub-streams. + * + * @author niki + */ public class NextableInputStreamStep { private int stopAt; - private boolean disabled; - private int pos; private int resumeLen; private int last = -1; private int skip; + /** + * Create a new divider that will separate the sub-streams each time it sees + * this byte. + *

+ * Note that the byte will be bypassed by the {@link InputStream} as far as + * the consumers will be aware. + * + * @param byt + * the byte at which to separate two sub-streams + */ public NextableInputStreamStep(int byt) { stopAt = byt; } - // do NOT stop twice on the same item + /** + * Check if we need to stop the {@link InputStream} reading at some point in + * the current buffer. + *

+ * If we do, return the index at which to stop; if not, return -1. + *

+ * This method will not return the same index a second time (unless + * we cleared the buffer). + * + * @param buffer + * the buffer to check + * @param pos + * the current position of what was read in the buffer + * @param len + * the maximum index to use in the buffer (anything above that is + * not to be used) + * + * @return the index at which to stop, or -1 + */ 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; @@ -31,22 +63,34 @@ public class NextableInputStreamStep { return -1; } + /** + * Get the maximum index to use in the buffer used in + * {@link NextableInputStreamStep#stop(byte[], int, int)} at resume time. + * + * @return the index + */ public int getResumeLen() { return resumeLen; } - - public int getSkip() { + + /** + * Get the number of bytes to skip at resume time. + * + * @return the number of bytes to skip + */ + public int getResumeSkip() { return skip; } + /** + * Clear the information we may have kept about the current buffer + *

+ * You should call this method each time you change the content of the + * buffer used in {@link NextableInputStreamStep#stop(byte[], int, int)}. + */ public void clearBuffer() { this.last = -1; - this.pos = 0; this.skip = 0; this.resumeLen = 0; } - - public boolean isEnabled() { - return !disabled; - } }