now mostly streamified!
[nikiroo-utils.git] / src / be / nikiroo / utils / streams / BufferedInputStream.java
index da862e3f6802aa22f47586196a680d53d89d302f..683fa55865aff5ee4b6d442638721372c99e0e02 100644 (file)
@@ -105,6 +105,15 @@ public class BufferedInputStream extends InputStream {
                this.stop = length;
        }
 
+       /**
+        * The internal buffer size (can be useful to know for search methods).
+        * 
+        * @return the size of the internal buffer, in bytes.
+        */
+       public int getInternalBufferSize() {
+               return originalBuffer.length;
+       }
+
        /**
         * Return this very same {@link BufferedInputStream}, but keep a counter of
         * how many streams were open this way. When calling
@@ -127,6 +136,50 @@ public class BufferedInputStream extends InputStream {
                return this;
        }
 
+       /**
+        * Check if the current content (until eof) is equal to the given search
+        * term.
+        * <p>
+        * Note: the search term size <b>must</b> be smaller or equal the internal
+        * buffer size.
+        * 
+        * @param search
+        *            the term to search for
+        * 
+        * @return TRUE if the content that will be read starts with it
+        * 
+        * @throws IOException
+        *             in case of I/O error or if the size of the search term is
+        *             greater than the internal buffer
+        */
+       public boolean is(String search) throws IOException {
+               return is(StringUtils.getBytes(search));
+       }
+
+       /**
+        * Check if the current content (until eof) is equal to the given search
+        * term.
+        * <p>
+        * Note: the search term size <b>must</b> be smaller or equal the internal
+        * buffer size.
+        * 
+        * @param search
+        *            the term to search for
+        * 
+        * @return TRUE if the content that will be read starts with it
+        * 
+        * @throws IOException
+        *             in case of I/O error or if the size of the search term is
+        *             greater than the internal buffer
+        */
+       public boolean is(byte[] search) throws IOException {
+               if (startsWith(search)) {
+                       return (stop - start) == search.length;
+               }
+
+               return false;
+       }
+
        /**
         * Check if the current content (what will be read next) starts with the
         * given search term.
@@ -143,7 +196,7 @@ public class BufferedInputStream extends InputStream {
         *             in case of I/O error or if the size of the search term is
         *             greater than the internal buffer
         */
-       public boolean startsWiths(String search) throws IOException {
+       public boolean startsWith(String search) throws IOException {
                return startsWith(StringUtils.getBytes(search));
        }
 
@@ -151,6 +204,9 @@ public class BufferedInputStream extends InputStream {
         * Check if the current content (what will be read next) starts with the
         * given search term.
         * <p>
+        * An empty string will always return true (unless the stream is closed,
+        * which would throw an {@link IOException}).
+        * <p>
         * Note: the search term size <b>must</b> be smaller or equal the internal
         * buffer size.
         * 
@@ -179,7 +235,7 @@ public class BufferedInputStream extends InputStream {
                if (available() >= search.length) {
                        // Easy path
                        return StreamUtils.startsWith(search, buffer, start, stop);
-               } else if (!eof) {
+               } else if (in != null && !eof) {
                        // Harder path
                        if (buffer2 == null && buffer.length == originalBuffer.length) {
                                buffer2 = Arrays.copyOf(buffer, buffer.length * 2);
@@ -210,7 +266,7 @@ public class BufferedInputStream extends InputStream {
        }
 
        /**
-        * Check if this stream is totally spent (no more data to read or to
+        * Check if this stream is spent (no more data to read or to
         * process).
         * 
         * @return TRUE if it is
@@ -227,6 +283,24 @@ public class BufferedInputStream extends InputStream {
                return !hasMoreData();
        }
 
+       /**
+        * Read the whole {@link InputStream} until the end and return the number of
+        * bytes read.
+        * 
+        * @return the number of bytes read
+        * 
+        * @throws IOException
+        *             in case of I/O error
+        */
+       public long end() throws IOException {
+               long skipped = 0;
+               while (hasMoreData()) {
+                       skipped += skip(buffer.length);
+               }
+
+               return skipped;
+       }
+
        @Override
        public int read() throws IOException {
                checkClose();