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>
* @author niki
*/
public class BufferedInputStream extends InputStream {
+ /**
+ * The size of the internal buffer (can be different if you pass your own
+ * buffer, of course).
+ * <p>
+ * A second buffer of twice the size can sometimes be created as needed for
+ * the {@link BufferedInputStream#startsWith(byte[])} search operation.
+ */
+ 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;
public BufferedInputStream(InputStream in) {
this.in = in;
- this.buffer = new byte[4096];
+ this.buffer = new byte[BUFFER_SIZE];
this.originalBuffer = this.buffer;
this.start = 0;
this.stop = 0;
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
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.
* greater than the internal buffer
*/
public boolean startsWiths(String search) throws IOException {
- return startsWith(search.getBytes("UTF-8"));
+ return startsWith(StringUtils.getBytes(search));
}
/**
while (hasMoreData() && done < blen) {
preRead();
if (hasMoreData()) {
- int now = Math.min(blen, stop) - start;
+ int now = Math.min(blen - done, stop - start);
if (now > 0) {
System.arraycopy(buffer, start, b, boff + done, now);
start += now;