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

    https://github.com/apache/spark/pull/5400#discussion_r27923456
  
    --- Diff: 
core/src/main/scala/org/apache/spark/util/LargeByteBufferOutputStream.scala ---
    @@ -0,0 +1,70 @@
    +/*
    + * 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.util
    +
    +import java.io.OutputStream
    +import java.nio.ByteBuffer
    +
    +import org.apache.spark.network.buffer.{LargeByteBufferHelper, 
WrappedLargeByteBuffer, LargeByteBuffer}
    +import org.apache.spark.util.io.ByteArrayChunkOutputStream
    +
    +private[spark]
    +class LargeByteBufferOutputStream(chunkSize: Int = 65536)
    +  extends OutputStream {
    +
    +  private[util] val output = new ByteArrayChunkOutputStream(chunkSize)
    +
    +  private var _pos = 0
    +
    +  override def write(b: Int): Unit = {
    +    output.write(b)
    +  }
    +
    +  override def write(bytes: Array[Byte], offs: Int, len: Int): Unit = {
    +    output.write(bytes, offs, len)
    +    _pos += len
    +  }
    +
    +  def largeBuffer: LargeByteBuffer = {
    +    largeBuffer(LargeByteBufferHelper.MAX_CHUNK)
    +  }
    +
    +  // exposed for testing
    +  private[util] def largeBuffer(maxChunk: Int): LargeByteBuffer = {
    +    // LargeByteBuffer is supposed to make a "best effort" to get all the 
data
    +    // in one nio.ByteBuffer, so we want to try to merge the smaller 
chunks together
    +    // as much as possible.  This is necessary b/c there are a number of 
parts of spark that
    +    // can only deal w/ one nio.ByteBuffer, and can't use a 
LargeByteBuffer yet.
    +    val totalSize = output.size
    +    val chunksNeeded = ((totalSize + maxChunk -1) / maxChunk).toInt
    +    val chunks = new Array[Array[Byte]](chunksNeeded)
    +    var remaining = totalSize
    +    var pos = 0
    +    (0 until chunksNeeded).foreach{idx =>
    --- End diff --
    
    nit: spaces around `{`


---
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