code: private void buildIndex() {
      // Read the current buffer position.
      long position = buffer.mark().position();
  
      // Read the first entry length.
      int length = buffer.readInt();
  
      // While the length is non-zero...
      while (length != 0) {
        // Read the full entry into memory.
        buffer.read(memory.clear().limit(length));
  
        // Flip the in-memory buffer.
        memory.flip();
  
        // Read the 64-bit entry checksum.
        long checksum = memory.readUnsignedInt();
  
        // Read the 64-bit entry offset.
        long offset = memory.readLong();
  
        // If the term is set on the entry, read the term.
        Long term = memory.readBoolean() ? memory.readLong() : null;
  
        // Calculate the entry position and length.
        int entryPosition = (int) memory.position();
        int entryLength = length - entryPosition;
  
        // Compute the checksum for the entry bytes.
        Checksum crc32 = new CRC32();
        crc32.update(memory.array(), entryPosition, entryLength);
  
        // If the computed checksum equals the stored checksum...
        if (checksum == crc32.getValue()) {
          // If the entry contained a term, index the term.
          if (term != null) {
            termIndex.index(offset, term);
          }
  
          // Index the entry offset.
          offsetIndex.index(offset, position);
        } else {
          break;
        }
  
        // Store the next entry start position.
        position = buffer.position();
  
        // Read the next entry length.
        length = buffer.mark().readInt();
      }
  
      // Reset the buffer back to the start of the next entry.
      buffer.reset();
    }
docs: Builds the index from the segment bytes.