I would recommend using the writeDelimitedTo(OutputStream)
and parseDelimitedFrom(InputStream)
methods on Message
objects. writeDelimitedTo
writes the length of the message before the message itself; parseDelimitedFrom
then uses that length to read only one message and no farther. This allows multiple messages to be written to a single OutputStream
to then be parsed separately. For more information, see https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/MessageLite#writeDelimitedTo(java.io.OutputStream)