{"record":{"id":"a83747744db79250","repo":"apache/iceberg","slug":"encoded-string-is-too-long-a83747","errorCode":null,"errorMessage":"Encoded string is too long: ","messagePattern":"Encoded string is too long: ","errorType":"exception","errorClass":"UTFDataFormatException","httpStatus":null,"severity":"error","filePath":"flink/v2.2/flink/src/main/java/org/apache/iceberg/flink/util/SerializerHelper.java","lineNumber":70,"sourceCode":"   * @param str the string value to be written.\n   */\n  public static void writeLongUTF(DataOutputView out, String str) throws IOException {\n    int strlen = str.length();\n    long utflen = 0;\n    int ch;\n\n    /* use charAt instead of copying String to char array */\n    for (int i = 0; i < strlen; i++) {\n      ch = str.charAt(i);\n      utflen += getUTFBytesSize(ch);\n\n      if (utflen > Integer.MAX_VALUE) {\n        throw new UTFDataFormatException(\"Encoded string reached maximum length: \" + utflen);\n      }\n    }\n\n    if (utflen > Integer.MAX_VALUE - 4) {\n      throw new UTFDataFormatException(\"Encoded string is too long: \" + utflen);\n    }\n\n    out.writeInt((int) utflen);\n    writeUTFBytes(out, str, (int) utflen);\n  }\n\n  /**\n   * Similar to {@link DataInputDeserializer#readUTF()}. Except this supports larger payloads which\n   * is up to max integer value.\n   *\n   * <p>Note: This method can be removed when the method which does similar thing within the {@link\n   * DataOutputSerializer} already which does the same thing, so use that one instead once that is\n   * released on Flink version 1.20.\n   *\n   * <p>See * <a href=\"https://issues.apache.org/jira/browse/FLINK-34228\">FLINK-34228</a> * <a\n   * href=\"https://github.com/apache/flink/pull/24191\">https://github.com/apache/flink/pull/24191</a>\n   *\n   * @param in the input stream to read the string from.","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/flink/v2.2/flink/src/main/java/org/apache/iceberg/flink/util/SerializerHelper.java#L52-L88","documentation":"After computing total encoded length, writeLongUTF requires room for a 4-byte int length header: if utflen exceeds Integer.MAX_VALUE - 4 it throws UTFDataFormatException('Encoded string is too long'). It is the final, stricter guard before writing the int-prefixed modified-UTF payload.","triggerScenarios":"Writing a string whose UTF-8 size is between Integer.MAX_VALUE-3 and Integer.MAX_VALUE bytes via SerializerHelper.writeLongUTF.","commonSituations":"Same as the maximum-length guard: oversized single-string payloads in serializer pipelines; edge-case sizes just under 2GB that still fail because of the length header.","solutions":["Reduce the string size below Integer.MAX_VALUE - 4 encoded bytes","Chunk or compress the payload before writing","Persist large values via external storage and write a reference"],"exampleFix":"// before\nhelper.writeLongUTF(out, str); // may exceed Integer.MAX_VALUE - 4\n// after\nint size = SerializerHelper.getUTFBytesSize(str); // or compute helper-style\nif (size > Integer.MAX_VALUE - 4) {\n  throw new IllegalArgumentException(\"String too large for longUTF; chunk or compress\");\n}\nhelper.writeLongUTF(out, str);","handlingStrategy":"validation","validationCode":"int encoded = str.getBytes(StandardCharsets.UTF_8).length; // approximate\nif (encoded >= Integer.MAX_VALUE - 4) throw new IllegalArgumentException(\"String too long for writeLongUTF\");","typeGuard":"static boolean fitsLongUTF(String s) {\n  return s == null || s.getBytes(StandardCharsets.UTF_8).length < Integer.MAX_VALUE - 4;\n}","tryCatchPattern":"try { helper.writeLongUTF(out, str); }\ncatch (UTFDataFormatException e) {\n  if (e.getMessage().contains(\"too long\")) { compressAndWrite(out, str); }\n  else throw e;\n}","preventionTips":["Reserve headroom for the 4-byte length header: stay below Integer.MAX_VALUE - 4","Compress giant strings before serialization","Monitor record sizes at producers to catch growth before it hits the limit"],"tags":["flink","serialization","utf","string-size-limit"],"backgroundTag":"payload-too-large","analyzedSha":"86d9c8fc543e7c56c9f624eb725f76c9baff9570","analyzedAt":"2026-09-12T00:46:39.097Z","contentChangedAt":"2026-09-12T00:46:39.097Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}