javrasya commented on code in PR #9464:
URL: https://github.com/apache/iceberg/pull/9464#discussion_r1567888070


##########
flink/v1.18/flink/src/main/java/org/apache/iceberg/flink/source/split/SerializerHelper.java:
##########
@@ -0,0 +1,186 @@
+/*
+ * 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.flink.source.split;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.io.UTFDataFormatException;
+import org.apache.flink.core.memory.DataInputDeserializer;
+import org.apache.flink.core.memory.DataOutputSerializer;
+
+/**
+ * Helper class to serialize and deserialize strings longer than 65K. The 
inspiration is mostly
+ * taken from the class 
org.apache.flink.core.memory.DataInputSerializer.readUTF and
+ * org.apache.flink.core.memory.DataOutputSerializer.writeUTF.
+ */
+public class SerializerHelper implements Serializable {
+
+  /**
+   * Similar to {@link DataOutputSerializer#writeUTF(String)}. The size is 
only limited by the
+   * maximum java array size of the buffer.
+   *
+   * @param out the output stream to write the string to.
+   * @param str the string value to be written.
+   * @deprecated This method is deprecated because there will be a method 
within the {@link
+   *     DataOutputSerializer} already which does the same thing, so use that 
one instead once that
+   *     is released on Flink version 1.20.
+   *     <p>See * <a 
href="https://issues.apache.org/jira/browse/FLINK-34228";>FLINK-34228</a> * <a
+   *     
href="https://github.com/apache/flink/pull/24191";>https://github.com/apache/flink/pull/24191</a>
+   */
+  @Deprecated
+  public static void writeLongUTF(DataOutputSerializer out, String str) throws 
IOException {
+    int strlen = str.length();
+    long utflen = 0;
+    int c;
+
+    /* use charAt instead of copying String to char array */
+    for (int i = 0; i < strlen; i++) {
+      c = str.charAt(i);
+      utflen += getUTFBytesSize(c);
+
+      if (utflen > Integer.MAX_VALUE) {
+        throw new UTFDataFormatException("Encoded string reached maximum 
length: " + utflen);
+      }
+    }
+    if (utflen > Integer.MAX_VALUE - 4) {
+      throw new UTFDataFormatException("Encoded string is too long: " + 
utflen);
+    }
+

Review Comment:
   Exactly, as @pvary said, `writeUTFBytes` will resize the the array as needed 
already. The only downside of not resizing it preliminary like in the Flink PR 
could be that there will be two resizing one when writing `out.writeInt((int) 
utflen);` and then `out.write(bytearr, 0, count);`. 
   
   As an improvement, maybe I can combine these two by turning `writeUTFBytes` 
into `writeUTFBytesWithSize` and call only `out.write(bytearr, 0, count + 4)`  
(`4` which is for the size) so that resizing of the actual byte array would be 
done once when needed. Then it will do exactly as in the Flink PR.
   
   Wdyt @pvary @stevenzwu ?



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