code: private int offset(long index) {
      int offset = (int) (index % buffer.length);
      if (offset < 0) {
        offset += buffer.length;
      }
      return offset;
    }
docs: Returns the buffer index for the given offset.