Nextable.next: blocking call
[nikiroo-utils.git] / src / be / nikiroo / utils / streams / NextableInputStream.java
index 4a6e0abc672abf441ef6acf51f9db9ed1d625585..b8c3fe8c5baf45b0c8b3adffa5cda11c53b99c79 100644 (file)
@@ -85,6 +85,8 @@ public class NextableInputStream extends BufferedInputStream {
         * {@link IOException}s can happen when we have no data available in the
         * buffer; in that case, we fetch more data to know if we can have a next
         * sub-stream or not.
+        * <p>
+        * This is can be a blocking call when data need to be fetched.
         * 
         * @return TRUE if we unblocked the next sub-stream, FALSE if not
         * 
@@ -101,6 +103,8 @@ public class NextableInputStream extends BufferedInputStream {
         * <p>
         * That is, the next stream, if any, will be the last one and will not be
         * subject to the {@link NextableInputStreamStep}.
+        * <p>
+        * This is can be a blocking call when data need to be fetched.
         * 
         * @return TRUE if we unblocked the next sub-stream, FALSE if not
         * 
@@ -115,16 +119,19 @@ public class NextableInputStream extends BufferedInputStream {
         * Check if this stream is totally spent (no more data to read or to
         * process).
         * <p>
-        * Note: an empty stream that is still not started will return FALSE, as we
-        * don't know yet if it is empty.
+        * Note: when the stream is divided into sub-streams, each sub-stream will
+        * report it is eof when emptied.
         * 
         * @return TRUE if it is
+        * 
+        * @throws IOException
+        *             in case of I/O error
         */
        @Override
-       public boolean eof() {
+       public boolean eof() throws IOException {
                return super.eof();
        }
-       
+
        /**
         * Check if we still have some data in the buffer and, if not, fetch some.
         * 
@@ -138,7 +145,7 @@ public class NextableInputStream extends BufferedInputStream {
        protected boolean preRead() throws IOException {
                if (!stopped) {
                        boolean bufferChanged = super.preRead();
-                       checkBuffer(true);
+                       checkBuffer(bufferChanged);
                        return bufferChanged;
                }
 
@@ -172,12 +179,12 @@ public class NextableInputStream extends BufferedInputStream {
         *            the {@link NextableInputStreamStep}
         */
        private void checkBuffer(boolean newBuffer) {
-               if (step != null && stop > 0) {
+               if (step != null && stop >= 0) {
                        if (newBuffer) {
                                step.clearBuffer();
                        }
 
-                       int stopAt = step.stop(buffer, start, stop);
+                       int stopAt = step.stop(buffer, start, stop, eof);
                        if (stopAt >= 0) {
                                stop = stopAt;
                                eof = true;
@@ -189,6 +196,8 @@ public class NextableInputStream extends BufferedInputStream {
        /**
         * The implementation of {@link NextableInputStream#next()} and
         * {@link NextableInputStream#nextAll()}.
+        * <p>
+        * This is can be a blocking call when data need to be fetched.
         * 
         * @param all
         *            TRUE for {@link NextableInputStream#nextAll()}, FALSE for
@@ -209,27 +218,38 @@ public class NextableInputStream extends BufferedInputStream {
                        if (all) {
                                step = null;
                        }
-
-                       return true;
                }
 
                if (step != null && !hasMoreData() && stopped) {
                        stop = step.getResumeLen();
                        start += step.getResumeSkip();
-                       eof = false;
+                       eof = step.getResumeEof();
+                       stopped = false;
 
                        if (all) {
                                step = null;
                        }
 
-                       if (!preRead()) {
-                               checkBuffer(false);
-                       }
+                       checkBuffer(false);
+               }
+
+               // consider that if EOF, there is no next
+               if (start >= stop) {
+                       // Make sure, block if necessary
+                       preRead();
 
-                       // consider that if EOF, there is no next
                        return hasMoreData();
                }
 
-               return false;
+               return true;
+       }
+
+       public String DEBUG() {
+               String rep = String.format(
+                               "Nextable %s: %d -> %d [eof: %s] [more data: %s]",
+                               (stopped ? "stopped" : "running"), start, stop, "" + eof, ""
+                                               + hasMoreData());
+
+               return rep;
        }
 }