- 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()) {
- int now = Math.min(blen, len) - pos;
- if (now > 0) {
- System.arraycopy(buffer, pos, b, boff, now);
- pos += now;
- done += now;
- }
- }
- }
-
- 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();
- }
- }
- }