Abstract interface implemented by Protocol Message objects.
This interface is implemented by all protocol message objects. Non-lite messages additionally
implement the Message interface, which is a subclass of MessageLite. Use MessageLite instead when
you only need the subset of features which it supports -- namely, nothing that uses descriptors
or reflection. You can instruct the protocol compiler to generate classes which implement only
MessageLite, not the full Message interface, by adding the follow line to the .proto file:
option optimize_for = LITE_RUNTIME;
This is particularly useful on resource-constrained systems where the full protocol buffers
runtime library is too big.
Note that on non-constrained systems (e.g. servers) when you need to link in lots of protocol
definitions, a better way to reduce total code footprint is to use optimize_for =
CODE_SIZE. This will make the generated code smaller while still supporting all the same
features (at the expense of speed). optimize_for = LITE_RUNTIME is best when you only
have a small number of message types linked into your binary, in which case the size of the
protocol buffers runtime itself is the biggest problem.
Get the number of bytes required to encode this message. The result is only computed on the
first call and memoized after that.
If this message requires more than Integer.MAX_VALUE bytes to encode, the return value will
be smaller than the actual number of bytes required and might be negative.
Serializes the message to a byte array and returns it. This is just a trivial wrapper
around #writeTo(CodedOutputStream).
If this message requires more than Integer.MAX_VALUE bytes to encode, the behavior is
unpredictable. It may throw a runtime exception or truncate or slice the data.
Serializes the message to a ByteString and returns it. This is just a trivial wrapper
around #writeTo(CodedOutputStream).
If this message requires more than Integer.MAX_VALUE bytes to encode, the behavior is
unpredictable. It may throw a runtime exception or truncate or slice the data.
Like #writeTo(OutputStream), but writes the size of the message as a varint before
writing the data. This allows more data to be written to the stream after the message without
the need to delimit the message data yourself. Use Builder#mergeDelimitedFrom(InputStream) (or the static method
YourMessageType.parseDelimitedFrom(InputStream)) to parse messages written by this method.
Serializes the message and writes it to output. This is just a trivial wrapper around
#writeTo(CodedOutputStream). This does not flush or close the stream.
NOTE: Protocol Buffers are not self-delimiting. Therefore, if you write any more data to the
stream after the message, you must somehow ensure that the parser on the receiving end does not
interpret this as being part of the protocol message. This can be done e.g. by writing the size
of the message before the data, then making sure to limit the input to that size on the
receiving end (e.g. by wrapping the InputStream in one which limits the input). Alternatively,
just use #writeDelimitedTo(OutputStream).
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[],[],null,["# Interface MessageLite (3.19.4)\n\n public interface MessageLite extends MessageLiteOrBuilder\n\nAbstract interface implemented by Protocol Message objects.\n\nThis interface is implemented by all protocol message objects. Non-lite messages additionally\nimplement the Message interface, which is a subclass of MessageLite. Use MessageLite instead when\nyou only need the subset of features which it supports -- namely, nothing that uses descriptors\nor reflection. You can instruct the protocol compiler to generate classes which implement only\nMessageLite, not the full Message interface, by adding the follow line to the .proto file:\n\noption optimize_for = LITE_RUNTIME;\n\nThis is particularly useful on resource-constrained systems where the full protocol buffers\nruntime library is too big.\n\nNote that on non-constrained systems (e.g. servers) when you need to link in lots of protocol\ndefinitions, a better way to reduce total code footprint is to use `optimize_for =\nCODE_SIZE`. This will make the generated code smaller while still supporting all the same\nfeatures (at the expense of speed). `optimize_for = LITE_RUNTIME` is best when you only\nhave a small number of message types linked into your binary, in which case the size of the\nprotocol buffers runtime itself is the biggest problem. \n\nImplements\n----------\n\n[MessageLiteOrBuilder](/java/docs/reference/protobuf/latest/com.google.protobuf.MessageLiteOrBuilder)\n\nMethods\n-------\n\n### getParserForType()\n\n public abstract Parser\u003c? extends MessageLite\u003e getParserForType()\n\nGets the parser for a message of the same type as this message.\n\n### getSerializedSize()\n\n public abstract int getSerializedSize()\n\nGet the number of bytes required to encode this message. The result is only computed on the\nfirst call and memoized after that.\n\nIf this message requires more than Integer.MAX_VALUE bytes to encode, the return value will\nbe smaller than the actual number of bytes required and might be negative.\n\n### newBuilderForType()\n\n public abstract MessageLite.Builder newBuilderForType()\n\nConstructs a new builder for a message of the same type as this message.\n\n### toBuilder()\n\n public abstract MessageLite.Builder toBuilder()\n\nConstructs a builder initialized with the current message. Use this to derive a new message\nfrom the current one.\n\n### toByteArray()\n\n public abstract byte[] toByteArray()\n\nSerializes the message to a `byte` array and returns it. This is just a trivial wrapper\naround [#writeTo(CodedOutputStream)](/java/docs/reference/protobuf/latest/com.google.protobuf.MessageLite#com_google_protobuf_MessageLite_writeTo_com_google_protobuf_CodedOutputStream_).\n\nIf this message requires more than Integer.MAX_VALUE bytes to encode, the behavior is\nunpredictable. It may throw a runtime exception or truncate or slice the data.\n\n### toByteString()\n\n public abstract ByteString toByteString()\n\nSerializes the message to a `ByteString` and returns it. This is just a trivial wrapper\naround [#writeTo(CodedOutputStream)](/java/docs/reference/protobuf/latest/com.google.protobuf.MessageLite#com_google_protobuf_MessageLite_writeTo_com_google_protobuf_CodedOutputStream_).\n\nIf this message requires more than Integer.MAX_VALUE bytes to encode, the behavior is\nunpredictable. It may throw a runtime exception or truncate or slice the data.\n\n### writeDelimitedTo(OutputStream output)\n\n public abstract void writeDelimitedTo(OutputStream output)\n\nLike [#writeTo(OutputStream)](/java/docs/reference/protobuf/latest/com.google.protobuf.MessageLite#com_google_protobuf_MessageLite_writeTo_java_io_OutputStream_), but writes the size of the message as a varint before\nwriting the data. This allows more data to be written to the stream after the message without\nthe need to delimit the message data yourself. Use Builder#mergeDelimitedFrom(InputStream) (or the static method `\nYourMessageType.parseDelimitedFrom(InputStream)`) to parse messages written by this method.\n\n### writeTo(CodedOutputStream output)\n\n public abstract void writeTo(CodedOutputStream output)\n\nSerializes the message and writes it to `output`. This does not flush or close the\nstream.\n\n### writeTo(OutputStream output)\n\n public abstract void writeTo(OutputStream output)\n\nSerializes the message and writes it to `output`. This is just a trivial wrapper around\n[#writeTo(CodedOutputStream)](/java/docs/reference/protobuf/latest/com.google.protobuf.MessageLite#com_google_protobuf_MessageLite_writeTo_com_google_protobuf_CodedOutputStream_). This does not flush or close the stream.\n\nNOTE: Protocol Buffers are not self-delimiting. Therefore, if you write any more data to the\nstream after the message, you must somehow ensure that the parser on the receiving end does not\ninterpret this as being part of the protocol message. This can be done e.g. by writing the size\nof the message before the data, then making sure to limit the input to that size on the\nreceiving end (e.g. by wrapping the InputStream in one which limits the input). Alternatively,\njust use [#writeDelimitedTo(OutputStream)](/java/docs/reference/protobuf/latest/com.google.protobuf.MessageLite#com_google_protobuf_MessageLite_writeDelimitedTo_java_io_OutputStream_)."]]