@Override
  public int read(byte[] buffer, int offset, int length) throws IOException {
    Preconditions.checkState(mBufferOffset <= mBufferedSize);
    ensureNotClosed();
    if (!ensureDataInBuffer()) {
      return -1;
    }

    final int bytesToRead = Math.min(mBufferedSize - mBufferOffset, length);
    System.arraycopy(mByteArray, mBufferOffset, buffer, offset, bytesToRead);
    mBufferOffset += bytesToRead;
    return bytesToRead;
  }
position of next buffered byte in mByteArray to be read

<p>invariant: 0 <= mBufferOffset <= mBufferedSize
fbcore/src/main/java/com/facebook/common/memory/PooledByteArrayBufferedInputStream.java