X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;ds=inline;f=src%2Fbe%2Fnikiroo%2Futils%2Fstreams%2FBufferedInputStream.java;h=3cad70d2152fc433c5313b8fbec1292f9c3ff9d2;hb=b8b60b3217e62f1896cc3a57763bddd147474695;hp=7c20f634824841c8c7dd5d60fe94456dce1c78da;hpb=8e76f6ab13a8a4a651f2518b6c91d5e6424c7922;p=fanfix.git diff --git a/src/be/nikiroo/utils/streams/BufferedInputStream.java b/src/be/nikiroo/utils/streams/BufferedInputStream.java index 7c20f63..3cad70d 100644 --- a/src/be/nikiroo/utils/streams/BufferedInputStream.java +++ b/src/be/nikiroo/utils/streams/BufferedInputStream.java @@ -2,7 +2,12 @@ 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; /** * A simple {@link InputStream} that is buffered with a bytes array. @@ -14,24 +19,32 @@ import java.util.Arrays; * @author niki */ public class BufferedInputStream extends InputStream { + /** + * The size of the internal buffer (can be different if you pass your own + * buffer, of course, and can also expand to search for longer "startsWith" + * data). + *
+ * Note that special "push-back" buffers can also be created during the life
+ * of this stream.
+ */
+ static private final int BUFFER_SIZE = 4096;
+
/** The current position in the buffer. */
protected int start;
/** The index of the last usable position of the buffer. */
protected int stop;
/** The buffer itself. */
protected byte[] buffer;
- /** An End-Of-File (or buffer, here) marker. */
+ /** An End-Of-File (or {@link InputStream}, here) marker. */
protected boolean eof;
private boolean closed;
private InputStream in;
private int openCounter;
+ private byte[] singleByteReader = new byte[1];
- // 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
+ * 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.
@@ -132,14 +189,17 @@ 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 {
- return startsWith(search.getBytes("UTF-8"));
+ public boolean startsWith(String search) throws IOException {
+ return startsWith(StringUtils.getBytes(search));
}
/**
* 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.
*
@@ -153,40 +213,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 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 = in.read(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 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
+ * 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
- * 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]) {
- same = false;
- break;
- }
- }
-
- return same;
- }
}