public long readNextOffset() throws IOException {
        FetchDataInfo fetchData = read(offsetIndex().lastOffset(), log.sizeInBytes());
        if (fetchData == null)
            return baseOffset;
        else
            return fetchData.records.lastBatch()
                .map(batch -> batch.nextOffset())
                .orElse(baseOffset);
    }
Calculate the offset that would be used for the next message to be append to this segment.
Note that this is expensive.

This method is thread-safe.
storage/src/main/java/org/apache/kafka/storage/internals/log/LogSegment.java