X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fstreams%2FBufferedInputStream.java;h=683fa55865aff5ee4b6d442638721372c99e0e02;hb=5584adbbbf5444c0039fed2b35dc7d5bb57b71b1;hp=336dba4000c54a26b25e33d0e84a2d8f46b2a01d;hpb=dce410fec00030c0affde7b19bcde6de847fae13;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/streams/BufferedInputStream.java b/src/be/nikiroo/utils/streams/BufferedInputStream.java index 336dba4..683fa55 100644 --- a/src/be/nikiroo/utils/streams/BufferedInputStream.java +++ b/src/be/nikiroo/utils/streams/BufferedInputStream.java @@ -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. *

+ * An empty string will always return true (unless the stream is closed, + * which would throw an {@link IOException}). + *

* Note: the search term size must 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); @@ -263,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 @@ -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();