public boolean resize(int newSize) throws IOException {
        lock.lock();
        try {
            int roundedNewSize = roundDownToExactMultiple(newSize, entrySize());

            if (length == roundedNewSize) {
                log.debug("Index {} was not resized because it already has size {}", file.getAbsolutePath(), roundedNewSize);
                return false;
            } else {
                RandomAccessFile raf = new RandomAccessFile(file, "rw");
                try {
                    int position = mmap.position();

                    /* Windows or z/OS won't let us modify the file length while the file is mmapped :-( */
                    if (OperatingSystem.IS_WINDOWS || OperatingSystem.IS_ZOS)
                        safeForceUnmap();
                    raf.setLength(roundedNewSize);
                    this.length = roundedNewSize;
                    mmap = raf.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, roundedNewSize);
                    this.maxEntries = mmap.limit() / entrySize();
                    mmap.position(position);
                    log.debug("Resized {} to {}, position is {} and limit is {}", file.getAbsolutePath(), roundedNewSize,
                            mmap.position(), mmap.limit());
                    return true;
                } finally {
                    Utils.closeQuietly(raf, "index file " + file.getName());
                }
            }
        } finally {
            lock.unlock();
        }
    }
Reset the size of the memory map and the underneath file. This is used in two kinds of cases: (1) in
trimToValidSize() which is called at closing the segment or new segment being rolled; (2) at
loading segments from disk or truncating back to an old segment where a new log segment became active;
we want to reset the index size to maximum index size to avoid rolling new segment.

@param newSize new size of the index file
@return a boolean indicating whether the size of the memory map and the underneath file is changed or not.
storage/src/main/java/org/apache/kafka/storage/internals/log/AbstractIndex.java