YunaiV/ruoyi-vue-pro · error · IllegalArgumentException

数据长度 %d 超过固定长度 %d

Error message

数据长度 %d 超过固定长度 %d

What it means

encode() throws IllegalArgumentException when the outbound data byte length exceeds the configured fixedLength. The fixed-length codec zero-pads shorter payloads but cannot truncate longer ones, so an oversized payload is a hard error. This is an encode-side (downstream/command) failure, not a decode failure.

Source

Thrown at yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/tcp/codec/length/IotTcpFixedLengthFrameCodec.java:51

    public IotTcpCodecTypeEnum getType() {
        return IotTcpCodecTypeEnum.FIXED_LENGTH;
    }

    @Override
    public RecordParser createDecodeParser(Handler<Buffer> handler) {
        RecordParser parser = RecordParser.newFixed(fixedLength);
        parser.handler(handler);
        parser.exceptionHandler(ex -> {
            throw new RuntimeException("[createDecodeParser][解析异常]", ex);
        });
        return parser;
    }

    @Override
    public Buffer encode(byte[] data) {
        // 校验数据长度不能超过固定长度
        if (data.length > fixedLength) {
            throw new IllegalArgumentException(String.format(
                    "数据长度 %d 超过固定长度 %d", data.length, fixedLength));
        }
        Buffer buffer = Buffer.buffer(fixedLength);
        buffer.appendBytes(data);
        // 如果数据不足固定长度,填充 0(RecordParser.newFixed 解码时按固定长度读取,所以发送端需要填充)
        if (data.length < fixedLength) {
            byte[] padding = new byte[fixedLength - data.length];
            buffer.appendBytes(padding);
        }
        return buffer;
    }

}

View on GitHub (pinned to 0418084e22)

Solutions

  1. Increase fixedLength to fit the largest legitimate payload (and align with the device's expected frame size).
  2. If payloads vary, switch from FIXED_LENGTH to LENGTH_FIELD or DELIMITER codec which handle variable sizes.
  3. Validate data.length <= fixedLength before calling encode() and fail the command with a clear business error.

Example fix

// before: encode throws when payload exceeds fixedLength
byte[] cmd = buildLargeCommand(); // longer than fixedLength
Buffer out = codec.encode(cmd);

// after: validate up front and use a variable-length codec
if (cmd.length > fixedLength) {
    throw new IllegalArgumentException("command too large for fixed frame");
}
Defensive patterns

Strategy: validation

Validate before calling

// before encoding, validate payload fits the fixed frame
if (data.length > fixedLength) {
    throw new IllegalArgumentException(
        String.format("数据长度 %d 超过固定长度 %d", data.length, fixedLength));
}
// or, at config time, ensure fixedLength >= max command payload size

Type guard

boolean fitsFixedFrame(byte[] data, int fixedLength) {
    return data != null && data.length <= fixedLength;
}

Prevention

When it happens

Trigger: Calling encode(byte[]) with a payload longer than fixedLength; a downstream command whose serialized body grew beyond the agreed frame size; fixedLength misconfigured smaller than the protocol's largest message.

Common situations: Command payload size increased after a firmware/protocol change; fixedLength set to a value too small; developer assumed encode would truncate.

Related errors


AI-assisted analysis of YunaiV/ruoyi-vue-pro@0418084e22 (2026-08-14). Data as JSON: /api/errors/9b62708f23de61d5. Report an issue: GitHub.