If you're only interested in writing the messages out, you could go really low level and use CodedOutputStream.writeBytes
. It's not how I normally prefer to use protocol buffers, but that's a different matter.
Fundamentally, the problem is that byte[]
is mutable, and protobuf messages in Java are meant to be immutable... which means you have to take a copy, basically. Even though the builders themselves are mutable, bytes
and string
fields are seen as atomic, effectively. (So you can change a builder to have a different byte string or string value for a field, but not mutate the byte string or string itself.)
You should absolutely not be using a string
field for data that isn't text though. You should use a bytes
field for arbitrary binary data. A string
field is expected to contain UTF-8-encoded text.