• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 package com.google.protobuf;
9 
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.io.OutputStream;
13 
14 /**
15  * Abstract interface implemented by Protocol Message objects.
16  *
17  * <p>This interface is implemented by all protocol message objects. Non-lite messages additionally
18  * implement the Message interface, which is a subclass of MessageLite. Use MessageLite instead when
19  * you only need the subset of features which it supports -- namely, nothing that uses descriptors
20  * or reflection. You can instruct the protocol compiler to generate classes which implement only
21  * MessageLite, not the full Message interface, by adding the follow line to the .proto file:
22  *
23  * <pre>
24  *   option optimize_for = LITE_RUNTIME;
25  * </pre>
26  *
27  * <p>This is particularly useful on resource-constrained systems where the full protocol buffers
28  * runtime library is too big.
29  *
30  * <p>Note that on non-constrained systems (e.g. servers) when you need to link in lots of protocol
31  * definitions, a better way to reduce total code footprint is to use {@code optimize_for =
32  * CODE_SIZE}. This will make the generated code smaller while still supporting all the same
33  * features (at the expense of speed). {@code optimize_for = LITE_RUNTIME} is best when you only
34  * have a small number of message types linked into your binary, in which case the size of the
35  * protocol buffers runtime itself is the biggest problem.
36  *
37  * @author kenton@google.com Kenton Varda
38  */
39 @CheckReturnValue
40 public interface MessageLite extends MessageLiteOrBuilder {
41 
42   /**
43    * Serializes the message and writes it to {@code output}. This does not flush or close the
44    * stream.
45    */
writeTo(CodedOutputStream output)46   void writeTo(CodedOutputStream output) throws IOException;
47 
48   /**
49    * Get the number of bytes required to encode this message. The result is only computed on the
50    * first call and memoized after that.
51    *
52    * If this message requires more than Integer.MAX_VALUE bytes to encode, the return value will
53    * be smaller than the actual number of bytes required and might be negative.
54    */
getSerializedSize()55   int getSerializedSize();
56 
57   /** Gets the parser for a message of the same type as this message. */
getParserForType()58   Parser<? extends MessageLite> getParserForType();
59 
60   // -----------------------------------------------------------------
61   // Convenience methods.
62 
63   /**
64    * Serializes the message to a {@code ByteString} and returns it. This is just a trivial wrapper
65    * around {@link #writeTo(CodedOutputStream)}.
66    *
67    * If this message requires more than Integer.MAX_VALUE bytes to encode, the behavior is
68    * unpredictable. It may throw a runtime exception or truncate or slice the data.
69    */
toByteString()70   ByteString toByteString();
71 
72   /**
73    * Serializes the message to a {@code byte} array and returns it. This is just a trivial wrapper
74    * around {@link #writeTo(CodedOutputStream)}.
75    *
76    * If this message requires more than Integer.MAX_VALUE bytes to encode, the behavior is
77    * unpredictable. It may throw a runtime exception or truncate or slice the data.
78    */
toByteArray()79   byte[] toByteArray();
80 
81   /**
82    * Serializes the message and writes it to {@code output}. This is just a trivial wrapper around
83    * {@link #writeTo(CodedOutputStream)}. This does not flush or close the stream.
84    *
85    * <p>NOTE: Protocol Buffers are not self-delimiting. Therefore, if you write any more data to the
86    * stream after the message, you must somehow ensure that the parser on the receiving end does not
87    * interpret this as being part of the protocol message. This can be done, for instance, by
88    *  writing the size of the message before the data, then making sure to limit the input to that
89    * size on the receiving end by wrapping the InputStream in one which limits the input.
90    * Alternatively, just use {@link #writeDelimitedTo(OutputStream)}.
91    */
writeTo(OutputStream output)92   void writeTo(OutputStream output) throws IOException;
93 
94   /**
95    * Like {@link #writeTo(OutputStream)}, but writes the size of the message as a varint before
96    * writing the data. This allows more data to be written to the stream after the message without
97    * the need to delimit the message data yourself. Use {@link
98    * Builder#mergeDelimitedFrom(InputStream)} (or the static method {@code
99    * YourMessageType.parseDelimitedFrom(InputStream)}) to parse messages written by this method.
100    */
writeDelimitedTo(OutputStream output)101   void writeDelimitedTo(OutputStream output) throws IOException;
102 
103   // =================================================================
104   // Builders
105 
106   /** Constructs a new builder for a message of the same type as this message. */
newBuilderForType()107   Builder newBuilderForType();
108 
109   /**
110    * Constructs a builder initialized with the current message. Use this to derive a new message
111    * from the current one.
112    */
toBuilder()113   Builder toBuilder();
114 
115   /** Abstract interface implemented by Protocol Message builders. */
116   interface Builder extends MessageLiteOrBuilder, Cloneable {
117     /** Resets all fields to their default values. */
118     @CanIgnoreReturnValue
clear()119     Builder clear();
120 
121     /**
122      * Constructs the message based on the state of the Builder. Subsequent changes to the Builder
123      * will not affect the returned message.
124      *
125      * @throws UninitializedMessageException The message is missing one or more required fields
126      *     (i.e. {@link #isInitialized()} returns false). Use {@link #buildPartial()} to bypass this
127      *     check.
128      */
build()129     MessageLite build();
130 
131     /**
132      * Like {@link #build()}, but does not throw an exception if the message is missing required
133      * fields. Instead, a partial message is returned. Subsequent changes to the Builder will not
134      * affect the returned message.
135      */
buildPartial()136     MessageLite buildPartial();
137 
138     /**
139      * Clones the Builder.
140      *
141      * @see Object#clone()
142      */
clone()143     Builder clone();
144 
145     /**
146      * Parses a message of this type from the input and merges it with this message.
147      *
148      * <p>Warning: This does not verify that all required fields are present in the input message.
149      * If you call {@link #build()} without setting all required fields, it will throw an {@link
150      * UninitializedMessageException}, which is a {@code RuntimeException} and thus might not be
151      * caught. There are a few good ways to deal with this:
152      *
153      * <ul>
154      *   <li>Call {@link #isInitialized()} to verify that all required fields are set before
155      *       building.
156      *   <li>Use {@code buildPartial()} to build, which ignores missing required fields.
157      * </ul>
158      *
159      * <p>Note: The caller should call {@link CodedInputStream#checkLastTagWas(int)} after calling
160      * this to verify that the last tag seen was the appropriate end-group tag, or zero for EOF.
161      *
162      * @throws InvalidProtocolBufferException the bytes read are not syntactically correct according
163      *     to the protobuf wire format specification. The data is corrupt, incomplete, or was never
164      *     a protobuf in the first place.
165      * @throws IOException an I/O error reading from the stream
166      */
167     @CanIgnoreReturnValue
mergeFrom(CodedInputStream input)168     Builder mergeFrom(CodedInputStream input) throws IOException;
169 
170     /**
171      * Like {@link Builder#mergeFrom(CodedInputStream)}, but also parses extensions. The extensions
172      * that you want to be able to parse must be registered in {@code extensionRegistry}. Extensions
173      * not in the registry will be treated as unknown fields.
174      *
175      * @throws InvalidProtocolBufferException the bytes read are not syntactically correct according
176      *     to the protobuf wire format specification. The data is corrupt, incomplete, or was never
177      *     a protobuf in the first place.
178      * @throws IOException an I/O error reading from the stream
179      */
180     @CanIgnoreReturnValue
mergeFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry)181     Builder mergeFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry)
182         throws IOException;
183 
184     // ---------------------------------------------------------------
185     // Convenience methods.
186 
187     /**
188      * Parse {@code data} as a message of this type and merge it with the message being built. This
189      * is just a small wrapper around {@link #mergeFrom(CodedInputStream)}.
190      *
191      * @throws InvalidProtocolBufferException the bytes in data are not syntactically correct
192      *     according to the protobuf wire format specification. The data is corrupt, incomplete, or
193      *     was never a protobuf in the first place.
194      * @return this
195      */
196     @CanIgnoreReturnValue
mergeFrom(ByteString data)197     Builder mergeFrom(ByteString data) throws InvalidProtocolBufferException;
198 
199     /**
200      * Parse {@code data} as a message of this type and merge it with the message being built. This
201      * is just a small wrapper around {@link #mergeFrom(CodedInputStream,ExtensionRegistryLite)}.
202      *
203      * @throws InvalidProtocolBufferException the bytes in data are not syntactically correct
204      *     according to the protobuf wire format specification. The data is corrupt, incomplete, or
205      *     was never a protobuf in the first place.
206      * @return this
207      */
208     @CanIgnoreReturnValue
mergeFrom(ByteString data, ExtensionRegistryLite extensionRegistry)209     Builder mergeFrom(ByteString data, ExtensionRegistryLite extensionRegistry)
210         throws InvalidProtocolBufferException;
211 
212     /**
213      * Parse {@code data} as a message of this type and merge it with the message being built. This
214      * is just a small wrapper around {@link #mergeFrom(CodedInputStream)}.
215      *
216      * @throws InvalidProtocolBufferException the bytes in data are not syntactically correct
217      *     according to the protobuf wire format specification. The data is corrupt, incomplete, or
218      *     was never a protobuf in the first place.
219      * @return this
220      */
221     @CanIgnoreReturnValue
mergeFrom(byte[] data)222     Builder mergeFrom(byte[] data) throws InvalidProtocolBufferException;
223 
224     /**
225      * Parse {@code data} as a message of this type and merge it with the message being built. This
226      * is just a small wrapper around {@link #mergeFrom(CodedInputStream)}.
227      *
228      * @throws InvalidProtocolBufferException the bytes in data are not syntactically correct
229      *     according to the protobuf wire format specification. The data is corrupt, incomplete, or
230      *     was never a protobuf in the first place.
231      * @return this
232      */
233     @CanIgnoreReturnValue
mergeFrom(byte[] data, int off, int len)234     Builder mergeFrom(byte[] data, int off, int len) throws InvalidProtocolBufferException;
235 
236     /**
237      * Parse {@code data} as a message of this type and merge it with the message being built. This
238      * is just a small wrapper around {@link #mergeFrom(CodedInputStream,ExtensionRegistryLite)}.
239      *
240      * @throws InvalidProtocolBufferException the bytes in data are not syntactically correct
241      *     according to the protobuf wire format specification. The data is corrupt, incomplete, or
242      *     was never a protobuf in the first place.
243      * @return this
244      */
245     @CanIgnoreReturnValue
mergeFrom(byte[] data, ExtensionRegistryLite extensionRegistry)246     Builder mergeFrom(byte[] data, ExtensionRegistryLite extensionRegistry)
247         throws InvalidProtocolBufferException;
248 
249     /**
250      * Parse {@code data} as a message of this type and merge it with the message being built. This
251      * is just a small wrapper around {@link #mergeFrom(CodedInputStream,ExtensionRegistryLite)}.
252      *
253      * @throws InvalidProtocolBufferException the bytes in data are not syntactically correct
254      *     according to the protobuf wire format specification. The data is corrupt, incomplete, or
255      *     was never a protobuf in the first place.
256      * @return this
257      */
258     @CanIgnoreReturnValue
mergeFrom(byte[] data, int off, int len, ExtensionRegistryLite extensionRegistry)259     Builder mergeFrom(byte[] data, int off, int len, ExtensionRegistryLite extensionRegistry)
260         throws InvalidProtocolBufferException;
261 
262     /**
263      * Parse a message of this type from {@code input} and merge it with the message being built.
264      * This is just a small wrapper around {@link #mergeFrom(CodedInputStream)}. Note that this
265      * method always reads the <i>entire</i> input (unless it throws an exception). If you want it
266      * to stop earlier, you will need to wrap your input in some wrapper stream that limits reading.
267      * Or, use {@link MessageLite#writeDelimitedTo(OutputStream)} to write your message and {@link
268      * #mergeDelimitedFrom(InputStream)} to read it.
269      *
270      * <p>Despite usually reading the entire input, this does not close the stream.
271      *
272      * @throws InvalidProtocolBufferException the bytes read are not syntactically correct according
273      *     to the protobuf wire format specification. The data is corrupt, incomplete, or was never
274      *     a protobuf in the first place.
275      * @throws IOException an I/O error reading from the stream
276      * @return this
277      */
278     @CanIgnoreReturnValue
mergeFrom(InputStream input)279     Builder mergeFrom(InputStream input) throws IOException;
280 
281     /**
282      * Parse a message of this type from {@code input} and merge it with the message being built.
283      * This is just a small wrapper around {@link
284      * #mergeFrom(CodedInputStream,ExtensionRegistryLite)}.
285      *
286      * @return this
287      */
288     @CanIgnoreReturnValue
mergeFrom(InputStream input, ExtensionRegistryLite extensionRegistry)289     Builder mergeFrom(InputStream input, ExtensionRegistryLite extensionRegistry)
290         throws IOException;
291 
292     /**
293      * Merge {@code other} into the message being built. {@code other} must have the exact same type
294      * as {@code this} (i.e. {@code getClass().equals(getDefaultInstanceForType().getClass())}).
295      *
296      * <p>Merging occurs as follows. For each field:<br>
297      * * For singular primitive fields, if the field is set in {@code other}, then {@code other}'s
298      * value overwrites the value in this message.<br>
299      * * For singular message fields, if the field is set in {@code other}, it is merged into the
300      * corresponding sub-message of this message using the same merging rules.<br>
301      * * For repeated fields, the elements in {@code other} are concatenated with the elements in
302      * this message. * For oneof groups, if the other message has one of the fields set, the group
303      * of this message is cleared and replaced by the field of the other message, so that the oneof
304      * constraint is preserved.
305      *
306      * <p>This is equivalent to the {@code Message::MergeFrom} method in C++.
307      */
308     @CanIgnoreReturnValue
mergeFrom(MessageLite other)309     Builder mergeFrom(MessageLite other);
310 
311     /**
312      * Like {@link #mergeFrom(InputStream)}, but does not read until EOF. Instead, the size of the
313      * message (encoded as a varint) is read first, then the message data. Use {@link
314      * MessageLite#writeDelimitedTo(OutputStream)} to write messages in this format.
315      *
316      * @return true if successful, or false if the stream is at EOF when the method starts. Any
317      *     other error (including reaching EOF during parsing) causes an exception to be thrown.
318      * @throws InvalidProtocolBufferException the bytes read are not syntactically correct according
319      *     to the protobuf wire format specification. The data is corrupt, incomplete, or was never
320      *     a protobuf in the first place.
321      * @throws IOException an I/O error reading from the stream
322      */
mergeDelimitedFrom(InputStream input)323     boolean mergeDelimitedFrom(InputStream input) throws IOException;
324 
325     /**
326      * Like {@link #mergeDelimitedFrom(InputStream)} but supporting extensions.
327      *
328      * @return true if successful, or false if the stream is at EOF when the method starts. Any
329      *     other error (including reaching EOF during parsing) causes an exception to be thrown.
330      * @throws InvalidProtocolBufferException the bytes read are not syntactically correct according
331      *     to the protobuf wire format specification. The data is corrupt, incomplete, or was never
332      *     a protobuf in the first place.
333      * @throws IOException an I/O error reading from the stream
334      */
mergeDelimitedFrom(InputStream input, ExtensionRegistryLite extensionRegistry)335     boolean mergeDelimitedFrom(InputStream input, ExtensionRegistryLite extensionRegistry)
336         throws IOException;
337   }
338 }
339