{"record":{"id":"181a162bbe3bbedf","repo":"apache/iceberg","slug":"encoded-string-is-too-long-utflen","errorCode":null,"errorMessage":"Encoded string is too long: {utflen}","messagePattern":"Encoded string is too long: (.+?)","errorType":"exception","errorClass":"UTFDataFormatException","httpStatus":null,"severity":"error","filePath":"flink/v2.1/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.1/flink/src/main/java/org/apache/iceberg/flink/util/SerializerHelper.java#L52-L88","documentation":"SerializerHelper.writeLongUTF reserves 4 bytes for the length header, so the encoded length must fit in an int while leaving room for the header. Strings longer than Integer.MAX_VALUE - 4 encoded UTF-8 bytes are rejected with this error, distinguishing it from the hard-overflow check inside the loop.","triggerScenarios":"Calling writeLongUTF with a string whose total encoded length is in the narrow window (Integer.MAX_VALUE - 4, Integer.MAX_VALUE] bytes.","commonSituations":"Serializing strings very close to the 2GB boundary — rare, usually from concatenated or accumulated payloads in Flink state serialization.","solutions":["Keep encoded strings comfortably below Integer.MAX_VALUE - 4 bytes (e.g. cap at 2GB minus headroom)","Chunk the payload before writing","Check length with getUTFBytesSize before calling writeLongUTF"],"exampleFix":"// before\nhelper.writeLongUTF(str);\n// after\nif (SerializerHelper.getUTFBytesSize(str) <= Integer.MAX_VALUE - 4) {\n  helper.writeLongUTF(str);\n} else {\n  throw new IllegalArgumentException(\"string too large\");\n}","handlingStrategy":"validation","validationCode":"if (SerializerHelper.getUTFBytesSize(str) > Integer.MAX_VALUE - 4) {\n  throw new IllegalArgumentException(\"encoded string too large for writeLongUTF\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  helper.writeLongUTF(str);\n} catch (UTFDataFormatException e) {\n  throw new IllegalArgumentException(\"string exceeds 2GB encoder limit\", e);\n}","preventionTips":["Leave headroom below Integer.MAX_VALUE - 4 encoded bytes","Pre-check with getUTFBytesSize for near-limit strings"],"tags":["flink","serialization","utf","limit"],"backgroundTag":"value-out-of-range","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"}