lidavidm commented on code in PR #38266: URL: https://github.com/apache/arrow/pull/38266#discussion_r1367460050
########## java/vector/src/main/java/org/apache/arrow/vector/util/ReusableByteArray.java: ########## @@ -0,0 +1,130 @@ +/* + * 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 java.util.Base64; + +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 long getLength() { + return length; + } + + @Override + public byte[] getBuffer() { + return bytes; + } + + @Override + public void set(ArrowBuf srcBytes, long start, long len) { + setCapacity((int) len, false); + srcBytes.getBytes(start, bytes, 0, (int) len); + length = (int) len; + } + + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } else if (o == null) { + return false; + } + if (!(o instanceof ReusableByteArray)) { + return false; + } + + final ReusableByteArray that = (ReusableByteArray) o; + if (this.getLength() != that.getLength()) { + return false; + } + + // copied from Arrays.equals so we don'thave to copy the byte arrays + for (int i = 0; i < length; i++) { + if (bytes[i] != that.bytes[i]) { + return false; + } + } + + return true; + } + + /** + * Copied from Arrays.hashCode so we don't have to copy the byte array. + * + * @return hashCode + */ + @Override + public int hashCode() { + if (bytes == null) { + return 0; + } + + int result = 1; + for (int i = 0; i < length; i++) { + result = 31 * result + bytes[i]; + } + + return result; + } + + @Override + public String toString() { Review Comment: Can we add some simple unit tests of the new methods? ########## java/vector/src/main/java/org/apache/arrow/vector/util/ReusableByteArray.java: ########## @@ -0,0 +1,130 @@ +/* + * 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 java.util.Base64; + +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 long getLength() { + return length; + } + + @Override + public byte[] getBuffer() { + return bytes; + } + + @Override + public void set(ArrowBuf srcBytes, long start, long len) { + setCapacity((int) len, false); + srcBytes.getBytes(start, bytes, 0, (int) len); + length = (int) len; + } + + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } else if (o == null) { + return false; + } + if (!(o instanceof ReusableByteArray)) { + return false; + } + + final ReusableByteArray that = (ReusableByteArray) o; + if (this.getLength() != that.getLength()) { + return false; + } + + // copied from Arrays.equals so we don'thave to copy the byte arrays Review Comment: OpenJDK is GPL, so we need to rewrite the code (though, yes, this is probably too trivial/can't be written many other ways) That said, it doesn't look like it's directly copied? At least looking at the jdk11 source -- 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]
