{"record":{"id":"9b62708f23de61d5","repo":"YunaiV/ruoyi-vue-pro","slug":"d-d","errorCode":null,"errorMessage":"数据长度 %d 超过固定长度 %d","messagePattern":"数据长度 (.+?) 超过固定长度 (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/tcp/codec/length/IotTcpFixedLengthFrameCodec.java","lineNumber":51,"sourceCode":"    public IotTcpCodecTypeEnum getType() {\n        return IotTcpCodecTypeEnum.FIXED_LENGTH;\n    }\n\n    @Override\n    public RecordParser createDecodeParser(Handler<Buffer> handler) {\n        RecordParser parser = RecordParser.newFixed(fixedLength);\n        parser.handler(handler);\n        parser.exceptionHandler(ex -> {\n            throw new RuntimeException(\"[createDecodeParser][解析异常]\", ex);\n        });\n        return parser;\n    }\n\n    @Override\n    public Buffer encode(byte[] data) {\n        // 校验数据长度不能超过固定长度\n        if (data.length > fixedLength) {\n            throw new IllegalArgumentException(String.format(\n                    \"数据长度 %d 超过固定长度 %d\", data.length, fixedLength));\n        }\n        Buffer buffer = Buffer.buffer(fixedLength);\n        buffer.appendBytes(data);\n        // 如果数据不足固定长度，填充 0（RecordParser.newFixed 解码时按固定长度读取，所以发送端需要填充）\n        if (data.length < fixedLength) {\n            byte[] padding = new byte[fixedLength - data.length];\n            buffer.appendBytes(padding);\n        }\n        return buffer;\n    }\n\n}\n","sourceCodeStart":33,"sourceCodeEnd":65,"githubUrl":"https://github.com/YunaiV/ruoyi-vue-pro/blob/0418084e222612af2fc1141f566af454f9236ab1/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/tcp/codec/length/IotTcpFixedLengthFrameCodec.java#L33-L65","documentation":"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.","triggerScenarios":"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.","commonSituations":"Command payload size increased after a firmware/protocol change; fixedLength set to a value too small; developer assumed encode would truncate.","solutions":["Increase fixedLength to fit the largest legitimate payload (and align with the device's expected frame size).","If payloads vary, switch from FIXED_LENGTH to LENGTH_FIELD or DELIMITER codec which handle variable sizes.","Validate data.length <= fixedLength before calling encode() and fail the command with a clear business error."],"exampleFix":"// before: encode throws when payload exceeds fixedLength\nbyte[] cmd = buildLargeCommand(); // longer than fixedLength\nBuffer out = codec.encode(cmd);\n\n// after: validate up front and use a variable-length codec\nif (cmd.length > fixedLength) {\n    throw new IllegalArgumentException(\"command too large for fixed frame\");\n}","handlingStrategy":"validation","validationCode":"// before encoding, validate payload fits the fixed frame\nif (data.length > fixedLength) {\n    throw new IllegalArgumentException(\n        String.format(\"数据长度 %d 超过固定长度 %d\", data.length, fixedLength));\n}\n// or, at config time, ensure fixedLength >= max command payload size","typeGuard":"boolean fitsFixedFrame(byte[] data, int fixedLength) {\n    return data != null && data.length <= fixedLength;\n}","tryCatchPattern":null,"preventionTips":["Size fixedLength to the largest legitimate command payload.","If payloads vary, prefer LENGTH_FIELD or DELIMITER codec over FIXED_LENGTH.","Validate payload length before encode() and fail with a business-level error."],"tags":["tcp","codec","fixed-length","validation"],"backgroundTag":null,"analyzedSha":"0418084e222612af2fc1141f566af454f9236ab1","analyzedAt":"2026-08-14T00:56:18.412Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}