X-Git-Url: http://git.nikiroo.be/?p=nikiroo-utils.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2FBufferedInputStream.java;h=b236f8dac290d5406f18c8e758e62804a3f0af1c;hp=9b95574b8165ea8d093cafe4869b8b307886a8ac;hb=eeaa5ebc0ebe6ffd860273ed6727024737085a25;hpb=876dbf8b4e0f2498fb41f626f98e5fae46600b7b diff --git a/src/be/nikiroo/utils/BufferedInputStream.java b/src/be/nikiroo/utils/BufferedInputStream.java index 9b95574..b236f8d 100644 --- a/src/be/nikiroo/utils/BufferedInputStream.java +++ b/src/be/nikiroo/utils/BufferedInputStream.java @@ -14,9 +14,13 @@ import java.util.Arrays; * @author niki */ public class BufferedInputStream extends InputStream { + /** The current position in the buffer. */ protected int pos; + /** The index of the last usable position of the buffer. */ protected int len; + /** The buffer itself. */ protected byte[] buffer; + /** An End-Of-File (or buffer, here) marker. */ protected boolean eof; private boolean closed; @@ -112,12 +116,42 @@ public class BufferedInputStream extends InputStream { return this; } - // max is buffer.size ! + /** + * Check if the current content (what will be read next) starts with the + * given search term. + *

+ * Note: the search term size must 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 startsWiths(String search) throws IOException { return startsWith(search.getBytes("UTF-8")); } - // max is buffer.size ! + /** + * Check if the current content (what will be read next) starts with the + * given search term. + *

+ * Note: the search term size must 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 startsWith(byte[] search) throws IOException { if (search.length > originalBuffer.length) { throw new IOException( @@ -133,7 +167,7 @@ public class BufferedInputStream extends InputStream { if (available() >= search.length) { // Easy path - return startsWith(search, buffer, pos); + return startsWith(search, buffer, pos, len); } else if (!eof) { // Harder path if (buffer2 == null && buffer.length == originalBuffer.length) { @@ -149,9 +183,7 @@ public class BufferedInputStream extends InputStream { len2 += pos2; } - if (available() + (len2 - pos2) >= search.length) { - return startsWith(search, buffer2, pos2); - } + return startsWith(search, buffer2, pos2, len2); } return false; @@ -288,6 +320,9 @@ public class BufferedInputStream extends InputStream { * closed when you have called {@link BufferedInputStream#close()} once more * than {@link BufferedInputStream#open()}. * + * @param includingSubStream + * also close the under-laying stream + * * @exception IOException * in case of I/O error */ @@ -381,12 +416,41 @@ public class BufferedInputStream extends InputStream { protected void checkClose() throws IOException { if (closed) { throw new IOException( - "This NextableInputStream was closed, you cannot use it anymore."); + "This BufferedInputStream was closed, you cannot use it anymore."); } } - // buffer must be > search - static protected boolean startsWith(byte[] search, byte[] buffer, int offset) { + /** + * Check if the buffer starts with the given search term (given as an array, + * a start position and a end position). + *

+ * Note: the parameter len is the index of the last + * position, not the length. + *

+ * Note: the search term size must be smaller or equal the internal + * buffer size. + * + * @param search + * the term to search for + * @param buffer + * the buffer to look into + * @param offset + * the offset at which to start the search + * @param len + * the maximum index of the data to check (this is not a + * length, but an index) + * + * @return TRUE if the search content is present at the given location and + * does not exceed the len index + */ + static protected boolean startsWith(byte[] search, byte[] buffer, + int offset, int len) { + + // Check if there even is enough space for it + if (search.length > (len - offset)) { + return false; + } + boolean same = true; for (int i = 0; i < search.length; i++) { if (search[i] != buffer[offset + i]) {