X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;ds=sidebyside;f=src%2Fbe%2Fnikiroo%2Futils%2Fstreams%2FBufferedInputStream.java;h=babd2ce88ec929883713af1895a556d6c4178ad9;hb=6d765e484c2f50fd8f5cb3af425b59c51adaca0d;hp=da862e3f6802aa22f47586196a680d53d89d302f;hpb=f8147a0ee57317e96d9ff0bf19573f7168d0354c;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/streams/BufferedInputStream.java b/src/be/nikiroo/utils/streams/BufferedInputStream.java index da862e3..babd2ce 100644 --- a/src/be/nikiroo/utils/streams/BufferedInputStream.java +++ b/src/be/nikiroo/utils/streams/BufferedInputStream.java @@ -2,7 +2,10 @@ package be.nikiroo.utils.streams; import java.io.IOException; import java.io.InputStream; -import java.util.Arrays; +import java.util.AbstractMap; +import java.util.ArrayList; +import java.util.List; +import java.util.Map.Entry; import be.nikiroo.utils.StringUtils; @@ -18,10 +21,11 @@ import be.nikiroo.utils.StringUtils; public class BufferedInputStream extends InputStream { /** * The size of the internal buffer (can be different if you pass your own - * buffer, of course). + * buffer, of course, and can also expand to search for longer "startsWith" + * data). *
- * A second buffer of twice the size can sometimes be created as needed for
- * the {@link BufferedInputStream#startsWith(byte[])} search operation.
+ * Note that special "push-back" buffers can also be created during the life
+ * of this stream.
*/
static private final int BUFFER_SIZE = 4096;
@@ -38,16 +42,13 @@ public class BufferedInputStream extends InputStream {
private InputStream in;
private int openCounter;
- // special use, prefetched next buffer
- private byte[] buffer2;
- private int pos2;
- private int len2;
- private byte[] originalBuffer;
+ /** array + offset of pushed-back buffers */
+ private List
* You are now responsible for it — you must close it.
@@ -127,6 +128,50 @@ public class BufferedInputStream extends InputStream {
return this;
}
+ /**
+ * Check if the current content (until eof) is equal to 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 is(String search) throws IOException {
+ return is(StringUtils.getBytes(search));
+ }
+
+ /**
+ * Check if the current content (until eof) is equal to 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 is(byte[] search) throws IOException {
+ if (startsWith(search)) {
+ return available() == search.length;
+ }
+
+ return false;
+ }
+
/**
* Check if the current content (what will be read next) starts with the
* given search term.
@@ -143,7 +188,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 +196,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.
*
@@ -164,40 +212,27 @@ public class BufferedInputStream extends InputStream {
* greater than the internal buffer
*/
public boolean startsWith(byte[] search) throws IOException {
- if (search.length > originalBuffer.length) {
- throw new IOException(
- "This stream does not support searching for more than "
- + buffer.length + " bytes");
- }
-
checkClose();
- if (available() < search.length) {
+ while (consolidatePushBack(search.length) < search.length) {
preRead();
- }
-
- if (available() >= search.length) {
- // Easy path
- return StreamUtils.startsWith(search, buffer, start, stop);
- } else if (!eof) {
- // Harder path
- if (buffer2 == null && buffer.length == originalBuffer.length) {
- buffer2 = Arrays.copyOf(buffer, buffer.length * 2);
-
- pos2 = buffer.length;
- len2 = read(in, buffer2, pos2, buffer.length);
- if (len2 > 0) {
- bytesRead += len2;
- }
-
- // Note: here, len/len2 = INDEX of last good byte
- len2 += pos2;
+ if (start >= stop) {
+ // Not enough data left to start with that
+ return false;
}
- return StreamUtils.startsWith(search, buffer2, pos2, len2);
+ byte[] newBuffer = new byte[stop - start];
+ System.arraycopy(buffer, start, newBuffer, 0, stop - start);
+ pushback(newBuffer, 0);
+ start = stop;
}
- return false;
+ Entry
* Including the under-laying {@link InputStream}.
*
- * Note: if you called the {@link BufferedInputStream#open()} method
+ * Note: if you called the {@link BufferedInputStream2#open()} method
* prior to this one, it will just decrease the internal count of how many
* open streams it held and do nothing else. The stream will actually be
- * closed when you have called {@link BufferedInputStream#close()} once more
- * than {@link BufferedInputStream#open()}.
+ * closed when you have called {@link BufferedInputStream2#close()} once
+ * more than {@link BufferedInputStream2#open()}.
*
* @exception IOException
* in case of I/O error
@@ -330,11 +426,11 @@ public class BufferedInputStream extends InputStream {
* You can call this method multiple times, it will not cause an
* {@link IOException} for subsequent calls.
*
- * Note: if you called the {@link BufferedInputStream#open()} method
+ * Note: if you called the {@link BufferedInputStream2#open()} method
* prior to this one, it will just decrease the internal count of how many
* open streams it held and do nothing else. The stream will actually be
- * closed when you have called {@link BufferedInputStream#close()} once more
- * than {@link BufferedInputStream#open()}.
+ * closed when you have called {@link BufferedInputStream2#close()} once
+ * more than {@link BufferedInputStream2#open()}.
*
* @param includingSubStream
* also close the under-laying stream
@@ -356,6 +452,52 @@ public class BufferedInputStream extends InputStream {
}
}
+ /**
+ * Consolidate the push-back buffers so the last one is at least the given
+ * size, if possible.
+ *
+ * If there is not enough data in the push-back buffers, they will all be
+ * consolidated.
+ *
+ * @param size
+ * the minimum size of the consolidated buffer, or -1 to force
+ * the consolidation of all push-back buffers
+ *
+ * @return the size of the last, consolidated buffer; can be less than the
+ * requested size if not enough data
+ */
+ protected int consolidatePushBack(int size) {
+ int bbIndex = -1;
+ int bbUpToSize = 0;
+ for (Entry