Top Description Inners Methods
com.google.protobuf

public Interface MessageLite

extends MessageLiteOrBuilder
Known Direct Subinterfaces
com.google.protobuf.Message
Known Direct Implementers
com.google.protobuf.UnknownFieldSet, com.google.protobuf.AbstractMessageLite
Imports
java.io.IOException, .InputStream, .OutputStream

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.

Author
[email protected] Kenton Varda

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
MessageLite.Builder

Abstract interface implemented by Protocol Message builders.

Method Summary

Modifier and TypeMethod and Description
public Parser<? extends MessageLite>
getParserForType()

Gets the parser for a message of the same type as this message.

public int
getSerializedSize()

Get the number of bytes required to encode this message.

public MessageLite.Builder
newBuilderForType()

Constructs a new builder for a message of the same type as this message.

public MessageLite.Builder
toBuilder()

Constructs a builder initialized with the current message.

public byte[]
toByteArray()

Serializes the message to a byte array and returns it.

public ByteString
toByteString()

Serializes the message to a ByteString and returns it.

public void
writeDelimitedTo(OutputStream output)

Like writeTo(OutputStream), but writes the size of the message as a varint before writing the data.

public void
writeTo(CodedOutputStream output)

Serializes the message and writes it to output.

public void
writeTo(OutputStream output)

Serializes the message and writes it to output.

Inherited from com.google.protobuf.MessageLiteOrBuilder:
getDefaultInstanceForTypeisInitialized

Method Detail

getParserForTypeback to summary
public Parser<? extends MessageLite> getParserForType()

Gets the parser for a message of the same type as this message.

getSerializedSizeback to summary
public int getSerializedSize()

Get the number of bytes required to encode this message. The result is only computed on the first call and memoized after that.

newBuilderForTypeback to summary
public MessageLite.Builder newBuilderForType()

Constructs a new builder for a message of the same type as this message.

toBuilderback to summary
public MessageLite.Builder toBuilder()

Constructs a builder initialized with the current message. Use this to derive a new message from the current one.

toByteArrayback to summary
public byte[] toByteArray()

Serializes the message to a byte array and returns it. This is just a trivial wrapper around writeTo(CodedOutputStream).

toByteStringback to summary
public ByteString toByteString()

Serializes the message to a ByteString and returns it. This is just a trivial wrapper around writeTo(CodedOutputStream).

writeDelimitedToback to summary
public void writeDelimitedTo(OutputStream output) throws IOException

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.

writeToback to summary
public void writeTo(CodedOutputStream output) throws IOException

Serializes the message and writes it to output. This does not flush or close the stream.

writeToback to summary
public void writeTo(OutputStream output) throws IOException

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).

com.google.protobuf back to summary

public Interface MessageLite.Builder

extends MessageLiteOrBuilder, Cloneable
Known Direct Subinterfaces
com.google.protobuf.Message.Builder
Known Direct Implementers
com.google.protobuf.UnknownFieldSet.Builder, com.google.protobuf.AbstractMessageLite.Builder

Abstract interface implemented by Protocol Message builders.

Method Summary

Modifier and TypeMethod and Description
public MessageLite
build()

Constructs the message based on the state of the Builder.

public MessageLite
buildPartial()

Like build(), but does not throw an exception if the message is missing required fields.

public MessageLite.Builder
clear()

Resets all fields to their default values.

public MessageLite.Builder
clone()

Clones the Builder.

public boolean

Returns:

True if successful, or false if the stream is at EOF when the method starts. Any other error (including reaching EOF during parsing) will cause an exception to be thrown.
mergeDelimitedFrom
(InputStream input)

Like mergeFrom(InputStream), but does not read until EOF.

public boolean
mergeDelimitedFrom(InputStream input, ExtensionRegistryLite extensionRegistry)

Like mergeDelimitedFrom(InputStream) but supporting extensions.

public MessageLite.Builder
mergeFrom(CodedInputStream input)

Parses a message of this type from the input and merges it with this message.

public MessageLite.Builder
mergeFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry)

Like Builder#mergeFrom(CodedInputStream), but also parses extensions.

public MessageLite.Builder

Returns:

this
mergeFrom
(ByteString data)

Parse data as a message of this type and merge it with the message being built.

public MessageLite.Builder

Returns:

this
mergeFrom
(ByteString data, ExtensionRegistryLite extensionRegistry)

Parse data as a message of this type and merge it with the message being built.

public MessageLite.Builder

Returns:

this
mergeFrom
(byte[] data)

Parse data as a message of this type and merge it with the message being built.

public MessageLite.Builder

Returns:

this
mergeFrom
(byte[] data, int off, int len)

Parse data as a message of this type and merge it with the message being built.

public MessageLite.Builder

Returns:

this
mergeFrom
(byte[] data, ExtensionRegistryLite extensionRegistry)

Parse data as a message of this type and merge it with the message being built.

public MessageLite.Builder

Returns:

this
mergeFrom
(byte[] data, int off, int len, ExtensionRegistryLite extensionRegistry)

Parse data as a message of this type and merge it with the message being built.

public MessageLite.Builder

Returns:

this
mergeFrom
(InputStream input)

Parse a message of this type from input and merge it with the message being built.

public MessageLite.Builder

Returns:

this
mergeFrom
(InputStream input, ExtensionRegistryLite extensionRegistry)

Parse a message of this type from input and merge it with the message being built.

public MessageLite.Builder
mergeFrom(MessageLite other)

Merge other into the message being built.

Inherited from com.google.protobuf.MessageLiteOrBuilder:
getDefaultInstanceForTypeisInitialized

