jduo commented on code in PR #38266:
URL: https://github.com/apache/arrow/pull/38266#discussion_r1367342108


##########
java/vector/src/main/java/org/apache/arrow/vector/util/ReusableByteArray.java:
##########
@@ -0,0 +1,80 @@
+/*
+ * 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.arrow.vector.util;
+
+import java.util.Arrays;
+
+import org.apache.arrow.memory.ArrowBuf;
+import org.apache.arrow.memory.ReusableBuffer;
+
+/**
+ * A wrapper around byte arrays for repeated writing.
+ */
+public class ReusableByteArray implements ReusableBuffer<byte[]> {
+
+  protected static final byte[] EMPTY_BYTES = new byte[0];
+
+  protected byte[] bytes;
+  protected int length;
+
+  public ReusableByteArray() {
+    bytes = EMPTY_BYTES;
+  }
+
+  /**
+   * Get the number of bytes in the byte array.
+   *
+   * @return the number of bytes in the byte array
+   */
+  @Override
+  public int getLength() {
+    return length;
+  }
+
+  @Override
+  public byte[] getBuffer() {
+    return bytes;
+  }
+
+  @Override
+  public void set(ArrowBuf srcBytes, long start, int len) {
+    setCapacity(len, false);
+    srcBytes.getBytes(start, bytes, 0, len);
+    length = len;
+  }
+
+  /**
+   * Sets the capacity of this object to <em>at least</em> <code>len</code> 
bytes. If the
+   * current buffer is longer, then the capacity and existing content of the 
buffer are unchanged.
+   * If <code>len</code> is larger than the current capacity, the Text 
object's capacity is
+   * increased to match.
+   *
+   * @param len      the number of bytes we need
+   * @param keepData should the old data be kept
+   */
+  protected void setCapacity(int len, boolean keepData) {
+    if (bytes == null || bytes.length < len) {
+      if (bytes != null && keepData) {
+        bytes = Arrays.copyOf(bytes, Math.max(len, length << 1));
+      } else {
+        bytes = new byte[len];
+      }
+    }
+  }
+

Review Comment:
   Text overrides equals(), hashCode(), and toString(). Its equals() and 
hashCode() method are only on the valid section of the byte array. toString() 
wouldn't make sense to port to ReusableByteArray() since it is a higher-level 
interpretation of the data than just raw bytes (it encodes assuming it's UTF-8 
byte content).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to