NextableInputStream: separate base behaviour from nextable
[nikiroo-utils.git] / src / be / nikiroo / utils / NextableInputStream.java
index 0def936c4247ea5019898d0862481713e8620c2b..37322c9e8e0d7c5f7b651f44a817aecbe6469e5b 100644 (file)
@@ -12,16 +12,11 @@ import java.io.InputStream;
  * 
  * @author niki
  */
-public class NextableInputStream extends InputStream {
+public class NextableInputStream extends BufferedInputStream {
        private NextableInputStreamStep step;
+       private boolean started;
        private boolean stopped;
 
-       private InputStream in;
-       private boolean eof;
-       private int pos = 0;
-       private int len = 0;
-       private byte[] buffer = new byte[4096];
-
        /**
         * Create a new {@link NextableInputStream} that wraps the given
         * {@link InputStream}.
@@ -33,10 +28,51 @@ public class NextableInputStream extends InputStream {
         *            case it will behave as a normal {@link InputStream})
         */
        public NextableInputStream(InputStream in, NextableInputStreamStep step) {
-               this.in = in;
+               super(in);
                this.step = step;
        }
 
+       /**
+        * 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) {
+               super(in, offset, length);
+               this.step = step;
+               checkBuffer(true);
+       }
+
        /**
         * Unblock the processing of the next sub-stream.
         * <p>
@@ -53,68 +89,26 @@ public class NextableInputStream extends InputStream {
         * @return TRUE if we unblocked the next sub-stream, FALSE if not
         * 
         * @throws IOException
-        *             in case of I/O error
+        *             in case of I/O error or if the stream is closed
         */
        public boolean next() throws IOException {
-               if (!hasMoreData() && stopped) {
-                       len = step.getResumeLen();
-                       pos += step.getResumeSkip();
-                       eof = false;
-
-                       if (!preRead()) {
-                               checkBuffer(false);
-                       }
-
-                       // consider that if EOF, there is no next
-                       return hasMoreData();
-               }
-
-               return false;
-       }
-
-       @Override
-       public int read() throws IOException {
-               preRead();
-               if (eof) {
-                       return -1;
-               }
-
-               return buffer[pos++];
-       }
-
-       @Override
-       public int read(byte[] b) throws IOException {
-               return read(b, 0, b.length);
-       }
-
-       @Override
-       public int read(byte[] b, int boff, int blen) throws IOException {
-               if (b == null) {
-                       throw new NullPointerException();
-               } else if (boff < 0 || blen < 0 || blen > b.length - boff) {
-                       throw new IndexOutOfBoundsException();
-               } else if (blen == 0) {
-                       return 0;
-               }
-
-               int done = 0;
-               while (hasMoreData() && done < blen) {
-                       preRead();
-                       if (hasMoreData()) {
-                               for (int i = pos; i < blen && i < len; i++) {
-                                       b[boff + done] = buffer[i];
-                                       pos++;
-                                       done++;
-                               }
-                       }
-               }
-
-               return done > 0 ? done : -1;
+               return next(false);
        }
 
-       @Override
-       public int available() throws IOException {
-               return Math.max(0, len - pos);
+       /**
+        * Unblock the next sub-stream as would have done
+        * {@link NextableInputStream#next()}, but disable the sub-stream systems.
+        * <p>
+        * That is, the next stream, if any, will be the last one and will not be
+        * subject to the {@link NextableInputStreamStep}.
+        * 
+        * @return TRUE if we unblocked the next sub-stream, FALSE if not
+        * 
+        * @throws IOException
+        *             in case of I/O error or if the stream is closed
+        */
+       public boolean nextAll() throws IOException {
+               return next(true);
        }
 
        /**
@@ -126,20 +120,19 @@ public class NextableInputStream extends InputStream {
         * @throws IOException
         *             in case of I/O error
         */
-       private boolean preRead() throws IOException {
-               boolean hasRead = false;
-               if (!eof && in != null && pos >= len && !stopped) {
-                       pos = 0;
-                       len = in.read(buffer);
+       @Override
+       protected boolean preRead() throws IOException {
+               if (!stopped) {
+                       boolean bufferChanged = super.preRead();
                        checkBuffer(true);
-                       hasRead = true;
+                       return bufferChanged;
                }
 
                if (pos >= len) {
                        eof = true;
                }
 
-               return hasRead;
+               return false;
        }
 
        /**
@@ -147,8 +140,9 @@ public class NextableInputStream extends InputStream {
         * 
         * @return TRUE if it is the case, FALSE if not
         */
-       private boolean hasMoreData() {
-               return !(eof && pos >= len);
+       @Override
+       protected boolean hasMoreData() {
+               return started && super.hasMoreData();
        }
 
        /**
@@ -164,7 +158,7 @@ public class NextableInputStream extends InputStream {
         *            the {@link NextableInputStreamStep}
         */
        private void checkBuffer(boolean newBuffer) {
-               if (step != null) {
+               if (step != null && len > 0) {
                        if (newBuffer) {
                                step.clearBuffer();
                        }
@@ -177,4 +171,51 @@ public class NextableInputStream extends InputStream {
                        }
                }
        }
+
+       /**
+        * The implementation of {@link NextableInputStream#next()} and
+        * {@link NextableInputStream#nextAll()}.
+        * 
+        * @param all
+        *            TRUE for {@link NextableInputStream#nextAll()}, FALSE for
+        *            {@link NextableInputStream#next()}
+        * 
+        * @return TRUE if we unblocked the next sub-stream, FALSE if not
+        * 
+        * @throws IOException
+        *             in case of I/O error or if the stream is closed
+        */
+       private boolean next(boolean all) throws IOException {
+               checkClose();
+
+               if (!started) {
+                       // First call before being allowed to read
+                       started = true;
+
+                       if (all) {
+                               step = null;
+                       }
+
+                       return true;
+               }
+
+               if (step != null && !hasMoreData() && stopped) {
+                       len = step.getResumeLen();
+                       pos += step.getResumeSkip();
+                       eof = false;
+
+                       if (all) {
+                               step = null;
+                       }
+
+                       if (!preRead()) {
+                               checkBuffer(false);
+                       }
+
+                       // consider that if EOF, there is no next
+                       return hasMoreData();
+               }
+
+               return false;
+       }
 }