Method Detail

buildback to summary
public MessageLite build()

Constructs the message based on the state of the Builder. Subsequent changes to the Builder will not affect the returned message.

Exceptions
UninitializedMessageException:
The message is missing one or more required fields (i.e. isInitialized() returns false). Use buildPartial() to bypass this check.
buildPartialback to summary
public MessageLite buildPartial()

Like build(), but does not throw an exception if the message is missing required fields. Instead, a partial message is returned. Subsequent changes to the Builder will not affect the returned message.

clearback to summary
public MessageLite.Builder clear()

Resets all fields to their default values.

cloneback to summary
public MessageLite.Builder clone()

Clones the Builder.

See Also
Object#clone()
mergeDelimitedFromback to summary
public boolean mergeDelimitedFrom(InputStream input) throws IOException

Like mergeFrom(InputStream), but does not read until EOF. Instead, the size of the message (encoded as a varint) is read first, then the message data. Use MessageLite#writeDelimitedTo(OutputStream) to write messages in this format.

Returns:boolean

True if successful, or false if the stream is at EOF when the method starts. Any other error (including reaching EOF during parsing) will cause an exception to be thrown.

mergeDelimitedFromback to summary
public boolean mergeDelimitedFrom(InputStream input, ExtensionRegistryLite extensionRegistry) throws IOException

Like mergeDelimitedFrom(InputStream) but supporting extensions.

mergeFromback to summary
public MessageLite.Builder mergeFrom(CodedInputStream input) throws IOException

Parses a message of this type from the input and merges it with this message.

Warning: This does not verify that all required fields are present in the input message. If you call build() without setting all required fields, it will throw an UninitializedMessageException, which is a RuntimeException and thus might not be caught. There are a few good ways to deal with this:

  • Call isInitialized() to verify that all required fields are set before building.
  • Use buildPartial() to build, which ignores missing required fields.

Note

The caller should call CodedInputStream#checkLastTagWas(int) after calling this to verify that the last tag seen was the appropriate end-group tag, or zero for EOF.

mergeFromback to summary
public MessageLite.Builder mergeFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry) throws IOException

Like Builder#mergeFrom(CodedInputStream), but also parses extensions. The extensions that you want to be able to parse must be registered in extensionRegistry. Extensions not in the registry will be treated as unknown fields.

mergeFromback to summary
public MessageLite.Builder mergeFrom(ByteString data) throws InvalidProtocolBufferException

Parse data as a message of this type and merge it with the message being built. This is just a small wrapper around mergeFrom(CodedInputStream).

Returns:MessageLite.Builder

this

mergeFromback to summary
public MessageLite.Builder mergeFrom(ByteString data, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException

Parse data as a message of this type and merge it with the message being built. This is just a small wrapper around mergeFrom(CodedInputStream, ExtensionRegistryLite).

Returns:MessageLite.Builder

this

mergeFromback to summary
public MessageLite.Builder mergeFrom(byte[] data) throws InvalidProtocolBufferException

Parse data as a message of this type and merge it with the message being built. This is just a small wrapper around mergeFrom(CodedInputStream).

Returns:MessageLite.Builder

this

mergeFromback to summary
public MessageLite.Builder mergeFrom(byte[] data, int off, int len) throws InvalidProtocolBufferException

Parse data as a message of this type and merge it with the message being built. This is just a small wrapper around mergeFrom(CodedInputStream).

Returns:MessageLite.Builder

this

mergeFromback to summary
public MessageLite.Builder mergeFrom(byte[] data, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException

Parse data as a message of this type and merge it with the message being built. This is just a small wrapper around mergeFrom(CodedInputStream, ExtensionRegistryLite).

Returns:MessageLite.Builder

this

mergeFromback to summary
public MessageLite.Builder mergeFrom(byte[] data, int off, int len, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException

Parse data as a message of this type and merge it with the message being built. This is just a small wrapper around mergeFrom(CodedInputStream, ExtensionRegistryLite).

Returns:MessageLite.Builder

this

mergeFromback to summary
public MessageLite.Builder mergeFrom(InputStream input) throws IOException

Parse a message of this type from input and merge it with the message being built. This is just a small wrapper around mergeFrom(CodedInputStream). Note that this method always reads the entire input (unless it throws an exception). If you want it to stop earlier, you will need to wrap your input in some wrapper stream that limits reading. Or, use MessageLite#writeDelimitedTo(OutputStream) to write your message and mergeDelimitedFrom(InputStream) to read it.

Despite usually reading the entire input, this does not close the stream.

Returns:MessageLite.Builder

this

mergeFromback to summary
public MessageLite.Builder mergeFrom(InputStream input, ExtensionRegistryLite extensionRegistry) throws IOException

Parse a message of this type from input and merge it with the message being built. This is just a small wrapper around mergeFrom(CodedInputStream, ExtensionRegistryLite).

Returns:MessageLite.Builder

this

mergeFromback to summary
public MessageLite.Builder mergeFrom(MessageLite other)

Merge other into the message being built. other must have the exact same type as this (i.e. getClass().equals(getDefaultInstanceForType().getClass())).

Merging occurs as follows. For each field:
* For singular primitive fields, if the field is set in other, then other's value overwrites the value in this message.
* For singular message fields, if the field is set in other, it is merged into the corresponding sub-message of this message using the same merging rules.
* For repeated fields, the elements in other are concatenated with the elements in this message. * For oneof groups, if the other message has one of the fields set, the group of this message is cleared and replaced by the field of the other message, so that the oneof constraint is preserved.

This is equivalent to the Message::MergeFrom method in C++.