X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2FNextableInputStream.java;h=d9f27ceff87446bf2e6f5bfe49578b4bf687986c;hb=59509e75f7a88cefad9c5478324bbeb64a871d31;hp=7ced59804b02ce5ccad360cf8c193ef1992765f2;hpb=32d89af14cbf18589a1daca5f999f8cedd13dd40;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/NextableInputStream.java b/src/be/nikiroo/utils/NextableInputStream.java index 7ced598..d9f27ce 100644 --- a/src/be/nikiroo/utils/NextableInputStream.java +++ b/src/be/nikiroo/utils/NextableInputStream.java @@ -224,6 +224,9 @@ public class NextableInputStream extends InputStream { /** * Check if this stream is totally spent (no more data to read or to * process). + *

+ * Note: an empty stream that is still not started will return FALSE, as we + * don't know yet if it is empty. * * @return TRUE if it is */ @@ -264,10 +267,11 @@ public class NextableInputStream extends InputStream { while (hasMoreData() && done < blen) { preRead(); if (hasMoreData()) { - for (int i = pos; i < blen && i < len; i++) { - b[boff + done] = buffer[i]; - pos++; - done++; + int now = Math.min(blen, len) - pos; + if (now > 0) { + System.arraycopy(buffer, pos, b, boff, now); + pos += now; + done += now; } } } @@ -277,8 +281,21 @@ public class NextableInputStream extends InputStream { @Override public long skip(long n) throws IOException { - // TODO Auto-generated method stub - return super.skip(n); + 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 @@ -481,8 +498,7 @@ public class NextableInputStream extends InputStream { } // buffer must be > search - static private boolean startsWith(byte[] search, byte[] buffer, - int offset) { + static private boolean startsWith(byte[] search, byte[] buffer, int offset) { boolean same = true; for (int i = 0; i < search.length; i++) { if (search[i] != buffer[offset + i]) {