+
+ this.buffer = new byte[4096];
+ this.originalBuffer = this.buffer;
+ this.pos = 0;
+ this.len = 0;
+ }
+
+ /**
+ * Create a new {@link NextableInputStream} that wraps the given bytes array
+ * as a data source.
+ *
+ * @param in
+ * the array to wrap, cannot be NULL
+ * @param step
+ * how to separate it into sub-streams (can be NULL, but in that
+ * case it will behave as a normal {@link InputStream})
+ */
+ public NextableInputStream(byte[] in, NextableInputStreamStep step) {
+ this(in, step, 0, in.length);
+ }
+
+ /**
+ * Create a new {@link NextableInputStream} that wraps the given bytes array
+ * as a data source.
+ *
+ * @param in
+ * the array to wrap, cannot be NULL
+ * @param step
+ * how to separate it into sub-streams (can be NULL, but in that
+ * case it will behave as a normal {@link InputStream})
+ * @param offset
+ * the offset to start the reading at
+ * @param length
+ * the number of bytes to take into account in the array,
+ * starting from the offset
+ *
+ * @throws NullPointerException
+ * if the array is NULL
+ * @throws IndexOutOfBoundsException
+ * if the offset and length do not correspond to the given array
+ */
+ public NextableInputStream(byte[] in, NextableInputStreamStep step,
+ int offset, int length) {
+ if (in == null) {
+ throw new NullPointerException();
+ } else if (offset < 0 || length < 0 || length > in.length - offset) {
+ throw new IndexOutOfBoundsException();
+ }
+
+ this.in = null;
+ this.step = step;
+
+ this.buffer = in;
+ this.originalBuffer = this.buffer;
+ this.pos = offset;
+ this.len = length;
+
+ checkBuffer(true);
+ }
+
+ /**
+ * Return this very same {@link NextableInputStream}, but keep a counter of
+ * how many streams were open this way. When calling
+ * {@link NextableInputStream#close()}, decrease this counter if it is not
+ * already zero instead of actually closing the stream.
+ * <p>
+ * You are now responsible for it — you <b>must</b> close it.
+ * <p>
+ * This method allows you to use a wrapping stream around this one and still
+ * close the wrapping stream.
+ *
+ * @return the same stream, but you are now responsible for closing it
+ *
+ * @throws IOException
+ * in case of I/O error or if the stream is closed
+ */
+ public synchronized InputStream open() throws IOException {
+ checkClose();
+ openCounter++;
+ return this;