Github user aarondav commented on a diff in the pull request:
https://github.com/apache/spark/pull/2054#discussion_r16509304
--- Diff:
core/src/main/scala/org/apache/spark/util/io/ByteArrayChunkOutputStream.scala
---
@@ -0,0 +1,87 @@
+/*
+ * 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 scala.collection.mutable.ArrayBuffer
+
+
+/**
+ * An OutputStream that writes to fixed-size chunks of byte arrays.
+ */
+private[spark]
+class ByteArrayChunkOutputStream(chunkSize: Int) extends OutputStream {
+
+ private val chunks = new ArrayBuffer[Array[Byte]]
+
+ private var lastChunkIndex = -1
+ private var position = chunkSize
+
+ def toArrays: Array[Array[Byte]] = {
+ if (lastChunkIndex == -1) {
+ new Array[Array[Byte]](0)
+ } else {
+ // Copy the first n-1 chunks to the output, and then create an array
that fits the last chunk.
+ // An alternative would have been returning an array of ByteBuffers,
with the last buffer
+ // bounded to only the last chunk's position. However, given our use
case in Spark (to put
+ // the chunks in block manager), only limiting the view bound of the
buffer would still
+ // require the block manager to store the whole chunk.
+ val ret = new Array[Array[Byte]](chunks.size)
+ for (i <- 0 until chunks.size - 1) {
+ ret(i) = chunks(i)
+ }
+ if (position == chunkSize) {
+ ret(lastChunkIndex) = chunks(lastChunkIndex)
+ } else {
+ ret(lastChunkIndex) = new Array[Byte](position)
+ System.arraycopy(chunks(lastChunkIndex), 0, ret(lastChunkIndex),
0, position)
+ }
+ ret
+ }
+ }
+
+ override def write(b: Int): Unit = {
+ allocateNewChunkIfNeeded()
+ chunks(lastChunkIndex)(position) = b.toByte
+ position += 1
+ }
+
+ override def write(bytes: Array[Byte]): Unit = write(bytes, 0,
bytes.length)
+
+ override def write(bytes: Array[Byte], off: Int, len: Int): Unit = {
+ var written = off
--- End diff --
I think written could be 0, total could be len (i.e., deleted), and `total
- written` could be `len - written`. That way all the variables mean what they
say :)
---
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]