private int appendChunkFromFile(FileRecords records, int position, BufferSupplier bufferSupplier) throws IOException {
        int bytesToAppend = 0;
        long maxTimestamp = Long.MIN_VALUE;
        long shallowOffsetOfMaxTimestamp = Long.MIN_VALUE;
        long maxOffset = Long.MIN_VALUE;
        ByteBuffer readBuffer = bufferSupplier.get(1024 * 1024);

        // find all batches that are valid to be appended to the current log segment and
        // determine the maximum offset and timestamp
        Iterator<FileChannelRecordBatch> nextBatches = records.batchesFrom(position).iterator();
        FileChannelRecordBatch batch;
        while ((batch = nextAppendableBatch(nextBatches, readBuffer, bytesToAppend)) != null) {
            if (batch.maxTimestamp() > maxTimestamp) {
                maxTimestamp = batch.maxTimestamp();
                shallowOffsetOfMaxTimestamp = batch.lastOffset();
            }
            maxOffset = batch.lastOffset();
            bytesToAppend += batch.sizeInBytes();
        }

        if (bytesToAppend > 0) {
            // Grow buffer if needed to ensure we copy at least one batch
            if (readBuffer.capacity() < bytesToAppend)
                readBuffer = bufferSupplier.get(bytesToAppend);

            readBuffer.limit(bytesToAppend);
            records.readInto(readBuffer, position);

            append(maxOffset, maxTimestamp, shallowOffsetOfMaxTimestamp, MemoryRecords.readableRecords(readBuffer));
        }

        bufferSupplier.release(readBuffer);
        return bytesToAppend;
    }
Append the given messages starting with the given offset. Add
an entry to the index if needed.

It is assumed this method is being called from within a lock, it is not thread-safe otherwise.

@param largestOffset The last offset in the message set
@param largestTimestampMs The largest timestamp in the message set.
@param shallowOffsetOfMaxTimestamp The last offset of earliest batch with max timestamp in the messages to append.
@param records The log entries to append.
@throws LogSegmentOffsetOverflowException if the largest offset causes index offset overflow
storage/src/main/java/org/apache/kafka/storage/internals/log/LogSegment.java