34
34
package com.google.protobuf;
36
import java.io.IOException;
36
37
import java.io.InputStream;
37
import java.io.IOException;
38
import java.io.OutputStream;
39
38
import java.util.Map;
42
41
* Abstract interface implemented by Protocol Message objects.
43
* See also {@link MessageLite}, which defines most of the methods that typical
44
* users care about. {@link Message} adds to it methods that are not available
45
* in the "lite" runtime. The biggest added features are introspection and
46
* reflection -- i.e., getting descriptors for the message type and accessing
47
* the field values dynamically.
44
49
* @author kenton@google.com Kenton Varda
46
public interface Message {
51
public interface Message extends MessageLite {
48
53
* Get the message's type's descriptor. This differs from the
49
54
* {@code getDescriptor()} method of generated message classes in that
163
145
* Converts the message to a string in protocol buffer text format. This is
164
146
* just a trivial wrapper around {@link TextFormat#printToString(Message)}.
166
149
String toString();
169
* Serializes the message to a {@code ByteString} and returns it. This is
170
* just a trivial wrapper around
171
* {@link #writeTo(CodedOutputStream)}.
173
ByteString toByteString();
176
* Serializes the message to a {@code byte} array and returns it. This is
177
* just a trivial wrapper around
178
* {@link #writeTo(CodedOutputStream)}.
180
byte[] toByteArray();
183
* Serializes the message and writes it to {@code output}. This is just a
184
* trivial wrapper around {@link #writeTo(CodedOutputStream)}. This does
185
* not flush or close the stream.
187
* NOTE: Protocol Buffers are not self-delimiting. Therefore, if you write
188
* any more data to the stream after the message, you must somehow ensure
189
* that the parser on the receiving end does not interpret this as being
190
* part of the protocol message. This can be done e.g. by writing the size
191
* of the message before the data, then making sure to limit the input to
192
* that size on the receiving end (e.g. by wrapping the InputStream in one
193
* which limits the input). Alternatively, just use
194
* {@link #writeDelimitedTo(OutputStream)}.
196
void writeTo(OutputStream output) throws IOException;
199
* Like {@link #writeTo(OutputStream)}, but writes the size of the message
200
* as a varint before writing the data. This allows more data to be written
201
* to the stream after the message without the need to delimit the message
202
* data yourself. Use {@link Builder#mergeDelimitedFrom(InputStream)} (or
203
* the static method {@code YourMessageType.parseDelimitedFrom(InputStream)})
204
* to parse messages written by this method.
206
void writeDelimitedTo(OutputStream output) throws IOException;
208
151
// =================================================================
212
* Constructs a new builder for a message of the same type as this message.
154
// (From MessageLite, re-declared here only for return type covariance.)
214
155
Builder newBuilderForType();
217
* Constructs a builder initialized with the current message. Use this to
218
* derive a new message from the current one.
220
156
Builder toBuilder();
223
159
* Abstract interface implemented by Protocol Message builders.
225
public static interface Builder extends Cloneable {
226
/** Resets all fields to their default values. */
161
interface Builder extends MessageLite.Builder {
162
// (From MessageLite.Builder, re-declared here only for return type
245
182
Builder mergeFrom(Message other);
248
* Construct the final message. Once this is called, the Builder is no
249
* longer valid, and calling any other method may throw a
250
* NullPointerException. If you need to continue working with the builder
251
* after calling {@code build()}, {@code clone()} it first.
252
* @throws UninitializedMessageException The message is missing one or more
253
* required fields (i.e. {@link #isInitialized()} returns false).
254
* Use {@link #buildPartial()} to bypass this check.
184
// (From MessageLite.Builder, re-declared here only for return type
259
* Like {@link #build()}, but does not throw an exception if the message
260
* is missing required fields. Instead, a partial message is returned.
262
187
Message buildPartial();
265
* Clones the Builder.
266
* @see Object#clone()
271
* Returns true if all required fields in the message and all embedded
272
* messages are set, false otherwise.
274
boolean isInitialized();
277
* Parses a message of this type from the input and merges it with this
278
* message, as if using {@link Builder#mergeFrom(Message)}.
280
* <p>Warning: This does not verify that all required fields are present in
281
* the input message. If you call {@link #build()} without setting all
282
* required fields, it will throw an {@link UninitializedMessageException},
283
* which is a {@code RuntimeException} and thus might not be caught. There
284
* are a few good ways to deal with this:
286
* <li>Call {@link #isInitialized()} to verify that all required fields
287
* are set before building.
288
* <li>Parse the message separately using one of the static
289
* {@code parseFrom} methods, then use {@link #mergeFrom(Message)}
290
* to merge it with this one. {@code parseFrom} will throw an
291
* {@link InvalidProtocolBufferException} (an {@code IOException})
292
* if some required fields are missing.
293
* <li>Use {@code buildPartial()} to build, which ignores missing
297
* <p>Note: The caller should call
298
* {@link CodedInputStream#checkLastTagWas(int)} after calling this to
299
* verify that the last tag seen was the appropriate end-group tag,
302
189
Builder mergeFrom(CodedInputStream input) throws IOException;
305
* Like {@link Builder#mergeFrom(CodedInputStream)}, but also
306
* parses extensions. The extensions that you want to be able to parse
307
* must be registered in {@code extensionRegistry}. Extensions not in
308
* the registry will be treated as unknown fields.
310
190
Builder mergeFrom(CodedInputStream input,
311
ExtensionRegistry extensionRegistry)
191
ExtensionRegistryLite extensionRegistry)
312
192
throws IOException;
399
277
// ---------------------------------------------------------------
400
278
// Convenience methods.
403
* Parse {@code data} as a message of this type and merge it with the
404
* message being built. This is just a small wrapper around
405
* {@link #mergeFrom(CodedInputStream)}.
280
// (From MessageLite.Builder, re-declared here only for return type
407
282
Builder mergeFrom(ByteString data) throws InvalidProtocolBufferException;
410
* Parse {@code data} as a message of this type and merge it with the
411
* message being built. This is just a small wrapper around
412
* {@link #mergeFrom(CodedInputStream,ExtensionRegistry)}.
414
283
Builder mergeFrom(ByteString data,
415
ExtensionRegistry extensionRegistry)
416
throws InvalidProtocolBufferException;
419
* Parse {@code data} as a message of this type and merge it with the
420
* message being built. This is just a small wrapper around
421
* {@link #mergeFrom(CodedInputStream)}.
423
public Builder mergeFrom(byte[] data) throws InvalidProtocolBufferException;
426
* Parse {@code data} as a message of this type and merge it with the
427
* message being built. This is just a small wrapper around
428
* {@link #mergeFrom(CodedInputStream)}.
430
public Builder mergeFrom(byte[] data, int off, int len) throws InvalidProtocolBufferException;
433
* Parse {@code data} as a message of this type and merge it with the
434
* message being built. This is just a small wrapper around
435
* {@link #mergeFrom(CodedInputStream,ExtensionRegistry)}.
284
ExtensionRegistryLite extensionRegistry)
285
throws InvalidProtocolBufferException;
286
Builder mergeFrom(byte[] data) throws InvalidProtocolBufferException;
287
Builder mergeFrom(byte[] data, int off, int len)
288
throws InvalidProtocolBufferException;
437
289
Builder mergeFrom(byte[] data,
438
ExtensionRegistry extensionRegistry)
290
ExtensionRegistryLite extensionRegistry)
439
291
throws InvalidProtocolBufferException;
442
* Parse {@code data} as a message of this type and merge it with the
443
* message being built. This is just a small wrapper around
444
* {@link #mergeFrom(CodedInputStream,ExtensionRegistry)}.
446
292
Builder mergeFrom(byte[] data, int off, int len,
447
ExtensionRegistry extensionRegistry)
293
ExtensionRegistryLite extensionRegistry)
448
294
throws InvalidProtocolBufferException;
451
* Parse a message of this type from {@code input} and merge it with the
452
* message being built. This is just a small wrapper around
453
* {@link #mergeFrom(CodedInputStream)}. Note that this method always
454
* reads the <i>entire</i> input (unless it throws an exception). If you
455
* want it to stop earlier, you will need to wrap your input in some
456
* wrapper stream that limits reading. Or, use
457
* {@link Message#writeDelimitedTo(OutputStream)} to write your message and
458
* {@link #mergeDelimitedFrom(InputStream)} to read it.
460
* Despite usually reading the entire input, this does not close the stream.
462
295
Builder mergeFrom(InputStream input) throws IOException;
465
* Parse a message of this type from {@code input} and merge it with the
466
* message being built. This is just a small wrapper around
467
* {@link #mergeFrom(CodedInputStream,ExtensionRegistry)}.
469
296
Builder mergeFrom(InputStream input,
470
ExtensionRegistry extensionRegistry)
297
ExtensionRegistryLite extensionRegistry)
471
298
throws IOException;
474
* Like {@link #mergeFrom(InputStream)}, but does not read until EOF.
475
* Instead, the size of the message (encoded as a varint) is read first,
476
* then the message data. Use
477
* {@link Message#writeDelimitedTo(OutputStream)} to write messages in this
480
299
Builder mergeDelimitedFrom(InputStream input)
481
300
throws IOException;
484
* Like {@link #mergeDelimitedFrom(InputStream)} but supporting extensions.
486
301
Builder mergeDelimitedFrom(InputStream input,
487
ExtensionRegistry extensionRegistry)
302
ExtensionRegistryLite extensionRegistry)
488
303
throws IOException;