{"record":{"id":"2439ae7a290d7003","repo":"apache/flink","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-core/src/main/java/org/apache/flink/core/memory/DataOutputSerializer.java","lineNumber":245,"sourceCode":"        }\n        this.buffer[this.position++] = (byte) ((v >>> 8) & 0xff);\n        this.buffer[this.position++] = (byte) (v & 0xff);\n    }\n\n    @Override\n    public void writeUTF(String str) throws IOException {\n        int strlen = str.length();\n        int utflen = 0;\n        int c;\n\n        /* use charAt instead of copying String to char array */\n        for (int i = 0; i < strlen; i++) {\n            c = str.charAt(i);\n            utflen += getUTFBytesSize(c);\n        }\n\n        if (utflen > 65535) {\n            throw new UTFDataFormatException(\"Encoded string is too long: \" + utflen);\n        } else if (this.position > this.buffer.length - utflen - 2) {\n            resize(utflen + 2);\n        }\n\n        byte[] bytearr = this.buffer;\n\n        bytearr[this.position++] = (byte) ((utflen >>> 8) & 0xFF);\n        bytearr[this.position++] = (byte) (utflen & 0xFF);\n\n        writeUTFBytes(str);\n    }\n\n    /**\n     * Similar to {@link #writeUTF(String)}. The size is only limited by the maximum java array size\n     * of the buffer.\n     *\n     * @param str the string value to be written.\n     * @throws IOException if an I/O error occurs.","sourceCodeStart":227,"sourceCodeEnd":263,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/core/memory/DataOutputSerializer.java#L227-L263","documentation":"Thrown by DataOutputSerializer.writeUTF(String) when the modified-UTF-8 encoded byte length of the string exceeds 65535. writeUTF prefixes the string with its byte length as an unsigned 16-bit value, so the encoded form is capped at 64KB. This mirrors java.io.DataOutput.writeUTF limits.","triggerScenarios":"Calling writeUTF(str) where the encoded UTF-8 byte length > 65535. ASCII strings > 65535 chars trigger this directly; strings with multi-byte chars trigger at correspondingly fewer characters.","commonSituations":"Serializing large text fields, JSON/XML blobs, stack traces, or long log messages with writeUTF; user-generated content that exceeds 64KB encoded.","solutions":["Switch to writeLongUTF/readLongUTF, which uses a 4-byte (int) length prefix supporting up to ~2GB.","Pre-check the encoded length and reject or chunk oversized strings before writeUTF.","For unbounded text, use a byte-array based serializer with an int length prefix instead of writeUTF."],"exampleFix":"// before\nout.writeUTF(jsonBlob);\n\n// after\nout.writeLongUTF(jsonBlob); // paired with input.readLongUTF()","handlingStrategy":"validation","validationCode":"static int modifiedUtf8Len(String s) {\n    int n = 0;\n    for (int i = 0; i < s.length(); i++) {\n        int c = s.charAt(i);\n        n += (c >= 0x0001 && c <= 0x007F) ? 1 : (c > 0x07FF ? 3 : 2);\n    }\n    return n;\n}\n// if (modifiedUtf8Len(str) > 65535) use writeLongUTF instead","typeGuard":null,"tryCatchPattern":"try {\n    out.writeUTF(str);\n} catch (UTFDataFormatException e) {\n    out.writeLongUTF(str); // fallback for strings over 64KB\n}","preventionTips":["Use writeLongUTF/readLongUTF for any string that may exceed 64KB encoded.","Enforce a maximum string length at the API boundary.","Pre-check encoded length for user-supplied text before writeUTF."],"tags":["serialization","utf-8","string","size-limit"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}