PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://mmorpg-client-server-learning.googlecode.com/
Java | 163 lines | 87 code | 18 blank | 58 comment | 0 complexity | 970c43977efc0c96557556337c4244f9 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. package com.google.protobuf;
  31. /**
  32. * This class is used internally by the Protocol Buffer library and generated
  33. * message implementations. It is public only because those generated messages
  34. * do not reside in the {@code protobuf} package. Others should not use this
  35. * class directly.
  36. *
  37. * This class contains constants and helper functions useful for dealing with
  38. * the Protocol Buffer wire format.
  39. *
  40. * @author kenton@google.com Kenton Varda
  41. */
  42. public final class WireFormat {
  43. // Do not allow instantiation.
  44. private WireFormat() {}
  45. public static final int WIRETYPE_VARINT = 0;
  46. public static final int WIRETYPE_FIXED64 = 1;
  47. public static final int WIRETYPE_LENGTH_DELIMITED = 2;
  48. public static final int WIRETYPE_START_GROUP = 3;
  49. public static final int WIRETYPE_END_GROUP = 4;
  50. public static final int WIRETYPE_FIXED32 = 5;
  51. static final int TAG_TYPE_BITS = 3;
  52. static final int TAG_TYPE_MASK = (1 << TAG_TYPE_BITS) - 1;
  53. /** Given a tag value, determines the wire type (the lower 3 bits). */
  54. static int getTagWireType(final int tag) {
  55. return tag & TAG_TYPE_MASK;
  56. }
  57. /** Given a tag value, determines the field number (the upper 29 bits). */
  58. public static int getTagFieldNumber(final int tag) {
  59. return tag >>> TAG_TYPE_BITS;
  60. }
  61. /** Makes a tag value given a field number and wire type. */
  62. static int makeTag(final int fieldNumber, final int wireType) {
  63. return (fieldNumber << TAG_TYPE_BITS) | wireType;
  64. }
  65. /**
  66. * Lite equivalent to {@link com.google.protobuf.Descriptors.FieldDescriptor.JavaType}. This is
  67. * only here to support the lite runtime and should not be used by users.
  68. */
  69. public enum JavaType {
  70. INT(0),
  71. LONG(0L),
  72. FLOAT(0F),
  73. DOUBLE(0D),
  74. BOOLEAN(false),
  75. STRING(""),
  76. BYTE_STRING(ByteString.EMPTY),
  77. ENUM(null),
  78. MESSAGE(null);
  79. JavaType(final Object defaultDefault) {
  80. this.defaultDefault = defaultDefault;
  81. }
  82. /**
  83. * The default default value for fields of this type, if it's a primitive
  84. * type.
  85. */
  86. Object getDefaultDefault() {
  87. return defaultDefault;
  88. }
  89. private final Object defaultDefault;
  90. }
  91. /**
  92. * Lite equivalent to {@link com.google.protobuf.Descriptors.FieldDescriptor.Type}. This is
  93. * only here to support the lite runtime and should not be used by users.
  94. */
  95. public enum FieldType {
  96. DOUBLE (JavaType.DOUBLE , WIRETYPE_FIXED64 ),
  97. FLOAT (JavaType.FLOAT , WIRETYPE_FIXED32 ),
  98. INT64 (JavaType.LONG , WIRETYPE_VARINT ),
  99. UINT64 (JavaType.LONG , WIRETYPE_VARINT ),
  100. INT32 (JavaType.INT , WIRETYPE_VARINT ),
  101. FIXED64 (JavaType.LONG , WIRETYPE_FIXED64 ),
  102. FIXED32 (JavaType.INT , WIRETYPE_FIXED32 ),
  103. BOOL (JavaType.BOOLEAN , WIRETYPE_VARINT ),
  104. STRING (JavaType.STRING , WIRETYPE_LENGTH_DELIMITED) {
  105. public boolean isPackable() { return false; }
  106. },
  107. GROUP (JavaType.MESSAGE , WIRETYPE_START_GROUP ) {
  108. public boolean isPackable() { return false; }
  109. },
  110. MESSAGE (JavaType.MESSAGE , WIRETYPE_LENGTH_DELIMITED) {
  111. public boolean isPackable() { return false; }
  112. },
  113. BYTES (JavaType.BYTE_STRING, WIRETYPE_LENGTH_DELIMITED) {
  114. public boolean isPackable() { return false; }
  115. },
  116. UINT32 (JavaType.INT , WIRETYPE_VARINT ),
  117. ENUM (JavaType.ENUM , WIRETYPE_VARINT ),
  118. SFIXED32(JavaType.INT , WIRETYPE_FIXED32 ),
  119. SFIXED64(JavaType.LONG , WIRETYPE_FIXED64 ),
  120. SINT32 (JavaType.INT , WIRETYPE_VARINT ),
  121. SINT64 (JavaType.LONG , WIRETYPE_VARINT );
  122. FieldType(final JavaType javaType, final int wireType) {
  123. this.javaType = javaType;
  124. this.wireType = wireType;
  125. }
  126. private final JavaType javaType;
  127. private final int wireType;
  128. public JavaType getJavaType() { return javaType; }
  129. public int getWireType() { return wireType; }
  130. public boolean isPackable() { return true; }
  131. }
  132. // Field numbers for feilds in MessageSet wire format.
  133. static final int MESSAGE_SET_ITEM = 1;
  134. static final int MESSAGE_SET_TYPE_ID = 2;
  135. static final int MESSAGE_SET_MESSAGE = 3;
  136. // Tag numbers.
  137. static final int MESSAGE_SET_ITEM_TAG =
  138. makeTag(MESSAGE_SET_ITEM, WIRETYPE_START_GROUP);
  139. static final int MESSAGE_SET_ITEM_END_TAG =
  140. makeTag(MESSAGE_SET_ITEM, WIRETYPE_END_GROUP);
  141. static final int MESSAGE_SET_TYPE_ID_TAG =
  142. makeTag(MESSAGE_SET_TYPE_ID, WIRETYPE_VARINT);
  143. static final int MESSAGE_SET_MESSAGE_TAG =
  144. makeTag(MESSAGE_SET_MESSAGE, WIRETYPE_LENGTH_DELIMITED);
  145. }