code: protected int checkRead(int offset, int length) {
      checkOffset(offset);
      if (limit == -1) {
        if (offset + length > capacity) {
          if (capacity < maxCapacity) {
            if (this.offset + offset + length <= bytes.size()) {
              capacity = bytes.size() - this.offset;
            } else {
              capacity(calculateCapacity(offset + length));
            }
          } else {
            throw new BufferUnderflowException();
          }
        }
      } else {
        if (offset + length > limit) {
          throw new BufferUnderflowException();
        }
      }
      return offset(offset);
    }
docs: Checks bounds for a read.