YunaiV/ruoyi-vue-pro · error · IllegalArgumentException
不支持的长度字段长度: {}
Error message
不支持的长度字段长度: {} What it means
readLength() decodes the length field using Vert.x Buffer accessors and supports only 1-byte (unsigned), 2-byte (unsigned short), and 4-byte (signed int) widths. Any other lengthFieldLength (0, 3, 5, 6, 8...) throws IllegalArgumentException at decode time. The constructor only Asserts the config value is non-null, not that it is in {1,2,4}, so a bad value survives until the first frame arrives.
Source
Thrown at yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/tcp/codec/length/IotTcpLengthFieldFrameCodec.java:140
// 写入消息体
buffer.appendBytes(data);
return buffer;
}
/**
* 从 Buffer 中读取长度字段
*/
@SuppressWarnings("EnhancedSwitchMigration")
private int readLength(Buffer buffer, int offset, int length) {
switch (length) {
case 1:
return buffer.getUnsignedByte(offset);
case 2:
return buffer.getUnsignedShort(offset);
case 4:
return buffer.getInt(offset);
default:
throw new IllegalArgumentException("不支持的长度字段长度: " + length);
}
}
/**
* 向 Buffer 中写入长度字段
*/
private void writeLength(Buffer buffer, int length, int fieldLength) {
switch (fieldLength) {
case 1:
buffer.appendByte((byte) length);
break;
case 2:
buffer.appendShort((short) length);
break;
case 4:
buffer.appendInt(length);
break;
default:View on GitHub (pinned to 0418084e22)
Solutions
- Set lengthFieldLength to 1, 2, or 4 (whichever matches the device's length-field byte width).
- If the device truly uses a 3-byte (24-bit) or other width, pre/post-process the stream or extend readLength() with a custom case.
- Add constructor validation rejecting lengthFieldLength not in {1,2,4} so misconfiguration fails fast at startup, not on first frame.
Example fix
// before: constructor only checks non-null
Assert.notNull(config.getLengthFieldLength(), "lengthFieldLength 不能为空");
// after: also validate the allowed set
int lfl = config.getLengthFieldLength();
Assert.isTrue(lfl == 1 || lfl == 2 || lfl == 4,
"lengthFieldLength 必须是 1、2 或 4"); Defensive patterns
Strategy: validation
Validate before calling
// validate lengthFieldLength at construction so misconfiguration fails at startup
int lfl = config.getLengthFieldLength();
if (lfl == null || (lfl != 1 && lfl != 2 && lfl != 4)) {
throw new IllegalArgumentException("lengthFieldLength 必须是 1、2 或 4, 当前: " + lfl);
} Type guard
boolean isSupportedLengthFieldLength(Integer lfl) {
return lfl != null && (lfl == 1 || lfl == 2 || lfl == 4);
} Prevention
- Add constructor range validation for lengthFieldLength (and the other codec params).
- Set lengthFieldLength from the device datasheet's length-field byte width (1, 2, or 4).
- If the device uses a non-standard width, preprocess the stream or extend readLength() with a custom case.
When it happens
Trigger: config.lengthFieldLength set to a value other than 1, 2, or 4 (e.g. 3 for a 24-bit length field, 0, or a typo); the first inbound frame triggers readLength and fails.
Common situations: Device uses a non-standard 3-byte length field; config typo (e.g. 24 entered instead of being unsupported); misunderstanding that the codec only supports 1/2/4.
Related errors
- [createDecodeParser][帧长度异常,length: %d, frameBodyLength: %d]
- [createDecodeParser][消息体不能为空]
- [createDecodeParser][解析异常]
- [createDecodeParser][解析异常]
- [createDecodeParser][解析异常]
AI-assisted analysis of YunaiV/ruoyi-vue-pro@0418084e22 (2026-08-14).
Data as JSON: /api/errors/38c5f681310f0912.
Report an issue: GitHub.