+ // The only time we can return 0 is if len is 0, as per the
+ // InputStream contract.
+ if (len == 0) {
+ return 0;
+ }
+
+ // If the post-processed buffer has bytes, use that.
+ if (readBufferEnd - readBufferStart > 0) {
+ int n = Math.min(len, readBufferEnd - readBufferStart);
+ System.arraycopy(b, off, readBuffer, readBufferStart, n);
+ readBufferStart += n;
+ return n;
+ }
+
+ // The buffer is empty, so reset the indexes to 0.
+ readBufferStart = 0;
+ readBufferEnd = 0;
+
+ // The maximum number of bytes we will ask for will definitely be
+ // within the bounds of what we can return in a single call.
+ int n = Math.min(len, readBuffer.length);
+
+ // Read some fresh data and run it through the telnet protocol.
+ int rc = readImpl(readBuffer, readBufferEnd, n);
+
+ // If we got something, return it.
+ if (rc > 0) {
+ System.arraycopy(readBuffer, 0, b, off, len);
+ return rc;
+ }
+ // If we read 0, I screwed up big time.
+ assert (rc != 0);
+
+ // We read -1 (EOF).
+ return rc;