client-mr/core/src/main/java/org/apache/hadoop/mapred/SortWriteBuffer.java [141:172]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  private boolean compact(int lastIndex, int lastOffset, int dataLength) {
    if (lastIndex != currentIndex) {
      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 [148:179]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  private boolean compact(int lastIndex, int lastOffset, int dataLength) {
    if (lastIndex != currentIndex) {
      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;
  }
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



