Protobuf error:Protocol message tag had invalid wi

2020-06-12 04:15发布

问题:

I am having the following error when trying to read the message in 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())


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_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

Thanks

回答1:

I very much doubt that you're getting the exception there - I'd expect you to get it in 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? You don't use parseFrom like this to parse an ASCII representation of a protobuf message.

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();


回答2:

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).

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.