TooTallNate/Java-WebSocket · error · InvalidDataException

1007

1007

Error message

Received text is no valid utf8 string!

What it means

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.

Solutions

  1. Fix the sender to encode text frames as UTF-8 only; send binary data with opcode BINARY
  2. Validate/normalize text on the client before sending (e.g. new String(bytes, StandardCharsets.UTF_8) round-trip)
  3. Check for proxies or gateways that corrupt payload bytes
  4. Handle the 1007 close on the client side by treating it as a protocol error and logging the offending payload

Example fix

// before (client)
byte[] payload = text.getBytes(Charset.forName("ISO-8859-1"));
// after
byte[] payload = text.getBytes(StandardCharsets.UTF_8);
Defensive patterns

Strategy: validation

Validate before calling

Charsetfunctions.isValidUTF8(payloadBytes); // returns false for invalid UTF-8
// or: StandardCharsets.UTF_8.newDecoder() with REPORT to detect malformed input

Try / catch

try {
  client.send(text);
} catch (WebsocketNotConnectedException | IllegalArgumentException e) {
  // encode check failed or connection gone; re-encode as UTF-8 and retry once
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of TooTallNate/Java-WebSocket@afeacbf8c0 (2026-09-09). Data as JSON: /api/errors/a0e44618a514d320. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/org/java_websocket/framing/TextFrame.java:48

import org.java_websocket.util.Charsetfunctions;

/**
 * Class to represent a text frames
 */
public class TextFrame extends DataFrame {

  /**
   * constructor which sets the opcode of this frame to text
   */
  public TextFrame() {
    super(Opcode.TEXT);
  }

  @Override
  public void isValid() throws InvalidDataException {
    super.isValid();
    if (!Charsetfunctions.isValidUTF8(getPayloadData())) {
      throw new InvalidDataException(CloseFrame.NO_UTF8, "Received text is no valid utf8 string!");
    }
  }
}

View on GitHub (pinned to afeacbf8c0)