- // max is buffer.size !
- public boolean startsWiths(String search) throws IOException {
- return startsWith(search.getBytes("UTF-8"));
- }
-
- // max is buffer.size !
- 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) {
- preRead();
- }
-
- if (available() >= search.length) {
- // Easy path
- return startsWith(search, buffer, pos);
- } 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 (available() + (len2 - pos2) >= search.length) {
- return startsWith(search, buffer2, pos2);
- }
- }
-
- return false;
- }
-
- /**
- * The number of bytes read from the under-laying {@link InputStream}.
- *
- * @return the number of bytes
- */
- public long getBytesRead() {
- return bytesRead;
- }
-
- /**
- * Check if this stream is totally spent (no more data to read or to
- * process).
- *
- * @return TRUE if it is
- */
- public boolean eof() {
- return closed || (len < 0 && !hasMoreData());
- }
-
- @Override
- public int read() throws IOException {
- checkClose();
-
- preRead();
- if (eof) {
- return -1;
- }
-
- return buffer[pos++];
- }
-
- @Override
- public int read(byte[] b) throws IOException {
- return read(b, 0, b.length);
- }
-
- @Override
- public int read(byte[] b, int boff, int blen) throws IOException {
- checkClose();
-
- if (b == null) {
- throw new NullPointerException();
- } else if (boff < 0 || blen < 0 || blen > b.length - boff) {
- throw new IndexOutOfBoundsException();
- } else if (blen == 0) {
- return 0;
- }
-
- int done = 0;
- while (hasMoreData() && done < blen) {
- preRead();
- if (hasMoreData()) {
- for (int i = pos; i < blen && i < len; i++) {
- b[boff + done] = buffer[i];
- pos++;
- done++;
- }
- }
- }
-
- return done > 0 ? done : -1;
- }
-
- @Override
- public long skip(long n) throws IOException {
- if (n <= 0) {
- return 0;
- }
-
- long skipped = 0;
- while (hasMoreData() && n > 0) {
- preRead();
-
- long inBuffer = Math.min(n, available());
- pos += inBuffer;
- n -= inBuffer;
- skipped += inBuffer;
- }
-
- return skipped;
- }
-
- @Override
- public int available() {
- if (closed) {
- return 0;
- }
-
- return Math.max(0, len - pos);
- }
-
- /**
- * Closes this stream and releases any system resources associated with the
- * stream.
- * <p>
- * Including the under-laying {@link InputStream}.
- * <p>
- * <b>Note:</b> if you called the {@link NextableInputStream#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 NextableInputStream#close()} once more
- * than {@link NextableInputStream#open()}.
- *
- * @exception IOException
- * in case of I/O error
- */
- @Override
- public synchronized void close() throws IOException {
- close(true);
- }
-
- /**
- * Closes this stream and releases any system resources associated with the
- * stream.
- * <p>
- * Including the under-laying {@link InputStream} if
- * <tt>incudingSubStream</tt> is true.
- * <p>
- * You can call this method multiple times, it will not cause an
- * {@link IOException} for subsequent calls.
- * <p>
- * <b>Note:</b> if you called the {@link NextableInputStream#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 NextableInputStream#close()} once more
- * than {@link NextableInputStream#open()}.
- *
- * @exception IOException
- * in case of I/O error
- */
- public synchronized void close(boolean includingSubStream)
- throws IOException {
- if (!closed) {
- if (openCounter > 0) {
- openCounter--;
- } else {
- closed = true;
- if (includingSubStream && in != null) {
- in.close();
- }
- }
- }
- }
-