X-Git-Url: http://git.nikiroo.be/?p=nikiroo-utils.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2FBufferedInputStream.java;h=aa455a2426d0b96d2534f27c0740c545510c858f;hp=b236f8dac290d5406f18c8e758e62804a3f0af1c;hb=a26188d393b11040b8ee8476338a73bfadabffb6;hpb=eeaa5ebc0ebe6ffd860273ed6727024737085a25 diff --git a/src/be/nikiroo/utils/BufferedInputStream.java b/src/be/nikiroo/utils/BufferedInputStream.java index b236f8d..aa455a2 100644 --- a/src/be/nikiroo/utils/BufferedInputStream.java +++ b/src/be/nikiroo/utils/BufferedInputStream.java @@ -15,9 +15,9 @@ import java.util.Arrays; */ public class BufferedInputStream extends InputStream { /** The current position in the buffer. */ - protected int pos; + protected int start; /** The index of the last usable position of the buffer. */ - protected int len; + protected int stop; /** The buffer itself. */ protected byte[] buffer; /** An End-Of-File (or buffer, here) marker. */ @@ -47,8 +47,8 @@ public class BufferedInputStream extends InputStream { this.buffer = new byte[4096]; this.originalBuffer = this.buffer; - this.pos = 0; - this.len = 0; + this.start = 0; + this.stop = 0; } /** @@ -90,8 +90,8 @@ public class BufferedInputStream extends InputStream { this.buffer = in; this.originalBuffer = this.buffer; - this.pos = offset; - this.len = length; + this.start = offset; + this.stop = length; } /** @@ -167,7 +167,7 @@ public class BufferedInputStream extends InputStream { if (available() >= search.length) { // Easy path - return startsWith(search, buffer, pos, len); + return startsWith(search, buffer, start, stop); } else if (!eof) { // Harder path if (buffer2 == null && buffer.length == originalBuffer.length) { @@ -201,14 +201,11 @@ public class BufferedInputStream 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 */ public boolean eof() { - return closed || (len < 0 && !hasMoreData()); + return closed || (stop < 0 && !hasMoreData()); } @Override @@ -220,7 +217,7 @@ public class BufferedInputStream extends InputStream { return -1; } - return buffer[pos++]; + return buffer[start++]; } @Override @@ -244,10 +241,10 @@ public class BufferedInputStream extends InputStream { while (hasMoreData() && done < blen) { preRead(); if (hasMoreData()) { - int now = Math.min(blen, len) - pos; + int now = Math.min(blen, stop) - start; if (now > 0) { - System.arraycopy(buffer, pos, b, boff, now); - pos += now; + System.arraycopy(buffer, start, b, boff + done, now); + start += now; done += now; } } @@ -267,7 +264,7 @@ public class BufferedInputStream extends InputStream { preRead(); long inBuffer = Math.min(n, available()); - pos += inBuffer; + start += inBuffer; n -= inBuffer; skipped += inBuffer; } @@ -281,7 +278,7 @@ public class BufferedInputStream extends InputStream { return 0; } - return Math.max(0, len - pos); + return Math.max(0, stop - start); } /** @@ -351,12 +348,12 @@ public class BufferedInputStream extends InputStream { */ protected boolean preRead() throws IOException { boolean hasRead = false; - if (!eof && in != null && pos >= len) { - pos = 0; + if (!eof && in != null && start >= stop) { + start = 0; if (buffer2 != null) { buffer = buffer2; - pos = pos2; - len = len2; + start = pos2; + stop = len2; buffer2 = null; pos2 = 0; @@ -364,16 +361,16 @@ public class BufferedInputStream extends InputStream { } else { buffer = originalBuffer; - len = read(in, buffer); - if (len > 0) { - bytesRead += len; + stop = read(in, buffer); + if (stop > 0) { + bytesRead += stop; } } hasRead = true; } - if (pos >= len) { + if (start >= stop) { eof = true; } @@ -403,7 +400,7 @@ public class BufferedInputStream extends InputStream { * @return TRUE if it is the case, FALSE if not */ protected boolean hasMoreData() { - return !closed && !(eof && pos >= len); + return !closed && !(eof && start >= stop); } /**