• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package com.google.protobuf;
32 
33 import java.io.IOException;
34 
35 /**
36  * This class is used internally by the Protocol Buffer library and generated
37  * message implementations.  It is public only because those generated messages
38  * do not reside in the {@code protobuf} package.  Others should not use this
39  * class directly.
40  *
41  * This class contains constants and helper functions useful for dealing with
42  * the Protocol Buffer wire format.
43  *
44  * @author kenton@google.com Kenton Varda
45  */
46 public final class WireFormat {
47   // Do not allow instantiation.
WireFormat()48   private WireFormat() {}
49 
50   public static final int WIRETYPE_VARINT           = 0;
51   public static final int WIRETYPE_FIXED64          = 1;
52   public static final int WIRETYPE_LENGTH_DELIMITED = 2;
53   public static final int WIRETYPE_START_GROUP      = 3;
54   public static final int WIRETYPE_END_GROUP        = 4;
55   public static final int WIRETYPE_FIXED32          = 5;
56 
57   static final int TAG_TYPE_BITS = 3;
58   static final int TAG_TYPE_MASK = (1 << TAG_TYPE_BITS) - 1;
59 
60   /** Given a tag value, determines the wire type (the lower 3 bits). */
getTagWireType(final int tag)61   public static int getTagWireType(final int tag) {
62     return tag & TAG_TYPE_MASK;
63   }
64 
65   /** Given a tag value, determines the field number (the upper 29 bits). */
getTagFieldNumber(final int tag)66   public static int getTagFieldNumber(final int tag) {
67     return tag >>> TAG_TYPE_BITS;
68   }
69 
70   /** Makes a tag value given a field number and wire type. */
makeTag(final int fieldNumber, final int wireType)71   static int makeTag(final int fieldNumber, final int wireType) {
72     return (fieldNumber << TAG_TYPE_BITS) | wireType;
73   }
74 
75   /**
76    * Lite equivalent to {@link Descriptors.FieldDescriptor.JavaType}.  This is
77    * only here to support the lite runtime and should not be used by users.
78    */
79   public enum JavaType {
80     INT(0),
81     LONG(0L),
82     FLOAT(0F),
83     DOUBLE(0D),
84     BOOLEAN(false),
85     STRING(""),
86     BYTE_STRING(ByteString.EMPTY),
87     ENUM(null),
88     MESSAGE(null);
89 
JavaType(final Object defaultDefault)90     JavaType(final Object defaultDefault) {
91       this.defaultDefault = defaultDefault;
92     }
93 
94     /**
95      * The default default value for fields of this type, if it's a primitive
96      * type.
97      */
getDefaultDefault()98     Object getDefaultDefault() {
99       return defaultDefault;
100     }
101 
102     private final Object defaultDefault;
103   }
104 
105   /**
106    * Lite equivalent to {@link Descriptors.FieldDescriptor.Type}.  This is
107    * only here to support the lite runtime and should not be used by users.
108    */
109   public enum FieldType {
110     DOUBLE  (JavaType.DOUBLE     , WIRETYPE_FIXED64         ),
111     FLOAT   (JavaType.FLOAT      , WIRETYPE_FIXED32         ),
112     INT64   (JavaType.LONG       , WIRETYPE_VARINT          ),
113     UINT64  (JavaType.LONG       , WIRETYPE_VARINT          ),
114     INT32   (JavaType.INT        , WIRETYPE_VARINT          ),
115     FIXED64 (JavaType.LONG       , WIRETYPE_FIXED64         ),
116     FIXED32 (JavaType.INT        , WIRETYPE_FIXED32         ),
117     BOOL    (JavaType.BOOLEAN    , WIRETYPE_VARINT          ),
STRING(JavaType.STRING , WIRETYPE_LENGTH_DELIMITED)118     STRING  (JavaType.STRING     , WIRETYPE_LENGTH_DELIMITED) {
119       @Override
120       public boolean isPackable() {
121         return false; }
122     },
GROUP(JavaType.MESSAGE , WIRETYPE_START_GROUP )123     GROUP   (JavaType.MESSAGE    , WIRETYPE_START_GROUP     ) {
124       @Override
125       public boolean isPackable() {
126         return false; }
127     },
MESSAGE(JavaType.MESSAGE , WIRETYPE_LENGTH_DELIMITED)128     MESSAGE (JavaType.MESSAGE    , WIRETYPE_LENGTH_DELIMITED) {
129       @Override
130       public boolean isPackable() {
131         return false; }
132     },
BYTES(JavaType.BYTE_STRING, WIRETYPE_LENGTH_DELIMITED)133     BYTES   (JavaType.BYTE_STRING, WIRETYPE_LENGTH_DELIMITED) {
134       @Override
135       public boolean isPackable() {
136         return false; }
137     },
138     UINT32  (JavaType.INT        , WIRETYPE_VARINT          ),
139     ENUM    (JavaType.ENUM       , WIRETYPE_VARINT          ),
140     SFIXED32(JavaType.INT        , WIRETYPE_FIXED32         ),
141     SFIXED64(JavaType.LONG       , WIRETYPE_FIXED64         ),
142     SINT32  (JavaType.INT        , WIRETYPE_VARINT          ),
143     SINT64  (JavaType.LONG       , WIRETYPE_VARINT          );
144 
FieldType(final JavaType javaType, final int wireType)145     FieldType(final JavaType javaType, final int wireType) {
146       this.javaType = javaType;
147       this.wireType = wireType;
148     }
149 
150     private final JavaType javaType;
151     private final int wireType;
152 
getJavaType()153     public JavaType getJavaType() { return javaType; }
getWireType()154     public int getWireType() { return wireType; }
155 
isPackable()156     public boolean isPackable() { return true; }
157   }
158 
159   // Field numbers for fields in MessageSet wire format.
160   static final int MESSAGE_SET_ITEM    = 1;
161   static final int MESSAGE_SET_TYPE_ID = 2;
162   static final int MESSAGE_SET_MESSAGE = 3;
163 
164   // Tag numbers.
165   static final int MESSAGE_SET_ITEM_TAG =
166     makeTag(MESSAGE_SET_ITEM, WIRETYPE_START_GROUP);
167   static final int MESSAGE_SET_ITEM_END_TAG =
168     makeTag(MESSAGE_SET_ITEM, WIRETYPE_END_GROUP);
169   static final int MESSAGE_SET_TYPE_ID_TAG =
170     makeTag(MESSAGE_SET_TYPE_ID, WIRETYPE_VARINT);
171   static final int MESSAGE_SET_MESSAGE_TAG =
172     makeTag(MESSAGE_SET_MESSAGE, WIRETYPE_LENGTH_DELIMITED);
173 
174   /**
175    * Validation level for handling incoming string field data which potentially
176    * contain non-UTF8 bytes.
177    */
178   enum Utf8Validation {
179     /** Eagerly parses to String; silently accepts invalid UTF8 bytes. */
180     LOOSE {
181       @Override
readString(CodedInputStream input)182       Object readString(CodedInputStream input) throws IOException {
183         return input.readString();
184       }
185     },
186     /** Eagerly parses to String; throws an IOException on invalid bytes. */
187     STRICT {
188       @Override
readString(CodedInputStream input)189       Object readString(CodedInputStream input) throws IOException {
190         return input.readStringRequireUtf8();
191       }
192     },
193     /** Keep data as ByteString; validation/conversion to String is lazy. */
194     LAZY {
195       @Override
readString(CodedInputStream input)196       Object readString(CodedInputStream input) throws IOException {
197         return input.readBytes();
198       }
199     };
200 
201     /** Read a string field from the input with the proper UTF8 validation. */
readString(CodedInputStream input)202     abstract Object readString(CodedInputStream input) throws IOException;
203   }
204 
205   /**
206    * Read a field of any primitive type for immutable messages from a
207    * CodedInputStream. Enums, groups, and embedded messages are not handled by
208    * this method.
209    *
210    * @param input The stream from which to read.
211    * @param type Declared type of the field.
212    * @param utf8Validation Different string UTF8 validation level for handling
213    *                       string fields.
214    * @return An object representing the field's value, of the exact
215    *         type which would be returned by
216    *         {@link Message#getField(Descriptors.FieldDescriptor)} for
217    *         this field.
218    */
readPrimitiveField( CodedInputStream input, FieldType type, Utf8Validation utf8Validation)219   static Object readPrimitiveField(
220       CodedInputStream input,
221       FieldType type,
222       Utf8Validation utf8Validation) throws IOException {
223     switch (type) {
224       case DOUBLE  : return input.readDouble  ();
225       case FLOAT   : return input.readFloat   ();
226       case INT64   : return input.readInt64   ();
227       case UINT64  : return input.readUInt64  ();
228       case INT32   : return input.readInt32   ();
229       case FIXED64 : return input.readFixed64 ();
230       case FIXED32 : return input.readFixed32 ();
231       case BOOL    : return input.readBool    ();
232       case BYTES   : return input.readBytes   ();
233       case UINT32  : return input.readUInt32  ();
234       case SFIXED32: return input.readSFixed32();
235       case SFIXED64: return input.readSFixed64();
236       case SINT32  : return input.readSInt32  ();
237       case SINT64  : return input.readSInt64  ();
238 
239       case STRING  : return utf8Validation.readString(input);
240       case GROUP:
241         throw new IllegalArgumentException(
242           "readPrimitiveField() cannot handle nested groups.");
243       case MESSAGE:
244         throw new IllegalArgumentException(
245           "readPrimitiveField() cannot handle embedded messages.");
246       case ENUM:
247         // We don't handle enums because we don't know what to do if the
248         // value is not recognized.
249         throw new IllegalArgumentException(
250           "readPrimitiveField() cannot handle enums.");
251     }
252 
253     throw new RuntimeException(
254       "There is no way to get here, but the compiler thinks otherwise.");
255   }
256 }
257