1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // https://developers.google.com/protocol-buffers/ 4 // 5 // Redistribution and use in source and binary forms, with or without 6 // modification, are permitted provided that the following conditions are 7 // met: 8 // 9 // * Redistributions of source code must retain the above copyright 10 // notice, this list of conditions and the following disclaimer. 11 // * Redistributions in binary form must reproduce the above 12 // copyright notice, this list of conditions and the following disclaimer 13 // in the documentation and/or other materials provided with the 14 // distribution. 15 // * Neither the name of Google Inc. nor the names of its 16 // contributors may be used to endorse or promote products derived from 17 // this software without specific prior written permission. 18 // 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31 // TODO(kenton): Use generics? E.g. Builder<BuilderType extends Builder>, then 32 // mergeFrom*() could return BuilderType for better type-safety. 33 34 package com.google.protobuf; 35 36 import java.io.IOException; 37 import java.io.InputStream; 38 import java.io.OutputStream; 39 40 /** 41 * Abstract interface implemented by Protocol Message objects. 42 * 43 * <p>This interface is implemented by all protocol message objects. Non-lite messages additionally 44 * implement the Message interface, which is a subclass of MessageLite. Use MessageLite instead when 45 * you only need the subset of features which it supports -- namely, nothing that uses descriptors 46 * or reflection. You can instruct the protocol compiler to generate classes which implement only 47 * MessageLite, not the full Message interface, by adding the follow line to the .proto file: 48 * 49 * <pre> 50 * option optimize_for = LITE_RUNTIME; 51 * </pre> 52 * 53 * <p>This is particularly useful on resource-constrained systems where the full protocol buffers 54 * runtime library is too big. 55 * 56 * <p>Note that on non-constrained systems (e.g. servers) when you need to link in lots of protocol 57 * definitions, a better way to reduce total code footprint is to use {@code optimize_for = 58 * CODE_SIZE}. This will make the generated code smaller while still supporting all the same 59 * features (at the expense of speed). {@code optimize_for = LITE_RUNTIME} is best when you only 60 * have a small number of message types linked into your binary, in which case the size of the 61 * protocol buffers runtime itself is the biggest problem. 62 * 63 * @author kenton@google.com Kenton Varda 64 */ 65 public interface MessageLite extends MessageLiteOrBuilder { 66 67 /** 68 * Serializes the message and writes it to {@code output}. This does not flush or close the 69 * stream. 70 */ writeTo(CodedOutputStream output)71 void writeTo(CodedOutputStream output) throws IOException; 72 73 /** 74 * Get the number of bytes required to encode this message. The result is only computed on the 75 * first call and memoized after that. 76 */ getSerializedSize()77 int getSerializedSize(); 78 79 /** Gets the parser for a message of the same type as this message. */ getParserForType()80 Parser<? extends MessageLite> getParserForType(); 81 82 // ----------------------------------------------------------------- 83 // Convenience methods. 84 85 /** 86 * Serializes the message to a {@code ByteString} and returns it. This is just a trivial wrapper 87 * around {@link #writeTo(CodedOutputStream)}. 88 */ toByteString()89 ByteString toByteString(); 90 91 /** 92 * Serializes the message to a {@code byte} array and returns it. This is just a trivial wrapper 93 * around {@link #writeTo(CodedOutputStream)}. 94 */ toByteArray()95 byte[] toByteArray(); 96 97 /** 98 * Serializes the message and writes it to {@code output}. This is just a trivial wrapper around 99 * {@link #writeTo(CodedOutputStream)}. This does not flush or close the stream. 100 * 101 * <p>NOTE: Protocol Buffers are not self-delimiting. Therefore, if you write any more data to the 102 * stream after the message, you must somehow ensure that the parser on the receiving end does not 103 * interpret this as being part of the protocol message. This can be done e.g. by writing the size 104 * of the message before the data, then making sure to limit the input to that size on the 105 * receiving end (e.g. by wrapping the InputStream in one which limits the input). Alternatively, 106 * just use {@link #writeDelimitedTo(OutputStream)}. 107 */ writeTo(OutputStream output)108 void writeTo(OutputStream output) throws IOException; 109 110 /** 111 * Like {@link #writeTo(OutputStream)}, but writes the size of the message as a varint before 112 * writing the data. This allows more data to be written to the stream after the message without 113 * the need to delimit the message data yourself. Use {@link 114 * Builder#mergeDelimitedFrom(InputStream)} (or the static method {@code 115 * YourMessageType.parseDelimitedFrom(InputStream)}) to parse messages written by this method. 116 */ writeDelimitedTo(OutputStream output)117 void writeDelimitedTo(OutputStream output) throws IOException; 118 119 120 // ================================================================= 121 // Builders 122 123 /** Constructs a new builder for a message of the same type as this message. */ newBuilderForType()124 Builder newBuilderForType(); 125 126 /** 127 * Constructs a builder initialized with the current message. Use this to derive a new message 128 * from the current one. 129 */ toBuilder()130 Builder toBuilder(); 131 132 /** Abstract interface implemented by Protocol Message builders. */ 133 interface Builder extends MessageLiteOrBuilder, Cloneable { 134 /** Resets all fields to their default values. */ clear()135 Builder clear(); 136 137 /** 138 * Constructs the message based on the state of the Builder. Subsequent changes to the Builder 139 * will not affect the returned message. 140 * 141 * @throws UninitializedMessageException The message is missing one or more required fields 142 * (i.e. {@link #isInitialized()} returns false). Use {@link #buildPartial()} to bypass this 143 * check. 144 */ build()145 MessageLite build(); 146 147 /** 148 * Like {@link #build()}, but does not throw an exception if the message is missing required 149 * fields. Instead, a partial message is returned. Subsequent changes to the Builder will not 150 * affect the returned message. 151 */ buildPartial()152 MessageLite buildPartial(); 153 154 /** 155 * Clones the Builder. 156 * 157 * @see Object#clone() 158 */ clone()159 Builder clone(); 160 161 /** 162 * Parses a message of this type from the input and merges it with this message. 163 * 164 * <p>Warning: This does not verify that all required fields are present in the input message. 165 * If you call {@link #build()} without setting all required fields, it will throw an {@link 166 * UninitializedMessageException}, which is a {@code RuntimeException} and thus might not be 167 * caught. There are a few good ways to deal with this: 168 * 169 * <ul> 170 * <li>Call {@link #isInitialized()} to verify that all required fields are set before 171 * building. 172 * <li>Use {@code buildPartial()} to build, which ignores missing required fields. 173 * </ul> 174 * 175 * <p>Note: The caller should call {@link CodedInputStream#checkLastTagWas(int)} after calling 176 * this to verify that the last tag seen was the appropriate end-group tag, or zero for EOF. 177 */ mergeFrom(CodedInputStream input)178 Builder mergeFrom(CodedInputStream input) throws IOException; 179 180 /** 181 * Like {@link Builder#mergeFrom(CodedInputStream)}, but also parses extensions. The extensions 182 * that you want to be able to parse must be registered in {@code extensionRegistry}. Extensions 183 * not in the registry will be treated as unknown fields. 184 */ mergeFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry)185 Builder mergeFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry) 186 throws IOException; 187 188 // --------------------------------------------------------------- 189 // Convenience methods. 190 191 /** 192 * Parse {@code data} as a message of this type and merge it with the message being built. This 193 * is just a small wrapper around {@link #mergeFrom(CodedInputStream)}. 194 * 195 * @return this 196 */ 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 * @return this 204 */ mergeFrom(ByteString data, ExtensionRegistryLite extensionRegistry)205 Builder mergeFrom(ByteString data, ExtensionRegistryLite extensionRegistry) 206 throws InvalidProtocolBufferException; 207 208 /** 209 * Parse {@code data} as a message of this type and merge it with the message being built. This 210 * is just a small wrapper around {@link #mergeFrom(CodedInputStream)}. 211 * 212 * @return this 213 */ mergeFrom(byte[] data)214 Builder mergeFrom(byte[] data) throws InvalidProtocolBufferException; 215 216 /** 217 * Parse {@code data} as a message of this type and merge it with the message being built. This 218 * is just a small wrapper around {@link #mergeFrom(CodedInputStream)}. 219 * 220 * @return this 221 */ mergeFrom(byte[] data, int off, int len)222 Builder mergeFrom(byte[] data, int off, int len) 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,ExtensionRegistryLite)}. 227 * 228 * @return this 229 */ mergeFrom(byte[] data, ExtensionRegistryLite extensionRegistry)230 Builder mergeFrom(byte[] data, ExtensionRegistryLite extensionRegistry) 231 throws InvalidProtocolBufferException; 232 233 /** 234 * Parse {@code data} as a message of this type and merge it with the message being built. This 235 * is just a small wrapper around {@link #mergeFrom(CodedInputStream,ExtensionRegistryLite)}. 236 * 237 * @return this 238 */ mergeFrom(byte[] data, int off, int len, ExtensionRegistryLite extensionRegistry)239 Builder mergeFrom(byte[] data, int off, int len, ExtensionRegistryLite extensionRegistry) 240 throws InvalidProtocolBufferException; 241 242 /** 243 * Parse a message of this type from {@code input} and merge it with the message being built. 244 * This is just a small wrapper around {@link #mergeFrom(CodedInputStream)}. Note that this 245 * method always reads the <i>entire</i> input (unless it throws an exception). If you want it 246 * to stop earlier, you will need to wrap your input in some wrapper stream that limits reading. 247 * Or, use {@link MessageLite#writeDelimitedTo(OutputStream)} to write your message and {@link 248 * #mergeDelimitedFrom(InputStream)} to read it. 249 * 250 * <p>Despite usually reading the entire input, this does not close the stream. 251 * 252 * @return this 253 */ mergeFrom(InputStream input)254 Builder mergeFrom(InputStream input) throws IOException; 255 256 /** 257 * Parse a message of this type from {@code input} and merge it with the message being built. 258 * This is just a small wrapper around {@link 259 * #mergeFrom(CodedInputStream,ExtensionRegistryLite)}. 260 * 261 * @return this 262 */ mergeFrom(InputStream input, ExtensionRegistryLite extensionRegistry)263 Builder mergeFrom(InputStream input, ExtensionRegistryLite extensionRegistry) 264 throws IOException; 265 266 /** 267 * Merge {@code other} into the message being built. {@code other} must have the exact same type 268 * as {@code this} (i.e. {@code getClass().equals(getDefaultInstanceForType().getClass())}). 269 * 270 * <p>Merging occurs as follows. For each field:<br> 271 * * For singular primitive fields, if the field is set in {@code other}, then {@code other}'s 272 * value overwrites the value in this message.<br> 273 * * For singular message fields, if the field is set in {@code other}, it is merged into the 274 * corresponding sub-message of this message using the same merging rules.<br> 275 * * For repeated fields, the elements in {@code other} are concatenated with the elements in 276 * this message. * For oneof groups, if the other message has one of the fields set, the group 277 * of this message is cleared and replaced by the field of the other message, so that the oneof 278 * constraint is preserved. 279 * 280 * <p>This is equivalent to the {@code Message::MergeFrom} method in C++. 281 */ mergeFrom(MessageLite other)282 Builder mergeFrom(MessageLite other); 283 284 /** 285 * Like {@link #mergeFrom(InputStream)}, but does not read until EOF. Instead, the size of the 286 * message (encoded as a varint) is read first, then the message data. Use {@link 287 * MessageLite#writeDelimitedTo(OutputStream)} to write messages in this format. 288 * 289 * @return True if successful, or false if the stream is at EOF when the method starts. Any 290 * other error (including reaching EOF during parsing) will cause an exception to be thrown. 291 */ mergeDelimitedFrom(InputStream input)292 boolean mergeDelimitedFrom(InputStream input) throws IOException; 293 294 /** Like {@link #mergeDelimitedFrom(InputStream)} but supporting extensions. */ mergeDelimitedFrom(InputStream input, ExtensionRegistryLite extensionRegistry)295 boolean mergeDelimitedFrom(InputStream input, ExtensionRegistryLite extensionRegistry) 296 throws IOException; 297 } 298 } 299