I am having the following error when trying to read the message in java
Exception in thread \"main\" com.google.protobuf.InvalidProtocolBufferException: Prot
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();
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.