{"record":{"id":"a0e44618a514d320","repo":"TooTallNate/Java-WebSocket","slug":"1007-a0e446","errorCode":"1007","errorMessage":"Received text is no valid utf8 string!","messagePattern":"Received text is no valid utf8 string!","errorType":"error_code","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/java_websocket/framing/TextFrame.java","lineNumber":48,"sourceCode":"import org.java_websocket.util.Charsetfunctions;\n\n/**\n * Class to represent a text frames\n */\npublic class TextFrame extends DataFrame {\n\n  /**\n   * constructor which sets the opcode of this frame to text\n   */\n  public TextFrame() {\n    super(Opcode.TEXT);\n  }\n\n  @Override\n  public void isValid() throws InvalidDataException {\n    super.isValid();\n    if (!Charsetfunctions.isValidUTF8(getPayloadData())) {\n      throw new InvalidDataException(CloseFrame.NO_UTF8, \"Received text is no valid utf8 string!\");\n    }\n  }\n}\n","sourceCodeStart":30,"sourceCodeEnd":52,"githubUrl":"https://github.com/TooTallNate/Java-WebSocket/blob/afeacbf8c0f6f6a761c9d9daed8c813dd3b8ed7d/src/main/java/org/java_websocket/framing/TextFrame.java#L30-L52","documentation":"TextFrame.isValid() validates that the payload of a text message is well-formed UTF-8. If Charsetfunctions.isValidUTF8() fails, it throws InvalidDataException with close code 1007 (invalid payload data), as required by RFC 6455, and the connection is closed with that code.","triggerScenarios":"A remote peer sends a text frame whose bytes are not valid UTF-8 — e.g. binary data sent as text, truncated multibyte sequences, or a client using the wrong character encoding.","commonSituations":"Clients sending raw binary payloads with opcode TEXT, encoders writing ISO-8859-1 or GBK encoded strings into text frames, or corruption in transport/proxies mangling multibyte characters.","solutions":["Fix the sender to encode text frames as UTF-8 only; send binary data with opcode BINARY","Validate/normalize text on the client before sending (e.g. new String(bytes, StandardCharsets.UTF_8) round-trip)","Check for proxies or gateways that corrupt payload bytes","Handle the 1007 close on the client side by treating it as a protocol error and logging the offending payload"],"exampleFix":"// before (client)\nbyte[] payload = text.getBytes(Charset.forName(\"ISO-8859-1\"));\n// after\nbyte[] payload = text.getBytes(StandardCharsets.UTF_8);","handlingStrategy":"validation","validationCode":"Charsetfunctions.isValidUTF8(payloadBytes); // returns false for invalid UTF-8\n// or: StandardCharsets.UTF_8.newDecoder() with REPORT to detect malformed input","typeGuard":null,"tryCatchPattern":"try {\n  client.send(text);\n} catch (WebsocketNotConnectedException | IllegalArgumentException e) {\n  // encode check failed or connection gone; re-encode as UTF-8 and retry once\n}","preventionTips":["Always encode outgoing text as UTF-8 (StandardCharsets.UTF_8)","Send binary data with send(byte[]) / BINARY opcode, never as text","Round-trip strings through UTF-8 decode before sending when data origin is uncertain","Handle close code 1007 on clients to surface peer-side UTF-8 problems"],"tags":["websocket","utf8","encoding","frame-validation"],"backgroundTag":"invalid-utf8-payload","analyzedSha":"afeacbf8c0f6f6a761c9d9daed8c813dd3b8ed7d","analyzedAt":"2026-09-09T14:39:47.546Z","contentChangedAt":"2026-09-09T14:39:47.546Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}