danielcweeks commented on code in PR #11415:
URL: https://github.com/apache/iceberg/pull/11415#discussion_r1890858597


##########
core/src/main/java/org/apache/iceberg/variants/VariantUtil.java:
##########
@@ -0,0 +1,195 @@
+/*
+ * 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.iceberg.variants;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.util.function.Function;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+
+class VariantUtil {
+  private static final int BASIC_TYPE_MASK = 0b11;
+  private static final int BASIC_TYPE_PRIMITIVE = 0;
+  private static final int BASIC_TYPE_SHORT_STRING = 1;
+  private static final int BASIC_TYPE_OBJECT = 2;
+  private static final int BASIC_TYPE_ARRAY = 3;
+
+  private VariantUtil() {}
+
+  /** A hacky absolute put for ByteBuffer */
+  static int writeBufferAbsolute(ByteBuffer buffer, int offset, ByteBuffer 
toCopy) {
+    int originalPosition = buffer.position();
+    buffer.position(offset);
+    ByteBuffer copy = toCopy.duplicate();
+    buffer.put(copy); // duplicate so toCopy is not modified
+    buffer.position(originalPosition);
+    Preconditions.checkArgument(copy.remaining() <= 0, "Not fully written");
+    return toCopy.remaining();
+  }
+
+  static void writeByte(ByteBuffer buffer, int value, int offset) {
+    buffer.put(buffer.position() + offset, (byte) (value & 0xFF));
+  }
+
+  static void writeLittleEndianUnsigned(ByteBuffer buffer, int value, int 
offset, int size) {
+    int base = buffer.position() + offset;
+    switch (size) {
+      case 4:
+        buffer.putInt(base, value);
+        return;
+      case 3:
+        buffer.putShort(base, (short) (value & 0xFFFF));
+        buffer.put(base + 2, (byte) ((value >> 16) & 0xFF));
+        return;
+      case 2:
+        buffer.putShort(base, (short) (value & 0xFFFF));
+        return;
+      case 1:
+        buffer.put(base, (byte) (value & 0xFF));
+        return;
+    }
+
+    throw new IllegalArgumentException("Invalid size: " + size);
+  }
+
+  static byte readLittleEndianInt8(ByteBuffer buffer, int offset) {
+    return buffer.get(buffer.position() + offset);
+  }
+
+  static short readLittleEndianInt16(ByteBuffer buffer, int offset) {
+    return buffer.getShort(buffer.position() + offset);

Review Comment:
   Do we need to validate endianness on the buffer?



-- 
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: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to