尝试使用Java中的protobuf反序列化消息并获取以下异常。
原因:com.google.protobuf.InvalidProtocolBufferException:解析协议消息时,输入意外终止于字段中间。这可能意味着输入已被截断,或者嵌入的消息错误地报告了其自身的长度。 com.google.protobuf.InvalidProtocolBufferException.truncatedMessage(InvalidProtocolBufferException.java:86) 在com.google.protobuf.CodedInputStream $ ArrayDecoder.readRawLittleEndian64(CodedInputStream.java:1179) com.google.protobuf.CodedInputStream $ ArrayDecoder.readFixed64(CodedInputStream.java:791) 在com.google.protobuf.UnknownFieldSet $ Builder.mergeFieldFrom(UnknownFieldSet.java:534) 在com.google.protobuf.GeneratedMessageV3.parseUnknownFieldProto3(GeneratedMessageV3.java:305)
答案 0 :(得分:1)
我已经手动解码了您的字符串,并且我同意该库:您的消息被截断了。我猜测这是因为您使用的是基于字符串的API,并且数据中存在零字节-许多文本API看到的是零字节(NUL
ASCII术语)表示字符串的结尾。
以下是细分:
\n=10=field 1, length prefix - I'm assuming this is a string
\x14=20
"id:article:v1:964000"
(22 bytes used for field 1)
\x12=18=field 2, length prefix - I'm assuming this is a sub-messssage
$=36
\n=10=field 1, length prefix - I'm assuming this is a string
\x10=16
"predicted_topics"
(18 bytes used for field 2.1)
\x12=18=field 2, length prefix - I'm assuming this is a string
\x06=6
"IS/biz"
(8 bytes used for field 2.2)
\x1a=26=field 3, length prefix - I'm assuming this is "bytes"
\x08=8
\xf0
l
\x8f
\xde
p
\x9f
\xe4
(unexpected EOF)
最后,我们尝试解码最里面的消息的8个字节,而我们只剩下7个字节。我知道这不是一个子消息,因为这将导致标记无效,并且它看起来不像UTF-8,因此我假设这是一个bytes
字段(但坦率地说,它不会没关系:我们需要8个字节,而我们只有7个字节。
我的猜测是bytes
字段中的最后一个字节为零;如果我们假设末尾缺少\x00
,则字段2.3为10个字节,并且我们已经考虑了18 + 8 + 10 = 36个字节,这将使子消息(字段2)完整。在外部子消息之后 可能还有更多丢失的数据-我无法知道。
因此:请确保您没有对二进制数据使用基于文本的API。