code cleanup + BufInputStream.is()
[nikiroo-utils.git] / src / be / nikiroo / utils / streams / BufferedInputStream.java
index 42f0d9d24c123be0fad89cce35f43bb9b5831855..336dba4000c54a26b25e33d0e84a2d8f46b2a01d 100644 (file)
@@ -4,6 +4,8 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.Arrays;
 
+import be.nikiroo.utils.StringUtils;
+
 /**
  * A simple {@link InputStream} that is buffered with a bytes array.
  * <p>
@@ -103,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
@@ -125,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 == search.length;
+               }
+
+               return false;
+       }
+
        /**
         * Check if the current content (what will be read next) starts with the
         * given search term.
@@ -142,7 +197,7 @@ public class BufferedInputStream extends InputStream {
         *             greater than the internal buffer
         */
        public boolean startsWiths(String search) throws IOException {
-               return startsWith(search.getBytes("UTF-8"));
+               return startsWith(StringUtils.getBytes(search));
        }
 
        /**