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

    https://github.com/apache/spark/pull/397#discussion_r11567459
  
    --- Diff: 
core/src/main/scala/org/apache/spark/util/io/FastByteArrayOutputStream.scala ---
    @@ -0,0 +1,113 @@
    +/*
    + * 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.io
    +
    +import java.io.OutputStream
    +import java.nio.ByteBuffer
    +
    +/**
    + * A simple, fast byte-array output stream that exposes the backing array,
    + * inspired by fastutil's FastByteArrayOutputStream.
    + *
    + * [[java.io.ByteArrayOutputStream]] is nice, but to get its content you
    + * must generate each time a new object. This doesn't happen here.
    + *
    + * This class will automatically enlarge the backing array, doubling its
    + * size whenever new space is needed.
    + */
    +private[spark] class FastByteArrayOutputStream(initialCapacity: Int = 16) 
extends OutputStream {
    +
    +  private[this] var _array = new Array[Byte](initialCapacity)
    +
    +  /** The current writing position. */
    +  private[this] var _position: Int = 0
    +
    +  /** The number of valid bytes in array. */
    +  def length: Int = _position
    +
    +  override def write(b: Int): Unit = {
    +    if (_position >= _array.length ) {
    +      _array = FastByteArrayOutputStream.growArray(_array, _position + 1, 
_position)
    +    }
    +    _array(_position) = b.toByte
    +    _position += 1
    +  }
    +
    +  override def write(b: Array[Byte], off: Int, len: Int) {
    +    if (off < 0) {
    +      throw new ArrayIndexOutOfBoundsException(s"Offset ($off) is 
negative" )
    +    }
    +    if (len < 0) {
    +      throw new IllegalArgumentException(s"Length ($len) is negative" )
    +    }
    +    if (off + len > b.length) {
    +      throw new ArrayIndexOutOfBoundsException(
    +        s"Last index (${off + len}) is greater than array length 
(${b.length})")
    +    }
    +    if ( _position + len > _array.length ) {
    +      _array = FastByteArrayOutputStream.growArray(_array, _position + 
len, _position)
    +    }
    +    System.arraycopy(b, off, _array, _position, len)
    +    _position += len
    +  }
    +
    +  /** Return a ByteBuffer wrapping around the filled content of the 
underlying array. */
    +  def toByteBuffer: ByteBuffer = {
    +    ByteBuffer.wrap(_array, 0, _position)
    +  }
    +
    +  /**
    +   * Return a tuple, where the first element is the underlying array, and 
the second element
    +   * is the length of the filled content.
    +   */
    +  def toArray: (Array[Byte], Int) = (_array, _position)
    +
    +  /** Ensures that the length of the backing array is equal to [[length]]. 
*/
    +  def trim(): this.type = {
    --- End diff --
    
    We probably don't need this method, as it eliminates the whole purpose of 
this stream. You might as well use ByteArrayOutputStream if you need trim().


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

Reply via email to