client-mr/core/src/main/java/org/apache/hadoop/mapred/SortWriteBuffer.java [142:200]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    for (Record<K> record : records) {
      offset = writeDataInt(data, offset, record.getKeyLength());
      offset = writeDataInt(data, offset, record.getValueLength());
      int recordLength = record.getKeyLength() + record.getValueLength();
      int copyOffset = record.getKeyOffSet();
      int copyIndex = record.getKeyIndex();
      while (recordLength > 0) {
        byte[] srcBytes = buffers.get(copyIndex).getBuffer();
        int length = copyOffset + recordLength;
        int copyLength = recordLength;
        if (length > srcBytes.length) {
          copyLength = srcBytes.length - copyOffset;
        }
        System.arraycopy(srcBytes, copyOffset, data, offset, copyLength);
        copyOffset = 0;
        copyIndex++;
        recordLength -= copyLength;
        offset += copyLength;
      }
    }
    offset = writeDataInt(data, offset, -1);
    writeDataInt(data, offset, -1);
    copyTime += System.currentTimeMillis() - startCopy;
    return data;
  }

  private boolean compact(int lastIndex, int lastOffset, int dataLength) {
    if (lastIndex != currentIndex) {
      if (LOG.isDebugEnabled()) {
        LOG.debug(
            "compact lastIndex {}, currentIndex {}, lastOffset {} currentOffset {} dataLength {}",
            lastIndex,
            currentIndex,
            lastOffset,
            currentOffset,
            dataLength);
      }
      WrappedBuffer buffer = new WrappedBuffer(lastOffset + dataLength);
      // copy data
      int offset = 0;
      for (int i = lastIndex; i < currentIndex; i++) {
        byte[] sourceBuffer = buffers.get(i).getBuffer();
        System.arraycopy(sourceBuffer, 0, buffer.getBuffer(), offset, sourceBuffer.length);
        offset += sourceBuffer.length;
      }
      System.arraycopy(
          buffers.get(currentIndex).getBuffer(), 0, buffer.getBuffer(), offset, currentOffset);
      // remove data
      for (int i = currentIndex; i >= lastIndex; i--) {
        buffers.remove(i);
      }
      buffers.add(buffer);
      currentOffset = 0;
      WrappedBuffer anotherBuffer = new WrappedBuffer((int) maxSegmentSize);
      buffers.add(anotherBuffer);
      currentIndex = buffers.size() - 1;
      return true;
    }
    return false;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



client-tez/src/main/java/org/apache/tez/runtime/library/common/sort/buffer/WriteBuffer.java [146:204]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    for (Record<K> record : records) {
      offset = writeDataInt(data, offset, record.getKeyLength());
      offset = writeDataInt(data, offset, record.getValueLength());
      int recordLength = record.getKeyLength() + record.getValueLength();
      int copyOffset = record.getKeyOffSet();
      int copyIndex = record.getKeyIndex();
      while (recordLength > 0) {
        byte[] srcBytes = buffers.get(copyIndex).getBuffer();
        int length = copyOffset + recordLength;
        int copyLength = recordLength;
        if (length > srcBytes.length) {
          copyLength = srcBytes.length - copyOffset;
        }
        System.arraycopy(srcBytes, copyOffset, data, offset, copyLength);
        copyOffset = 0;
        copyIndex++;
        recordLength -= copyLength;
        offset += copyLength;
      }
    }
    offset = writeDataInt(data, offset, -1);
    writeDataInt(data, offset, -1);
    copyTime += System.currentTimeMillis() - startCopy;
    return data;
  }

  private boolean compact(int lastIndex, int lastOffset, int dataLength) {
    if (lastIndex != currentIndex) {
      if (LOG.isDebugEnabled()) {
        LOG.debug(
            "compact lastIndex {}, currentIndex {}, lastOffset {} currentOffset {} dataLength {}",
            lastIndex,
            currentIndex,
            lastOffset,
            currentOffset,
            dataLength);
      }
      WrappedBuffer buffer = new WrappedBuffer(lastOffset + dataLength);
      // copy data
      int offset = 0;
      for (int i = lastIndex; i < currentIndex; i++) {
        byte[] sourceBuffer = buffers.get(i).getBuffer();
        System.arraycopy(sourceBuffer, 0, buffer.getBuffer(), offset, sourceBuffer.length);
        offset += sourceBuffer.length;
      }
      System.arraycopy(
          buffers.get(currentIndex).getBuffer(), 0, buffer.getBuffer(), offset, currentOffset);
      // remove data
      for (int i = currentIndex; i >= lastIndex; i--) {
        buffers.remove(i);
      }
      buffers.add(buffer);
      currentOffset = 0;
      WrappedBuffer anotherBuffer = new WrappedBuffer((int) maxSegmentSize);
      buffers.add(anotherBuffer);
      currentIndex = buffers.size() - 1;
      return true;
    }
    return false;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



