Protobuf - Missing toByteArray()?

2019-02-23 22:07发布

问题:

I probably just messed up something in my proto file but I can't seem to call the toByteArray function.

Using Java and the proto file was compiled with protoc.

What I'm calling...

BaseMessage.builder = baseMessage = BaseMessage.newBuilder();
Acknowledgement.builder = acknowledgeMessage = Acknowledgement.newBuilder();

acknowledgeMessage.setOk(true);

baseMessage.setAcknowledgement(acknowledgeMessage);
baseMessage.build();

byte[] send = baseMessage.toByteArray();

And BaseMessage is declared as...

public static final class BaseMessage   
       extends com.google.protobuf.GeneratedMessage   
       implements BaseMessageOrBuilder {

Maybe I'm missing somthing...

BaseMessage is extending GeneratedMessage which inherits toByteArray() from AbstractMessageLite in which toByteArray is public... So I should be able to use it?

回答1:

Yes you are, you probably meant:

baseMessage.build().toByteArray()


回答2:

Try this:

baseMessage.build().toByteArray()