简体   繁体   English

Protobuf 错误:协议消息标签的线路类型无效

[英]Protobuf error:Protocol message tag had invalid wire type

I am having the following error when trying to read the message in java尝试阅读 java 中的消息时出现以下错误

Exception in thread "main" com.google.protobuf.InvalidProtocolBufferException: Protocol message tag had invalid wire type.
    at com.google.protobuf.InvalidProtocolBufferException.invalidWireType(InvalidProtocolBufferException.java:78)
    at com.google.protobuf.UnknownFieldSet$Builder.mergeFieldFrom(UnknownFieldSet.java:498)
    at com.google.protobuf.GeneratedMessage$Builder.parseUnknownField(GeneratedMessage.java:438)

FileInputStream fis = new FileInputStream("F:/Newfolder/sample_message.txt");
Nt nlc = Nt.parseFrom(fis);

if(nlc.hasMessageId())
{
    System.out.println("MessageId: "+nta2sse.getMessageId());
}

I am getting exception at if(nlc.hasMessageId())我在if(nlc.hasMessageId())遇到异常


Here is full stack trace.这是完整的堆栈跟踪。

Exception in thread "main" com.google.protobuf.InvalidProtocolBufferException: Protocol message tag had invalid wire type.
    at com.google.protobuf.InvalidProtocolBufferException.invalidWireType(InvalidProtocolBufferException.java:78)
    at com.google.protobuf.UnknownFieldSet$Builder.mergeFieldFrom(UnknownFieldSet.java:498)
    at com.google.protobuf.GeneratedMessage$Builder.parseUnknownField(GeneratedMessage.java:438)
    at com.soeasy.aanta.nta.sse.NtaSse$Nta2Sse$Builder.mergeFrom(NtaSse.java:523)
    at com.soeasy.aanta.nta.sse.NtaSse$Nta2Sse$Builder.mergeFrom(NtaSse.java:1)
    at com.google.protobuf.AbstractMessage$Builder.mergeFrom(AbstractMessage.java:1)
    at com.google.protobuf.AbstractMessageLite$Builder.mergeFrom(AbstractMessageLite.java:212)
    at com.google.protobuf.AbstractMessage$Builder.mergeFrom(AbstractMessage.java:746)
    at com.google.protobuf.AbstractMessage$Builder.mergeFrom(AbstractMessage.java:1)
    at com.google.protobuf.AbstractMessageLite$Builder.mergeDelimitedFrom(AbstractMessageLite.java:282)
    at com.google.protobuf.AbstractMessage$Builder.mergeDelimitedFrom(AbstractMessage.java:760)
    at com.google.protobuf.AbstractMessageLite$Builder.mergeDelimitedFrom(AbstractMessageLite.java:288)
    at com.google.protobuf.AbstractMessage$Builder.mergeDelimitedFrom(AbstractMessage.java:752)
    at com.soeasy.aanta.nta.sse.NtaSse$Nta2Sse.parseDelimitedFrom(NtaSse.java:338)
    at com.soeasy.aanta.nta.sse.NtaSseServer.main(NtaSseServer.java:60)

and the sample _message.txt has the following:并且示例 _message.txt 具有以下内容:

message_id: 1
batch_meas_update {
  device_update {
    unique_device_id {
      device_type: ME
      device_id: 161
    }
    meas_update {
      override_status: OVERRIDE_INACTIVE
      bad_data_status: GOOD_DATA
      scada_status: SCADA_ACTIVE
      weight: 1.0
      value: 406.596
    }
  }
}

It is in accordance with.proto file是按照.proto文件

Thanks谢谢

I very much doubt that you're getting the exception there - I'd expect you to get it in parseFrom .我非常怀疑你在那里得到了异常 - 我希望你能在parseFrom中得到它。 Could you post the full stack trace instead of just the first three lines?你能发布完整的堆栈跟踪而不是前三行吗?

I strongly suspect you've basically got a broken file.我强烈怀疑你基本上有一个损坏的文件。 The fact that you've given a .txt extension for what should be a binary file is somewhat suspect... what does the file actually look like?您为应该是二进制文件的文件提供了.txt扩展名这一事实有点令人怀疑……该文件实际上是什么样的? You don't use parseFrom like this to parse an ASCII representation of a protobuf message.您不会像这样使用parseFrom来解析 protobuf 消息的 ASCII 表示。

EDIT: As per the question linked in the comment, you're trying to parse a text file using a method designed for binary data.编辑:根据评论中链接的问题,您正在尝试使用为二进制数据设计的方法解析文本文件。

You want to use something like:你想使用类似的东西:

// Use the normal try/finally for closing reliably
InputStreamReader reader = new InputStreamReader(fis, "ASCII");

Nt.Builder builder = Nt.newBuilder();
TextFormat.merge(reader, builder);
Nt nt = builder.build();

When I see users report this type of message, it almost always means they've corrupted the file.当我看到用户报告此类消息时,几乎总是意味着他们已经损坏了文件。 Starting from a .txt is a worrying sign, as protocol buffers is a binary format that cannot be represented in a text encoding (unless you count base-64 etc)..txt开始是一个令人担忧的迹象,因为协议缓冲区是一种无法以文本编码表示的二进制格式(除非您计算 base-64 等)。

Another common cause of this is over-writing a file with less data and not trimming the excess.造成这种情况的另一个常见原因是用较少的数据覆盖文件而不修剪多余的文件。 Since protocol buffers includes (for the root message) neither a length prefix nor a terminator, any excess data (essentially garbage now) from previous file contents will be processed.由于协议缓冲区(对于根消息)既不包括长度前缀也不包括终止符,因此将处理来自先前文件内容的任何多余数据(现在基本上是垃圾)。 This is a bad thing;这是一件坏事; you must always trim your outputs when over-writing.覆盖时必须始终修剪输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM