Bilal Shafqat
Bilal Shafqat

Reputation: 821

How to write google protobuf messages in java?

I am using google protobuf library to write messages in a file. However these messages do not look binary messages to me. The proto I am using is simple it has Name, Email and Cell No. The file I get is:

*
bilalÈÇ[email protected]"
12345678

If I read back I am able to read it, but this dont look binary data, what i want is writing binary to a file and then parsing it back while reading. I have gone through the API and found that we have multiple methods available for parseFrom(), like:

1) parseFrom(InputStream)
2) parseFrom(CodedInputStream)
3) parseFrom(Byte[])

In the google protobuf tutorials they are using parseFrom(InputStream). And for binary messages i think I need parseFrom(Byte[]). But I dont know how i have to write binary messages. Help is appreciated. My end goal is to read binary messages in a spark dataframe using scalapb.

Upvotes: 0

Views: 1346

Answers (2)

Antal Attila
Antal Attila

Reputation: 618

You are talking about writing messages, so that means you have to use the command {protoObj}.writeDelimitedTo({an outputstream}) (please pay attention to the delimited nature) and read them with {yourprotodefclass}.parseDelimitedFrom({an inputstream})

Upvotes: 0

rici
rici

Reputation: 241671

Character strings are not altered by the protobuf encoding. So if your protobuf includes an email address as a string field, you'll see the email address in the protobuf. And the same for telephone numbers, because they are generally stored as strings, not integers. (Integer fields won't be easily readable.)

"Binary format" doesn't mean the data is encrypted or obscured. It just means that the encoding might include some bytes which are not valid character codes (like 0). Protobuf, for example, sends the length of a string as a binary integer before it sends the string. That length is not human-readable. The ÈÇ you see is the result of interpreting as text the binary data (not just the length) associated with the email address.

Upvotes: 1

Related Questions