WireFormat.java
Go to the documentation of this file.
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 
45 public final class WireFormat {
46  // Do not allow instantiation.
47  private WireFormat() {}
48 
49  static final int FIXED32_SIZE = 4;
50  static final int FIXED64_SIZE = 8;
51  static final int MAX_VARINT32_SIZE = 5;
52  static final int MAX_VARINT64_SIZE = 10;
53  static final int MAX_VARINT_SIZE = 10;
54 
55  public static final int WIRETYPE_VARINT = 0;
56  public static final int WIRETYPE_FIXED64 = 1;
57  public static final int WIRETYPE_LENGTH_DELIMITED = 2;
58  public static final int WIRETYPE_START_GROUP = 3;
59  public static final int WIRETYPE_END_GROUP = 4;
60  public static final int WIRETYPE_FIXED32 = 5;
61 
62  static final int TAG_TYPE_BITS = 3;
63  static final int TAG_TYPE_MASK = (1 << TAG_TYPE_BITS) - 1;
64 
66  public static int getTagWireType(final int tag) {
67  return tag & TAG_TYPE_MASK;
68  }
69 
71  public static int getTagFieldNumber(final int tag) {
72  return tag >>> TAG_TYPE_BITS;
73  }
74 
76  static int makeTag(final int fieldNumber, final int wireType) {
77  return (fieldNumber << TAG_TYPE_BITS) | wireType;
78  }
79 
84  public enum JavaType {
85  INT(0),
86  LONG(0L),
87  FLOAT(0F),
88  DOUBLE(0D),
89  BOOLEAN(false),
90  STRING(""),
92  ENUM(null),
93  MESSAGE(null);
94 
95  JavaType(final Object defaultDefault) {
96  this.defaultDefault = defaultDefault;
97  }
98 
100  Object getDefaultDefault() {
101  return defaultDefault;
102  }
103 
104  private final Object defaultDefault;
105  }
106 
111  public enum FieldType {
121  @Override
122  public boolean isPackable() {
123  return false;
124  }
125  },
127  @Override
128  public boolean isPackable() {
129  return false;
130  }
131  },
133  @Override
134  public boolean isPackable() {
135  return false;
136  }
137  },
139  @Override
140  public boolean isPackable() {
141  return false;
142  }
143  },
150 
151  FieldType(final JavaType javaType, final int wireType) {
152  this.javaType = javaType;
153  this.wireType = wireType;
154  }
155 
156  private final JavaType javaType;
157  private final int wireType;
158 
160  return javaType;
161  }
162 
163  public int getWireType() {
164  return wireType;
165  }
166 
167  public boolean isPackable() {
168  return true;
169  }
170  }
171 
172  // Field numbers for fields in MessageSet wire format.
173  static final int MESSAGE_SET_ITEM = 1;
174  static final int MESSAGE_SET_TYPE_ID = 2;
175  static final int MESSAGE_SET_MESSAGE = 3;
176 
177  // Tag numbers.
178  static final int MESSAGE_SET_ITEM_TAG = makeTag(MESSAGE_SET_ITEM, WIRETYPE_START_GROUP);
179  static final int MESSAGE_SET_ITEM_END_TAG = makeTag(MESSAGE_SET_ITEM, WIRETYPE_END_GROUP);
180  static final int MESSAGE_SET_TYPE_ID_TAG = makeTag(MESSAGE_SET_TYPE_ID, WIRETYPE_VARINT);
181  static final int MESSAGE_SET_MESSAGE_TAG =
182  makeTag(MESSAGE_SET_MESSAGE, WIRETYPE_LENGTH_DELIMITED);
183 
188  enum Utf8Validation {
190  LOOSE {
191  @Override
192  Object readString(CodedInputStream input) throws IOException {
193  return input.readString();
194  }
195  },
197  STRICT {
198  @Override
199  Object readString(CodedInputStream input) throws IOException {
200  return input.readStringRequireUtf8();
201  }
202  },
204  LAZY {
205  @Override
206  Object readString(CodedInputStream input) throws IOException {
207  return input.readBytes();
208  }
209  };
210 
212  abstract Object readString(CodedInputStream input) throws IOException;
213  }
214 
225  static Object readPrimitiveField(
226  CodedInputStream input, FieldType type, Utf8Validation utf8Validation) throws IOException {
227  switch (type) {
228  case DOUBLE:
229  return input.readDouble();
230  case FLOAT:
231  return input.readFloat();
232  case INT64:
233  return input.readInt64();
234  case UINT64:
235  return input.readUInt64();
236  case INT32:
237  return input.readInt32();
238  case FIXED64:
239  return input.readFixed64();
240  case FIXED32:
241  return input.readFixed32();
242  case BOOL:
243  return input.readBool();
244  case BYTES:
245  return input.readBytes();
246  case UINT32:
247  return input.readUInt32();
248  case SFIXED32:
249  return input.readSFixed32();
250  case SFIXED64:
251  return input.readSFixed64();
252  case SINT32:
253  return input.readSInt32();
254  case SINT64:
255  return input.readSInt64();
256 
257  case STRING:
258  return utf8Validation.readString(input);
259  case GROUP:
260  throw new IllegalArgumentException("readPrimitiveField() cannot handle nested groups.");
261  case MESSAGE:
262  throw new IllegalArgumentException("readPrimitiveField() cannot handle embedded messages.");
263  case ENUM:
264  // We don't handle enums because we don't know what to do if the
265  // value is not recognized.
266  throw new IllegalArgumentException("readPrimitiveField() cannot handle enums.");
267  }
268 
269  throw new RuntimeException("There is no way to get here, but the compiler thinks otherwise.");
270  }
271 }
com.google.protobuf.WireFormat.WIRETYPE_VARINT
static final int WIRETYPE_VARINT
Definition: WireFormat.java:55
com.google.protobuf.WireFormat.WireFormat
WireFormat()
Definition: WireFormat.java:47
com.google.protobuf.WireFormat.FieldType.BOOL
BOOL
Definition: WireFormat.java:119
com.google.protobuf.WireFormat.JavaType.INT
INT
Definition: WireFormat.java:85
com.google.protobuf.WireFormat.JavaType.MESSAGE
MESSAGE
Definition: WireFormat.java:93
input
std::string input
Definition: tokenizer_unittest.cc:197
com.google.protobuf.WireFormat.FieldType.UINT32
UINT32
Definition: WireFormat.java:144
com.google.protobuf.WireFormat.WIRETYPE_FIXED64
static final int WIRETYPE_FIXED64
Definition: WireFormat.java:56
com.google.protobuf.WireFormat.FieldType.SFIXED32
SFIXED32
Definition: WireFormat.java:146
com.google.protobuf.ByteString.EMPTY
static final ByteString EMPTY
Definition: ByteString.java:85
com.google.protobuf.WireFormat
Definition: WireFormat.java:45
com.google.protobuf.WireFormat.FieldType.isPackable
boolean isPackable()
Definition: WireFormat.java:167
com.google.protobuf.WireFormat.JavaType.getDefaultDefault
Object getDefaultDefault()
Definition: WireFormat.java:100
com.google.protobuf.WireFormat.JavaType.BOOLEAN
BOOLEAN
Definition: WireFormat.java:89
com.google.protobuf.WireFormat.JavaType.ENUM
ENUM
Definition: WireFormat.java:92
com.google.protobuf.CodedInputStream
Definition: CodedInputStream.java:61
com.google.protobuf.WireFormat.JavaType.DOUBLE
DOUBLE
Definition: WireFormat.java:88
com.google.protobuf.WireFormat.FieldType.SFIXED64
SFIXED64
Definition: WireFormat.java:147
google::protobuf::compiler::cpp::STRICT
@ STRICT
Definition: cpp_helpers.cc:981
com.google.protobuf.WireFormat.FieldType.FLOAT
FLOAT
Definition: WireFormat.java:113
com.google.protobuf.WireFormat.FieldType.FIXED32
FIXED32
Definition: WireFormat.java:118
com.google.protobuf.WireFormat.FieldType.javaType
final JavaType javaType
Definition: WireFormat.java:156
F
#define F(msg, field)
Definition: ruby/ext/google/protobuf_c/upb.c:9347
com.google.protobuf.WireFormat.FieldType.DOUBLE
DOUBLE
Definition: WireFormat.java:112
com.google.protobuf.WireFormat.WIRETYPE_END_GROUP
static final int WIRETYPE_END_GROUP
Definition: WireFormat.java:59
com.google.protobuf.WireFormat.JavaType.LONG
LONG
Definition: WireFormat.java:86
com.google.protobuf.WireFormat.WIRETYPE_FIXED32
static final int WIRETYPE_FIXED32
Definition: WireFormat.java:60
com.google.protobuf.WireFormat.WIRETYPE_START_GROUP
static final int WIRETYPE_START_GROUP
Definition: WireFormat.java:58
com.google.protobuf.WireFormat.JavaType.FLOAT
FLOAT
Definition: WireFormat.java:87
com.google.protobuf.WireFormat.JavaType.JavaType
JavaType(final Object defaultDefault)
Definition: WireFormat.java:95
java
type
GLenum type
Definition: glcorearb.h:2695
com.google.protobuf.WireFormat.FieldType.SINT64
SINT64
Definition: WireFormat.java:149
com.google.protobuf.WireFormat.WIRETYPE_LENGTH_DELIMITED
static final int WIRETYPE_LENGTH_DELIMITED
Definition: WireFormat.java:57
com.google.protobuf.WireFormat.FieldType.INT32
INT32
Definition: WireFormat.java:116
com.google.protobuf.WireFormat.FieldType.getJavaType
JavaType getJavaType()
Definition: WireFormat.java:159
com.google.protobuf.WireFormat.getTagWireType
static int getTagWireType(final int tag)
Definition: WireFormat.java:66
com.google.protobuf.WireFormat.JavaType.defaultDefault
final Object defaultDefault
Definition: WireFormat.java:104
com.google.protobuf.WireFormat.FieldType.UINT64
UINT64
Definition: WireFormat.java:115
com.google.protobuf.WireFormat.FieldType.getWireType
int getWireType()
Definition: WireFormat.java:163
com.google.protobuf.WireFormat.JavaType.STRING
STRING
Definition: WireFormat.java:90
com.google.protobuf.WireFormat.JavaType.BYTE_STRING
BYTE_STRING
Definition: WireFormat.java:91
com.google.protobuf.WireFormat.FieldType.INT64
INT64
Definition: WireFormat.java:114
com.google.protobuf.WireFormat.FieldType.SINT32
SINT32
Definition: WireFormat.java:148
com.google.protobuf.WireFormat.FieldType.FIXED64
FIXED64
Definition: WireFormat.java:117
com.google.protobuf.WireFormat.getTagFieldNumber
static int getTagFieldNumber(final int tag)
Definition: WireFormat.java:71
com.google.protobuf.WireFormat.FieldType
Definition: WireFormat.java:111
com.google.protobuf.WireFormat.FieldType.wireType
final int wireType
Definition: WireFormat.java:157
google::protobuf.internal::FieldType
uint8 FieldType
Definition: extension_set.h:87
com.google.protobuf.WireFormat.FieldType.FieldType
FieldType(final JavaType javaType, final int wireType)
Definition: WireFormat.java:151
com.google.protobuf.WireFormat.FieldType.ENUM
ENUM
Definition: WireFormat.java:145
com.google.protobuf.ByteString
Definition: ByteString.java:67
com.google.protobuf.WireFormat.JavaType
Definition: WireFormat.java:84


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:07:02