apache/flink · error · UTFDataFormatException

malformed input around byte {count}

Error message

malformed input around byte {count}

What it means

Thrown while decoding a two-byte modified-UTF-8 character (cases 12/13). The lead byte was valid (110xxxxx) but the continuation byte (char2) does not have the required 10xxxxxx pattern (high two bits != 0x80 mask). The offending byte index is reported as 'around byte {count}'.

Source

Thrown at flink-core/src/main/java/org/apache/flink/core/memory/DataInputDeserializer.java:312

                case 4:
                case 5:
                case 6:
                case 7:
                    /* 0xxxxxxx */
                    count++;
                    chararr[chararrCount++] = (char) c;
                    break;
                case 12:
                case 13:
                    /* 110x xxxx 10xx xxxx */
                    count += 2;
                    if (count > utflen) {
                        throw new UTFDataFormatException(
                                "malformed input: partial character at end");
                    }
                    char2 = (int) bytearr[count - 1];
                    if ((char2 & 0xC0) != 0x80) {
                        throw new UTFDataFormatException("malformed input around byte " + count);
                    }
                    chararr[chararrCount++] = (char) (((c & 0x1F) << 6) | (char2 & 0x3F));
                    break;
                case 14:
                    /* 1110 xxxx 10xx xxxx 10xx xxxx */
                    count += 3;
                    if (count > utflen) {
                        throw new UTFDataFormatException(
                                "malformed input: partial character at end");
                    }
                    char2 = (int) bytearr[count - 2];
                    char3 = (int) bytearr[count - 1];
                    if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) {
                        throw new UTFDataFormatException(
                                "malformed input around byte " + (count - 1));
                    }
                    chararr[chararrCount++] =
                            (char) (((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | (char3 & 0x3F));

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Confirm writer and reader use the matched writeUTF/readUTF (or writeLongUTF/readLongUTF) encoding.
  2. Inspect the raw bytes around the reported offset to confirm corruption vs. encoding mismatch.
  3. For externally-produced bytes, decode with standard java.nio.charset UTF-8 instead of readUTF.
Defensive patterns

Strategy: try-catch

Try / catch

try {
    String s = input.readUTF();
} catch (UTFDataFormatException e) {
    // a 2-byte sequence had an invalid continuation byte
    throw new IOException("Invalid modified-UTF-8 continuation: " + e.getMessage(), e);
}

Prevention

When it happens

Trigger: readUTF()/readLongUTF() on data whose second byte of a 2-byte UTF sequence is malformed (e.g. 0xC3 followed by 0x41 instead of 0x80-0xBF).

Common situations: Raw standard UTF-8 mistakenly read where modified UTF-8 was expected (rare for the 2-byte range but possible); byte-level corruption from disk/network; a buffer overwritten by unrelated data; serializer version skew.

Understand the failure class

Related errors


AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14). Data as JSON: /api/errors/68b128902fc1611f. Report an issue: GitHub.