code cleanup + fixes
[nikiroo-utils.git] / src / be / nikiroo / utils / streams / BufferedInputStream.java
index 336dba4000c54a26b25e33d0e84a2d8f46b2a01d..d1f53dfe26b0a84367a56a61815477c1c321073e 100644 (file)
@@ -174,7 +174,7 @@ public class BufferedInputStream extends InputStream {
         */
        public boolean is(byte[] search) throws IOException {
                if (startsWith(search)) {
-                       return stop == search.length;
+                       return (stop - start) == search.length;
                }
 
                return false;
@@ -196,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));
        }
 
@@ -204,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.
         * 
@@ -232,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);
@@ -280,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();