NextableInputStream: separate base behaviour from nextable
[nikiroo-utils.git] / src / be / nikiroo / utils / NextableInputStream.java
index 69fd33b34244a7735fb2920b9153b1740573d208..37322c9e8e0d7c5f7b651f44a817aecbe6469e5b 100644 (file)
@@ -12,20 +12,10 @@ 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 boolean closed;
-
-       private InputStream in;
-       private int openCounter;
-       private boolean eof;
-       private int pos;
-       private int len;
-       private byte[] buffer;
-
-       private long bytesRead;
 
        /**
         * Create a new {@link NextableInputStream} that wraps the given
@@ -38,12 +28,8 @@ 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;
-
-               this.buffer = new byte[4096];
-               this.pos = 0;
-               this.len = 0;
        }
 
        /**
@@ -82,44 +68,11 @@ public class NextableInputStream extends InputStream {
         */
        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;
+               super(in, offset, length);
                this.step = step;
-
-               this.buffer = in;
-               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 &mdash; 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;
-       }
-
        /**
         * Unblock the processing of the next sub-stream.
         * <p>
@@ -158,147 +111,6 @@ public class NextableInputStream extends InputStream {
                return next(true);
        }
 
-       public boolean startWith() {
-               // TODO
-               return false;
-       }
-
-       public boolean startWiths() {
-               // TODO
-               return false;
-       }
-
-       /**
-        * The number of bytes read from the under-laying {@link InputStream}.
-        * 
-        * @return the number of bytes
-        */
-       public long getBytesRead() {
-               return bytesRead;
-       }
-
-       /**
-        * Check if this stream is totally spent (no more data to read or to
-        * process).
-        * 
-        * @return TRUE if it is
-        */
-       public boolean eof() {
-               return closed || (len < 0 && !hasMoreData());
-       }
-
-       @Override
-       public int read() throws IOException {
-               checkClose();
-
-               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 {
-               checkClose();
-
-               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;
-       }
-
-       @Override
-       public long skip(long n) throws IOException {
-               // TODO Auto-generated method stub
-               return super.skip(n);
-       }
-
-       @Override
-       public int available() {
-               if (closed) {
-                       return 0;
-               }
-
-               return Math.max(0, len - pos);
-       }
-
-       /**
-        * Closes this stream and releases any system resources associated with the
-        * stream.
-        * <p>
-        * Including the under-laying {@link InputStream}.
-        * <p>
-        * <b>Note:</b> if you called the {@link NextableInputStream#open()} method
-        * prior to this one, it will just decrease the internal count of how many
-        * open streams it held and do nothing else. The stream will actually be
-        * closed when you have called {@link NextableInputStream#close()} once more
-        * than {@link NextableInputStream#open()}.
-        * 
-        * @exception IOException
-        *                in case of I/O error
-        */
-       @Override
-       public synchronized void close() throws IOException {
-               close(true);
-       }
-
-       /**
-        * Closes this stream and releases any system resources associated with the
-        * stream.
-        * <p>
-        * Including the under-laying {@link InputStream} if
-        * <tt>incudingSubStream</tt> is true.
-        * <p>
-        * You can call this method multiple times, it will not cause an
-        * {@link IOException} for subsequent calls.
-        * <p>
-        * <b>Note:</b> if you called the {@link NextableInputStream#open()} method
-        * prior to this one, it will just decrease the internal count of how many
-        * open streams it held and do nothing else. The stream will actually be
-        * closed when you have called {@link NextableInputStream#close()} once more
-        * than {@link NextableInputStream#open()}.
-        * 
-        * @exception IOException
-        *                in case of I/O error
-        */
-       public synchronized void close(boolean includingSubStream)
-                       throws IOException {
-               if (!closed) {
-                       if (openCounter > 0) {
-                               openCounter--;
-                       } else {
-                               closed = true;
-                               if (includingSubStream && in != null) {
-                                       in.close();
-                               }
-                       }
-               }
-       }
-
        /**
         * Check if we still have some data in the buffer and, if not, fetch some.
         * 
@@ -308,24 +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);
-                       if (len > 0) {
-                               bytesRead += len;
-                       }
-
+       @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;
        }
 
        /**
@@ -333,8 +140,9 @@ public class NextableInputStream extends InputStream {
         * 
         * @return TRUE if it is the case, FALSE if not
         */
-       private boolean hasMoreData() {
-               return !closed && started && !(eof && pos >= len);
+       @Override
+       protected boolean hasMoreData() {
+               return started && super.hasMoreData();
        }
 
        /**
@@ -410,18 +218,4 @@ public class NextableInputStream extends InputStream {
 
                return false;
        }
-
-       /**
-        * Check that the stream was not closed, and throw an {@link IOException} if
-        * it was.
-        * 
-        * @throws IOException
-        *             if it was closed
-        */
-       private void checkClose() throws IOException {
-               if (closed) {
-                       throw new IOException(
-                                       "This NextableInputStream was closed, you cannot use it anymore.");
-               }
-       }
 }