PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/game_server/src/com/google/protobuf/MessageLite.java

http://mmorpg-client-server-learning.googlecode.com/
Java | 325 lines | 46 code | 32 blank | 247 comment | 0 complexity | bc483eec5b6489fb83ff725421011ffb MD5 | raw file
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  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. // TODO(kenton): Use generics? E.g. Builder<BuilderType extends Builder>, then
  31. // mergeFrom*() could return BuilderType for better type-safety.
  32. package com.google.protobuf;
  33. import java.io.IOException;
  34. import java.io.InputStream;
  35. import java.io.OutputStream;
  36. /**
  37. * Abstract interface implemented by Protocol Message objects.
  38. *
  39. * <p>This interface is implemented by all protocol message objects. Non-lite
  40. * messages additionally implement the Message interface, which is a subclass
  41. * of MessageLite. Use MessageLite instead when you only need the subset of
  42. * features which it supports -- namely, nothing that uses descriptors or
  43. * reflection. You can instruct the protocol compiler to generate classes
  44. * which implement only MessageLite, not the full Message interface, by adding
  45. * the follow line to the .proto file:
  46. * <pre>
  47. * option optimize_for = LITE_RUNTIME;
  48. * </pre>
  49. *
  50. * <p>This is particularly useful on resource-constrained systems where the
  51. * full protocol buffers runtime library is too big.
  52. *
  53. * <p>Note that on non-constrained systems (e.g. servers) when you need to link
  54. * in lots of protocol definitions, a better way to reduce total code footprint
  55. * is to use {@code optimize_for = CODE_SIZE}. This will make the generated
  56. * code smaller while still supporting all the same features (at the expense of
  57. * speed). {@code optimize_for = LITE_RUNTIME} is best when you only have a
  58. * small number of message types linked into your binary, in which case the
  59. * size of the protocol buffers runtime itself is the biggest problem.
  60. *
  61. * @author kenton@google.com Kenton Varda
  62. */
  63. public interface MessageLite extends MessageLiteOrBuilder {
  64. /**
  65. * Serializes the message and writes it to {@code output}. This does not
  66. * flush or close the stream.
  67. */
  68. void writeTo(CodedOutputStream output) throws IOException;
  69. /**
  70. * Get the number of bytes required to encode this message. The result
  71. * is only computed on the first call and memoized after that.
  72. */
  73. int getSerializedSize();
  74. // -----------------------------------------------------------------
  75. // Convenience methods.
  76. /**
  77. * Serializes the message to a {@code ByteString} and returns it. This is
  78. * just a trivial wrapper around
  79. * {@link #writeTo(CodedOutputStream)}.
  80. */
  81. ByteString toByteString();
  82. /**
  83. * Serializes the message to a {@code byte} array and returns it. This is
  84. * just a trivial wrapper around
  85. * {@link #writeTo(CodedOutputStream)}.
  86. */
  87. byte[] toByteArray();
  88. /**
  89. * Serializes the message and writes it to {@code output}. This is just a
  90. * trivial wrapper around {@link #writeTo(CodedOutputStream)}. This does
  91. * not flush or close the stream.
  92. * <p>
  93. * NOTE: Protocol Buffers are not self-delimiting. Therefore, if you write
  94. * any more data to the stream after the message, you must somehow ensure
  95. * that the parser on the receiving end does not interpret this as being
  96. * part of the protocol message. This can be done e.g. by writing the size
  97. * of the message before the data, then making sure to limit the input to
  98. * that size on the receiving end (e.g. by wrapping the InputStream in one
  99. * which limits the input). Alternatively, just use
  100. * {@link #writeDelimitedTo(java.io.OutputStream)}.
  101. */
  102. void writeTo(OutputStream output) throws IOException;
  103. /**
  104. * Like {@link #writeTo(java.io.OutputStream)}, but writes the size of the message
  105. * as a varint before writing the data. This allows more data to be written
  106. * to the stream after the message without the need to delimit the message
  107. * data yourself. Use {@link com.google.protobuf.MessageLite.Builder#mergeDelimitedFrom(java.io.InputStream)} (or
  108. * the static method {@code YourMessageType.parseDelimitedFrom(InputStream)})
  109. * to parse messages written by this method.
  110. */
  111. void writeDelimitedTo(OutputStream output) throws IOException;
  112. // =================================================================
  113. // Builders
  114. /**
  115. * Constructs a new builder for a message of the same type as this message.
  116. */
  117. Builder newBuilderForType();
  118. /**
  119. * Constructs a builder initialized with the current message. Use this to
  120. * derive a new message from the current one.
  121. */
  122. Builder toBuilder();
  123. /**
  124. * Abstract interface implemented by Protocol Message builders.
  125. */
  126. interface Builder extends MessageLiteOrBuilder, Cloneable {
  127. /** Resets all fields to their default values. */
  128. Builder clear();
  129. /**
  130. * Construct the final message. Once this is called, the Builder is no
  131. * longer valid, and calling any other method will result in undefined
  132. * behavior and may throw a NullPointerException. If you need to continue
  133. * working with the builder after calling {@code build()}, {@code clone()}
  134. * it first.
  135. * @throws com.google.protobuf.UninitializedMessageException The message is missing one or more
  136. * required fields (i.e. {@link #isInitialized()} returns false).
  137. * Use {@link #buildPartial()} to bypass this check.
  138. */
  139. MessageLite build();
  140. /**
  141. * Like {@link #build()}, but does not throw an exception if the message
  142. * is missing required fields. Instead, a partial message is returned.
  143. * Once this is called, the Builder is no longer valid, and calling any
  144. * will result in undefined behavior and may throw a NullPointerException.
  145. *
  146. * If you need to continue working with the builder after calling
  147. * {@code buildPartial()}, {@code clone()} it first.
  148. */
  149. MessageLite buildPartial();
  150. /**
  151. * Clones the Builder.
  152. * @see Object#clone()
  153. */
  154. Builder clone();
  155. /**
  156. * Parses a message of this type from the input and merges it with this
  157. * message, as if using {@link com.google.protobuf.MessageLite.Builder#mergeFrom(MessageLite)}.
  158. *
  159. * <p>Warning: This does not verify that all required fields are present in
  160. * the input message. If you call {@link #build()} without setting all
  161. * required fields, it will throw an {@link com.google.protobuf.UninitializedMessageException},
  162. * which is a {@code RuntimeException} and thus might not be caught. There
  163. * are a few good ways to deal with this:
  164. * <ul>
  165. * <li>Call {@link #isInitialized()} to verify that all required fields
  166. * are set before building.
  167. * <li>Parse the message separately using one of the static
  168. * {@code parseFrom} methods, then use {@link #mergeFrom(MessageLite)}
  169. * to merge it with this one. {@code parseFrom} will throw an
  170. * {@link InvalidProtocolBufferException} (an {@code IOException})
  171. * if some required fields are missing.
  172. * <li>Use {@code buildPartial()} to build, which ignores missing
  173. * required fields.
  174. * </ul>
  175. *
  176. * <p>Note: The caller should call
  177. * {@link CodedInputStream#checkLastTagWas(int)} after calling this to
  178. * verify that the last tag seen was the appropriate end-group tag,
  179. * or zero for EOF.
  180. */
  181. Builder mergeFrom(CodedInputStream input) throws IOException;
  182. /**
  183. * Like {@link com.google.protobuf.MessageLite.Builder#mergeFrom(CodedInputStream)}, but also
  184. * parses extensions. The extensions that you want to be able to parse
  185. * must be registered in {@code extensionRegistry}. Extensions not in
  186. * the registry will be treated as unknown fields.
  187. */
  188. Builder mergeFrom(CodedInputStream input,
  189. ExtensionRegistryLite extensionRegistry)
  190. throws IOException;
  191. // ---------------------------------------------------------------
  192. // Convenience methods.
  193. /**
  194. * Parse {@code data} as a message of this type and merge it with the
  195. * message being built. This is just a small wrapper around
  196. * {@link #mergeFrom(CodedInputStream)}.
  197. *
  198. * @return this
  199. */
  200. Builder mergeFrom(ByteString data) throws InvalidProtocolBufferException;
  201. /**
  202. * Parse {@code data} as a message of this type and merge it with the
  203. * message being built. This is just a small wrapper around
  204. * {@link #mergeFrom(CodedInputStream, ExtensionRegistry)}.
  205. *
  206. * @return this
  207. */
  208. Builder mergeFrom(ByteString data,
  209. ExtensionRegistryLite extensionRegistry)
  210. throws InvalidProtocolBufferException;
  211. /**
  212. * Parse {@code data} as a message of this type and merge it with the
  213. * message being built. This is just a small wrapper around
  214. * {@link #mergeFrom(CodedInputStream)}.
  215. *
  216. * @return this
  217. */
  218. Builder mergeFrom(byte[] data) throws InvalidProtocolBufferException;
  219. /**
  220. * Parse {@code data} as a message of this type and merge it with the
  221. * message being built. This is just a small wrapper around
  222. * {@link #mergeFrom(CodedInputStream)}.
  223. *
  224. * @return this
  225. */
  226. Builder mergeFrom(byte[] data, int off, int len)
  227. throws InvalidProtocolBufferException;
  228. /**
  229. * Parse {@code data} as a message of this type and merge it with the
  230. * message being built. This is just a small wrapper around
  231. * {@link #mergeFrom(CodedInputStream, ExtensionRegistry)}.
  232. *
  233. * @return this
  234. */
  235. Builder mergeFrom(byte[] data,
  236. ExtensionRegistryLite extensionRegistry)
  237. throws InvalidProtocolBufferException;
  238. /**
  239. * Parse {@code data} as a message of this type and merge it with the
  240. * message being built. This is just a small wrapper around
  241. * {@link #mergeFrom(CodedInputStream, ExtensionRegistry)}.
  242. *
  243. * @return this
  244. */
  245. Builder mergeFrom(byte[] data, int off, int len,
  246. ExtensionRegistryLite extensionRegistry)
  247. throws InvalidProtocolBufferException;
  248. /**
  249. * Parse a message of this type from {@code input} and merge it with the
  250. * message being built. This is just a small wrapper around
  251. * {@link #mergeFrom(CodedInputStream)}. Note that this method always
  252. * reads the <i>entire</i> input (unless it throws an exception). If you
  253. * want it to stop earlier, you will need to wrap your input in some
  254. * wrapper stream that limits reading. Or, use
  255. * {@link MessageLite#writeDelimitedTo(java.io.OutputStream)} to write your message
  256. * and {@link #mergeDelimitedFrom(java.io.InputStream)} to read it.
  257. * <p>
  258. * Despite usually reading the entire input, this does not close the stream.
  259. *
  260. * @return this
  261. */
  262. Builder mergeFrom(InputStream input) throws IOException;
  263. /**
  264. * Parse a message of this type from {@code input} and merge it with the
  265. * message being built. This is just a small wrapper around
  266. * {@link #mergeFrom(CodedInputStream, ExtensionRegistry)}.
  267. *
  268. * @return this
  269. */
  270. Builder mergeFrom(InputStream input,
  271. ExtensionRegistryLite extensionRegistry)
  272. throws IOException;
  273. /**
  274. * Like {@link #mergeFrom(java.io.InputStream)}, but does not read until EOF.
  275. * Instead, the size of the message (encoded as a varint) is read first,
  276. * then the message data. Use
  277. * {@link MessageLite#writeDelimitedTo(java.io.OutputStream)} to write messages in
  278. * this format.
  279. *
  280. * @returns True if successful, or false if the stream is at EOF when the
  281. * method starts. Any other error (including reaching EOF during
  282. * parsing) will cause an exception to be thrown.
  283. */
  284. boolean mergeDelimitedFrom(InputStream input)
  285. throws IOException;
  286. /**
  287. * Like {@link #mergeDelimitedFrom(java.io.InputStream)} but supporting extensions.
  288. */
  289. boolean mergeDelimitedFrom(InputStream input,
  290. ExtensionRegistryLite extensionRegistry)
  291. throws IOException;
  292. }
  293. }