Github user vanzin commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5400#discussion_r27925549
  
    --- Diff: 
network/common/src/main/java/org/apache/spark/network/buffer/WrappedLargeByteBuffer.java
 ---
    @@ -0,0 +1,252 @@
    +/*
    +* Licensed to the Apache Software Foundation (ASF) under one or more
    +* contributor license agreements.  See the NOTICE file distributed with
    +* this work for additional information regarding copyright ownership.
    +* The ASF licenses this file to You under the Apache License, Version 2.0
    +* (the "License"); you may not use this file except in compliance with
    +* the License.  You may obtain a copy of the License at
    +*
    +*    http://www.apache.org/licenses/LICENSE-2.0
    +*
    +* Unless required by applicable law or agreed to in writing, software
    +* distributed under the License is distributed on an "AS IS" BASIS,
    +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    +* See the License for the specific language governing permissions and
    +* limitations under the License.
    +*/
    +package org.apache.spark.network.buffer;
    +
    +import sun.nio.ch.DirectBuffer;
    +
    +import java.io.IOException;
    +import java.nio.BufferUnderflowException;
    +import java.nio.ByteBuffer;
    +import java.nio.MappedByteBuffer;
    +import java.nio.channels.WritableByteChannel;
    +import java.util.Arrays;
    +import java.util.List;
    +
    +public class WrappedLargeByteBuffer implements LargeByteBuffer {
    +
    +  //only public for tests
    +  public final ByteBuffer[] underlying;
    +
    +  private final long size;
    +  private long _pos;
    +  private int currentBufferIdx;
    +  private ByteBuffer currentBuffer;
    +
    +
    +  public WrappedLargeByteBuffer(ByteBuffer[] underlying) {
    +    this(underlying, findExpectedInitialPosition(underlying));
    +  }
    +
    +  private static long findExpectedInitialPosition(ByteBuffer[] bufs) {
    +    long sum = 0L;
    +    for (ByteBuffer b: bufs) {
    +      if (b.position() > 0) {
    +        // this could still lead to a mix of positions half-way through 
buffers that
    +        // would be inconsistent -- but we'll discover that in the 
constructor checks
    +        sum += b.position();
    +      } else {
    +        break;
    +      }
    +    }
    +    return sum;
    +  }
    +
    +  private WrappedLargeByteBuffer(ByteBuffer[] underlying, long 
initialPosition) {
    +    this.underlying = underlying;
    +    long sum = 0L;
    +    for (int i = 0; i < underlying.length; i++) {
    +      ByteBuffer b = underlying[i];
    +      long nextSum = sum + b.capacity();
    +      int expectedPosition;
    +      if (nextSum < initialPosition) {
    +        expectedPosition = b.capacity();
    +      } else if (sum > initialPosition) {
    +        expectedPosition = 0;
    +      } else {
    +        expectedPosition = (int) (initialPosition - sum);
    +      }
    +      if (b.position() != expectedPosition) {
    +        throw new IllegalArgumentException("ByteBuffer[" + i + "]:" + b + 
" was expected to have" +
    +          " position = " + expectedPosition + " to be consistent with the 
overall " +
    +          "initialPosition = " + initialPosition);
    +      }
    +      sum = nextSum;
    +    }
    +    _pos = initialPosition;
    +    currentBufferIdx = 0;
    +    currentBuffer = underlying[0];
    +    size = sum;
    +  }
    +
    +  @Override
    +  public void get(byte[] dest, int offset, int length) {
    +    if (length > remaining())
    --- End diff --
    
    nit: add braces


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to