108
110
* array is faster than writing to an {@code OutputStream}. See also
109
111
* {@link ByteString#newCodedBuilder}.
111
public static CodedOutputStream newInstance(byte[] flatArray, int offset,
113
public static CodedOutputStream newInstance(final byte[] flatArray,
113
116
return new CodedOutputStream(flatArray, offset, length);
116
119
// -----------------------------------------------------------------
118
121
/** Write a {@code double} field, including tag, to the stream. */
119
public void writeDouble(int fieldNumber, double value) throws IOException {
122
public void writeDouble(final int fieldNumber, final double value)
120
124
writeTag(fieldNumber, WireFormat.WIRETYPE_FIXED64);
121
125
writeDoubleNoTag(value);
124
128
/** Write a {@code float} field, including tag, to the stream. */
125
public void writeFloat(int fieldNumber, float value) throws IOException {
129
public void writeFloat(final int fieldNumber, final float value)
126
131
writeTag(fieldNumber, WireFormat.WIRETYPE_FIXED32);
127
132
writeFloatNoTag(value);
130
135
/** Write a {@code uint64} field, including tag, to the stream. */
131
public void writeUInt64(int fieldNumber, long value) throws IOException {
136
public void writeUInt64(final int fieldNumber, final long value)
132
138
writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT);
133
139
writeUInt64NoTag(value);
136
142
/** Write an {@code int64} field, including tag, to the stream. */
137
public void writeInt64(int fieldNumber, long value) throws IOException {
143
public void writeInt64(final int fieldNumber, final long value)
138
145
writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT);
139
146
writeInt64NoTag(value);
142
149
/** Write an {@code int32} field, including tag, to the stream. */
143
public void writeInt32(int fieldNumber, int value) throws IOException {
150
public void writeInt32(final int fieldNumber, final int value)
144
152
writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT);
145
153
writeInt32NoTag(value);
148
156
/** Write a {@code fixed64} field, including tag, to the stream. */
149
public void writeFixed64(int fieldNumber, long value) throws IOException {
157
public void writeFixed64(final int fieldNumber, final long value)
150
159
writeTag(fieldNumber, WireFormat.WIRETYPE_FIXED64);
151
160
writeFixed64NoTag(value);
154
163
/** Write a {@code fixed32} field, including tag, to the stream. */
155
public void writeFixed32(int fieldNumber, int value) throws IOException {
164
public void writeFixed32(final int fieldNumber, final int value)
156
166
writeTag(fieldNumber, WireFormat.WIRETYPE_FIXED32);
157
167
writeFixed32NoTag(value);
160
170
/** Write a {@code bool} field, including tag, to the stream. */
161
public void writeBool(int fieldNumber, boolean value) throws IOException {
171
public void writeBool(final int fieldNumber, final boolean value)
162
173
writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT);
163
174
writeBoolNoTag(value);
166
177
/** Write a {@code string} field, including tag, to the stream. */
167
public void writeString(int fieldNumber, String value) throws IOException {
178
public void writeString(final int fieldNumber, final String value)
168
180
writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED);
169
181
writeStringNoTag(value);
172
184
/** Write a {@code group} field, including tag, to the stream. */
173
public void writeGroup(int fieldNumber, Message value) throws IOException {
185
public void writeGroup(final int fieldNumber, final MessageLite value)
174
187
writeTag(fieldNumber, WireFormat.WIRETYPE_START_GROUP);
175
188
writeGroupNoTag(value);
176
189
writeTag(fieldNumber, WireFormat.WIRETYPE_END_GROUP);
179
/** Write a group represented by an {@link UnknownFieldSet}. */
180
public void writeUnknownGroup(int fieldNumber, UnknownFieldSet value)
182
writeTag(fieldNumber, WireFormat.WIRETYPE_START_GROUP);
183
writeUnknownGroupNoTag(value);
184
writeTag(fieldNumber, WireFormat.WIRETYPE_END_GROUP);
193
* Write a group represented by an {@link UnknownFieldSet}.
195
* @deprecated UnknownFieldSet now implements MessageLite, so you can just
196
* call {@link #writeGroup}.
199
public void writeUnknownGroup(final int fieldNumber,
200
final MessageLite value)
202
writeGroup(fieldNumber, value);
187
205
/** Write an embedded message field, including tag, to the stream. */
188
public void writeMessage(int fieldNumber, Message value) throws IOException {
206
public void writeMessage(final int fieldNumber, final MessageLite value)
189
208
writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED);
190
209
writeMessageNoTag(value);
193
212
/** Write a {@code bytes} field, including tag, to the stream. */
194
public void writeBytes(int fieldNumber, ByteString value) throws IOException {
213
public void writeBytes(final int fieldNumber, final ByteString value)
195
215
writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED);
196
216
writeBytesNoTag(value);
199
219
/** Write a {@code uint32} field, including tag, to the stream. */
200
public void writeUInt32(int fieldNumber, int value) throws IOException {
220
public void writeUInt32(final int fieldNumber, final int value)
201
222
writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT);
202
223
writeUInt32NoTag(value);
206
227
* Write an enum field, including tag, to the stream. Caller is responsible
207
228
* for converting the enum value to its numeric value.
209
public void writeEnum(int fieldNumber, int value) throws IOException {
230
public void writeEnum(final int fieldNumber, final int value)
210
232
writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT);
211
233
writeEnumNoTag(value);
214
236
/** Write an {@code sfixed32} field, including tag, to the stream. */
215
public void writeSFixed32(int fieldNumber, int value) throws IOException {
237
public void writeSFixed32(final int fieldNumber, final int value)
216
239
writeTag(fieldNumber, WireFormat.WIRETYPE_FIXED32);
217
240
writeSFixed32NoTag(value);
220
243
/** Write an {@code sfixed64} field, including tag, to the stream. */
221
public void writeSFixed64(int fieldNumber, long value) throws IOException {
244
public void writeSFixed64(final int fieldNumber, final long value)
222
246
writeTag(fieldNumber, WireFormat.WIRETYPE_FIXED64);
223
247
writeSFixed64NoTag(value);
226
250
/** Write an {@code sint32} field, including tag, to the stream. */
227
public void writeSInt32(int fieldNumber, int value) throws IOException {
251
public void writeSInt32(final int fieldNumber, final int value)
228
253
writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT);
229
254
writeSInt32NoTag(value);
232
257
/** Write an {@code sint64} field, including tag, to the stream. */
233
public void writeSInt64(int fieldNumber, long value) throws IOException {
258
public void writeSInt64(final int fieldNumber, final long value)
234
260
writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT);
235
261
writeSInt64NoTag(value);
259
287
writeTag(WireFormat.MESSAGE_SET_ITEM, WireFormat.WIRETYPE_END_GROUP);
263
* Write a field of arbitrary type, including tag, to the stream.
265
* @param type The field's type.
266
* @param number The field's number.
267
* @param value Object representing the field's value. Must be of the exact
268
* type which would be returned by
269
* {@link Message#getField(Descriptors.FieldDescriptor)} for
272
public void writeField(Descriptors.FieldDescriptor.Type type,
274
Object value) throws IOException {
275
// Special case for groups, which need a start and end tag; other fields
276
// can just use writeTag() and writeFieldNoTag().
277
if (type == Descriptors.FieldDescriptor.Type.GROUP) {
278
writeGroup(number, (Message) value);
280
writeTag(number, WireFormat.getWireFormatForFieldType(type));
281
writeFieldNoTag(type, value);
286
* Write a field of arbitrary type, without its tag, to the stream.
288
* @param type The field's type.
289
* @param value Object representing the field's value. Must be of the exact
290
* type which would be returned by
291
* {@link Message#getField(Descriptors.FieldDescriptor)} for
294
public void writeFieldNoTag(Descriptors.FieldDescriptor.Type type,
295
Object value) throws IOException {
297
case DOUBLE : writeDoubleNoTag ((Double ) value); break;
298
case FLOAT : writeFloatNoTag ((Float ) value); break;
299
case INT64 : writeInt64NoTag ((Long ) value); break;
300
case UINT64 : writeUInt64NoTag ((Long ) value); break;
301
case INT32 : writeInt32NoTag ((Integer ) value); break;
302
case FIXED64 : writeFixed64NoTag ((Long ) value); break;
303
case FIXED32 : writeFixed32NoTag ((Integer ) value); break;
304
case BOOL : writeBoolNoTag ((Boolean ) value); break;
305
case STRING : writeStringNoTag ((String ) value); break;
306
case GROUP : writeGroupNoTag ((Message ) value); break;
307
case MESSAGE : writeMessageNoTag ((Message ) value); break;
308
case BYTES : writeBytesNoTag ((ByteString) value); break;
309
case UINT32 : writeUInt32NoTag ((Integer ) value); break;
310
case SFIXED32: writeSFixed32NoTag((Integer ) value); break;
311
case SFIXED64: writeSFixed64NoTag((Long ) value); break;
312
case SINT32 : writeSInt32NoTag ((Integer ) value); break;
313
case SINT64 : writeSInt64NoTag ((Long ) value); break;
316
writeEnumNoTag(((Descriptors.EnumValueDescriptor) value).getNumber());
321
290
// -----------------------------------------------------------------
323
292
/** Write a {@code double} field to the stream. */
324
public void writeDoubleNoTag(double value) throws IOException {
293
public void writeDoubleNoTag(final double value) throws IOException {
325
294
writeRawLittleEndian64(Double.doubleToRawLongBits(value));
328
297
/** Write a {@code float} field to the stream. */
329
public void writeFloatNoTag(float value) throws IOException {
298
public void writeFloatNoTag(final float value) throws IOException {
330
299
writeRawLittleEndian32(Float.floatToRawIntBits(value));
333
302
/** Write a {@code uint64} field to the stream. */
334
public void writeUInt64NoTag(long value) throws IOException {
303
public void writeUInt64NoTag(final long value) throws IOException {
335
304
writeRawVarint64(value);
338
307
/** Write an {@code int64} field to the stream. */
339
public void writeInt64NoTag(long value) throws IOException {
308
public void writeInt64NoTag(final long value) throws IOException {
340
309
writeRawVarint64(value);
343
312
/** Write an {@code int32} field to the stream. */
344
public void writeInt32NoTag(int value) throws IOException {
313
public void writeInt32NoTag(final int value) throws IOException {
345
314
if (value >= 0) {
346
315
writeRawVarint32(value);
353
322
/** Write a {@code fixed64} field to the stream. */
354
public void writeFixed64NoTag(long value) throws IOException {
323
public void writeFixed64NoTag(final long value) throws IOException {
355
324
writeRawLittleEndian64(value);
358
327
/** Write a {@code fixed32} field to the stream. */
359
public void writeFixed32NoTag(int value) throws IOException {
328
public void writeFixed32NoTag(final int value) throws IOException {
360
329
writeRawLittleEndian32(value);
363
332
/** Write a {@code bool} field to the stream. */
364
public void writeBoolNoTag(boolean value) throws IOException {
333
public void writeBoolNoTag(final boolean value) throws IOException {
365
334
writeRawByte(value ? 1 : 0);
368
337
/** Write a {@code string} field to the stream. */
369
public void writeStringNoTag(String value) throws IOException {
338
public void writeStringNoTag(final String value) throws IOException {
370
339
// Unfortunately there does not appear to be any way to tell Java to encode
371
340
// UTF-8 directly into our buffer, so we have to let it create its own byte
372
341
// array and then copy.
373
byte[] bytes = value.getBytes("UTF-8");
342
final byte[] bytes = value.getBytes("UTF-8");
374
343
writeRawVarint32(bytes.length);
375
344
writeRawBytes(bytes);
378
347
/** Write a {@code group} field to the stream. */
379
public void writeGroupNoTag(Message value) throws IOException {
348
public void writeGroupNoTag(final MessageLite value) throws IOException {
380
349
value.writeTo(this);
383
/** Write a group represented by an {@link UnknownFieldSet}. */
384
public void writeUnknownGroupNoTag(UnknownFieldSet value)
353
* Write a group represented by an {@link UnknownFieldSet}.
355
* @deprecated UnknownFieldSet now implements MessageLite, so you can just
356
* call {@link #writeGroupNoTag}.
359
public void writeUnknownGroupNoTag(final MessageLite value)
385
360
throws IOException {
361
writeGroupNoTag(value);
389
364
/** Write an embedded message field to the stream. */
390
public void writeMessageNoTag(Message value) throws IOException {
365
public void writeMessageNoTag(final MessageLite value) throws IOException {
391
366
writeRawVarint32(value.getSerializedSize());
392
367
value.writeTo(this);
395
370
/** Write a {@code bytes} field to the stream. */
396
public void writeBytesNoTag(ByteString value) throws IOException {
397
byte[] bytes = value.toByteArray();
371
public void writeBytesNoTag(final ByteString value) throws IOException {
372
final byte[] bytes = value.toByteArray();
398
373
writeRawVarint32(bytes.length);
399
374
writeRawBytes(bytes);
402
377
/** Write a {@code uint32} field to the stream. */
403
public void writeUInt32NoTag(int value) throws IOException {
378
public void writeUInt32NoTag(final int value) throws IOException {
404
379
writeRawVarint32(value);
408
383
* Write an enum field to the stream. Caller is responsible
409
384
* for converting the enum value to its numeric value.
411
public void writeEnumNoTag(int value) throws IOException {
386
public void writeEnumNoTag(final int value) throws IOException {
412
387
writeRawVarint32(value);
415
390
/** Write an {@code sfixed32} field to the stream. */
416
public void writeSFixed32NoTag(int value) throws IOException {
391
public void writeSFixed32NoTag(final int value) throws IOException {
417
392
writeRawLittleEndian32(value);
420
395
/** Write an {@code sfixed64} field to the stream. */
421
public void writeSFixed64NoTag(long value) throws IOException {
396
public void writeSFixed64NoTag(final long value) throws IOException {
422
397
writeRawLittleEndian64(value);
425
400
/** Write an {@code sint32} field to the stream. */
426
public void writeSInt32NoTag(int value) throws IOException {
401
public void writeSInt32NoTag(final int value) throws IOException {
427
402
writeRawVarint32(encodeZigZag32(value));
430
405
/** Write an {@code sint64} field to the stream. */
431
public void writeSInt64NoTag(long value) throws IOException {
406
public void writeSInt64NoTag(final long value) throws IOException {
432
407
writeRawVarint64(encodeZigZag64(value));
518
499
* Compute the number of bytes that would be needed to encode a
519
500
* {@code group} field represented by an {@code UnknownFieldSet}, including
503
* @deprecated UnknownFieldSet now implements MessageLite, so you can just
504
* call {@link #computeGroupSize}.
522
public static int computeUnknownGroupSize(int fieldNumber,
523
UnknownFieldSet value) {
524
return computeTagSize(fieldNumber) * 2 +
525
computeUnknownGroupSizeNoTag(value);
507
public static int computeUnknownGroupSize(final int fieldNumber,
508
final MessageLite value) {
509
return computeGroupSize(fieldNumber, value);
529
513
* Compute the number of bytes that would be needed to encode an
530
514
* embedded message field, including tag.
532
public static int computeMessageSize(int fieldNumber, Message value) {
516
public static int computeMessageSize(final int fieldNumber,
517
final MessageLite value) {
533
518
return computeTagSize(fieldNumber) + computeMessageSizeNoTag(value);
778
770
* Compute the number of bytes that would be needed to encode an
779
771
* {@code sint64} field.
781
public static int computeSInt64SizeNoTag(long value) {
773
public static int computeSInt64SizeNoTag(final long value) {
782
774
return computeRawVarint64Size(encodeZigZag64(value));
786
* Compute the number of bytes that would be needed to encode a
787
* field of arbitrary type, including tag, to the stream.
789
* @param type The field's type.
790
* @param number The field's number.
791
* @param value Object representing the field's value. Must be of the exact
792
* type which would be returned by
793
* {@link Message#getField(Descriptors.FieldDescriptor)} for
796
public static int computeFieldSize(
797
Descriptors.FieldDescriptor.Type type,
798
int number, Object value) {
799
int tagSize = computeTagSize(number);
800
if (type == Descriptors.FieldDescriptor.Type.GROUP) {
803
return tagSize + computeFieldSizeNoTag(type, value);
807
* Compute the number of bytes that would be needed to encode a
808
* field of arbitrary type, excluding tag, to the stream.
810
* @param type The field's type.
811
* @param number The field's number.
812
* @param value Object representing the field's value. Must be of the exact
813
* type which would be returned by
814
* {@link Message#getField(Descriptors.FieldDescriptor)} for
817
public static int computeFieldSizeNoTag(
818
Descriptors.FieldDescriptor.Type type, Object value) {
820
case DOUBLE : return computeDoubleSizeNoTag ((Double )value);
821
case FLOAT : return computeFloatSizeNoTag ((Float )value);
822
case INT64 : return computeInt64SizeNoTag ((Long )value);
823
case UINT64 : return computeUInt64SizeNoTag ((Long )value);
824
case INT32 : return computeInt32SizeNoTag ((Integer )value);
825
case FIXED64 : return computeFixed64SizeNoTag ((Long )value);
826
case FIXED32 : return computeFixed32SizeNoTag ((Integer )value);
827
case BOOL : return computeBoolSizeNoTag ((Boolean )value);
828
case STRING : return computeStringSizeNoTag ((String )value);
829
case GROUP : return computeGroupSizeNoTag ((Message )value);
830
case MESSAGE : return computeMessageSizeNoTag ((Message )value);
831
case BYTES : return computeBytesSizeNoTag ((ByteString)value);
832
case UINT32 : return computeUInt32SizeNoTag ((Integer )value);
833
case SFIXED32: return computeSFixed32SizeNoTag((Integer )value);
834
case SFIXED64: return computeSFixed64SizeNoTag((Long )value);
835
case SINT32 : return computeSInt32SizeNoTag ((Integer )value);
836
case SINT64 : return computeSInt64SizeNoTag ((Long )value);
839
return computeEnumSizeNoTag(
840
((Descriptors.EnumValueDescriptor)value).getNumber());
843
throw new RuntimeException(
844
"There is no way to get here, but the compiler thinks otherwise.");
847
777
// =================================================================