apache/flink · error · UTFDataFormatException

malformed input around byte {count-1}

Error message

malformed input around byte {count-1}

What it means

Thrown while decoding a three-byte modified-UTF-8 character (case 14). The lead byte was valid (1110xxxx) but at least one of the two continuation bytes (char2, char3) lacks the required 10xxxxxx pattern. The byte index reported is 'around byte {count-1}'.

Source

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

                                "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));
                    break;
                default:
                    /* 10xx xxxx, 1111 xxxx */
                    throw new UTFDataFormatException("malformed input around byte " + count);
            }
        }
        // The number of chars produced may be less than utflen
        return new String(chararr, 0, chararrCount);
    }

    @Override
    public int readUnsignedByte() throws IOException {
        if (this.position < this.end) {
            return (this.buffer[this.position++] & 0xff);

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Inspect raw bytes at the reported offset to distinguish corruption from encoding mismatch.
  2. Ensure matched writeUTF/readUTF encoding on both sides.
  3. For externally sourced bytes, decode with java.nio.charset.StandardCharsets.UTF_8 instead of readUTF.
Defensive patterns

Strategy: try-catch

Try / catch

try {
    String s = input.readUTF();
} catch (UTFDataFormatException e) {
    // a 3-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 where a 3-byte sequence's continuation byte(s) are malformed (not in 0x80-0xBF).

Common situations: Byte corruption in multi-byte (non-ASCII) string regions; encoding mismatch (standard UTF-8 vs modified UTF-8); buffer overwritten; serializer version skew affecting string state.

Understand the failure class

Related errors


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