~ubuntu-branches/ubuntu/maverick/protobuf/maverick

« back to all changes in this revision

Viewing changes to java/src/main/java/com/google/protobuf/CodedOutputStream.java

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2010-02-11 11:13:19 UTC
  • mfrom: (2.2.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100211111319-zdn8hmw0gh8s4cf8
Tags: 2.2.0a-0.1ubuntu1
* Merge from Debian testing.
* Remaining Ubuntu changes:
  - Don't use python2.4.
* Ubuntu changes dropped:
  - Disable death tests on Itanium, fixed upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
 
33
33
import java.io.OutputStream;
34
34
import java.io.IOException;
 
35
import java.io.UnsupportedEncodingException;
35
36
 
36
37
/**
37
38
 * Encodes and writes protocol message fields.
55
56
  private final OutputStream output;
56
57
 
57
58
  /**
58
 
   * The buffer size used in {@link #newInstance(java.io.OutputStream)}.
 
59
   * The buffer size used in {@link #newInstance(OutputStream)}.
59
60
   */
60
61
  public static final int DEFAULT_BUFFER_SIZE = 4096;
61
62
 
62
 
  private CodedOutputStream(byte[] buffer, int offset, int length) {
63
 
    this.output = null;
 
63
  private CodedOutputStream(final byte[] buffer, final int offset,
 
64
                            final int length) {
 
65
    output = null;
64
66
    this.buffer = buffer;
65
 
    this.position = offset;
66
 
    this.limit = offset + length;
 
67
    position = offset;
 
68
    limit = offset + length;
67
69
  }
68
70
 
69
 
  private CodedOutputStream(OutputStream output, byte[] buffer) {
 
71
  private CodedOutputStream(final OutputStream output, final byte[] buffer) {
70
72
    this.output = output;
71
73
    this.buffer = buffer;
72
 
    this.position = 0;
73
 
    this.limit = buffer.length;
 
74
    position = 0;
 
75
    limit = buffer.length;
74
76
  }
75
77
 
76
78
  /**
77
79
   * Create a new {@code CodedOutputStream} wrapping the given
78
80
   * {@code OutputStream}.
79
81
   */
80
 
  public static CodedOutputStream newInstance(OutputStream output) {
 
82
  public static CodedOutputStream newInstance(final OutputStream output) {
81
83
    return newInstance(output, DEFAULT_BUFFER_SIZE);
82
84
  }
83
85
 
85
87
   * Create a new {@code CodedOutputStream} wrapping the given
86
88
   * {@code OutputStream} with a given buffer size.
87
89
   */
88
 
  public static CodedOutputStream newInstance(OutputStream output,
89
 
      int bufferSize) {
 
90
  public static CodedOutputStream newInstance(final OutputStream output,
 
91
      final int bufferSize) {
90
92
    return new CodedOutputStream(output, new byte[bufferSize]);
91
93
  }
92
94
 
97
99
   * array is faster than writing to an {@code OutputStream}.  See also
98
100
   * {@link ByteString#newCodedBuilder}.
99
101
   */
100
 
  public static CodedOutputStream newInstance(byte[] flatArray) {
 
102
  public static CodedOutputStream newInstance(final byte[] flatArray) {
101
103
    return newInstance(flatArray, 0, flatArray.length);
102
104
  }
103
105
 
108
110
   * array is faster than writing to an {@code OutputStream}.  See also
109
111
   * {@link ByteString#newCodedBuilder}.
110
112
   */
111
 
  public static CodedOutputStream newInstance(byte[] flatArray, int offset,
112
 
      int length) {
 
113
  public static CodedOutputStream newInstance(final byte[] flatArray,
 
114
                                              final int offset,
 
115
                                              final int length) {
113
116
    return new CodedOutputStream(flatArray, offset, length);
114
117
  }
115
118
 
116
119
  // -----------------------------------------------------------------
117
120
 
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)
 
123
                          throws IOException {
120
124
    writeTag(fieldNumber, WireFormat.WIRETYPE_FIXED64);
121
125
    writeDoubleNoTag(value);
122
126
  }
123
127
 
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)
 
130
                         throws IOException {
126
131
    writeTag(fieldNumber, WireFormat.WIRETYPE_FIXED32);
127
132
    writeFloatNoTag(value);
128
133
  }
129
134
 
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)
 
137
                          throws IOException {
132
138
    writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT);
133
139
    writeUInt64NoTag(value);
134
140
  }
135
141
 
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)
 
144
                         throws IOException {
138
145
    writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT);
139
146
    writeInt64NoTag(value);
140
147
  }
141
148
 
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)
 
151
                         throws IOException {
144
152
    writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT);
145
153
    writeInt32NoTag(value);
146
154
  }
147
155
 
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)
 
158
                           throws IOException {
150
159
    writeTag(fieldNumber, WireFormat.WIRETYPE_FIXED64);
151
160
    writeFixed64NoTag(value);
152
161
  }
153
162
 
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)
 
165
                           throws IOException {
156
166
    writeTag(fieldNumber, WireFormat.WIRETYPE_FIXED32);
157
167
    writeFixed32NoTag(value);
158
168
  }
159
169
 
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)
 
172
                        throws IOException {
162
173
    writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT);
163
174
    writeBoolNoTag(value);
164
175
  }
165
176
 
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)
 
179
                          throws IOException {
168
180
    writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED);
169
181
    writeStringNoTag(value);
170
182
  }
171
183
 
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)
 
186
                         throws IOException {
174
187
    writeTag(fieldNumber, WireFormat.WIRETYPE_START_GROUP);
175
188
    writeGroupNoTag(value);
176
189
    writeTag(fieldNumber, WireFormat.WIRETYPE_END_GROUP);
177
190
  }
178
191
 
179
 
  /** Write a group represented by an {@link UnknownFieldSet}. */
180
 
  public void writeUnknownGroup(int fieldNumber, UnknownFieldSet value)
181
 
      throws IOException {
182
 
    writeTag(fieldNumber, WireFormat.WIRETYPE_START_GROUP);
183
 
    writeUnknownGroupNoTag(value);
184
 
    writeTag(fieldNumber, WireFormat.WIRETYPE_END_GROUP);
 
192
  /**
 
193
   * Write a group represented by an {@link UnknownFieldSet}.
 
194
   *
 
195
   * @deprecated UnknownFieldSet now implements MessageLite, so you can just
 
196
   *             call {@link #writeGroup}.
 
197
   */
 
198
  @Deprecated
 
199
  public void writeUnknownGroup(final int fieldNumber,
 
200
                                final MessageLite value)
 
201
                                throws IOException {
 
202
    writeGroup(fieldNumber, value);
185
203
  }
186
204
 
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)
 
207
                           throws IOException {
189
208
    writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED);
190
209
    writeMessageNoTag(value);
191
210
  }
192
211
 
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)
 
214
                         throws IOException {
195
215
    writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED);
196
216
    writeBytesNoTag(value);
197
217
  }
198
218
 
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)
 
221
                          throws IOException {
201
222
    writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT);
202
223
    writeUInt32NoTag(value);
203
224
  }
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.
208
229
   */
209
 
  public void writeEnum(int fieldNumber, int value) throws IOException {
 
230
  public void writeEnum(final int fieldNumber, final int value)
 
231
                        throws IOException {
210
232
    writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT);
211
233
    writeEnumNoTag(value);
212
234
  }
213
235
 
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)
 
238
                            throws IOException {
216
239
    writeTag(fieldNumber, WireFormat.WIRETYPE_FIXED32);
217
240
    writeSFixed32NoTag(value);
218
241
  }
219
242
 
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)
 
245
                            throws IOException {
222
246
    writeTag(fieldNumber, WireFormat.WIRETYPE_FIXED64);
223
247
    writeSFixed64NoTag(value);
224
248
  }
225
249
 
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)
 
252
                          throws IOException {
228
253
    writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT);
229
254
    writeSInt32NoTag(value);
230
255
  }
231
256
 
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)
 
259
                          throws IOException {
234
260
    writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT);
235
261
    writeSInt64NoTag(value);
236
262
  }
239
265
   * Write a MessageSet extension field to the stream.  For historical reasons,
240
266
   * the wire format differs from normal fields.
241
267
   */
242
 
  public void writeMessageSetExtension(int fieldNumber, Message value)
243
 
                                throws IOException {
 
268
  public void writeMessageSetExtension(final int fieldNumber,
 
269
                                       final MessageLite value)
 
270
                                       throws IOException {
244
271
    writeTag(WireFormat.MESSAGE_SET_ITEM, WireFormat.WIRETYPE_START_GROUP);
245
272
    writeUInt32(WireFormat.MESSAGE_SET_TYPE_ID, fieldNumber);
246
273
    writeMessage(WireFormat.MESSAGE_SET_MESSAGE, value);
251
278
   * Write an unparsed MessageSet extension field to the stream.  For
252
279
   * historical reasons, the wire format differs from normal fields.
253
280
   */
254
 
  public void writeRawMessageSetExtension(int fieldNumber, ByteString value)
 
281
  public void writeRawMessageSetExtension(final int fieldNumber,
 
282
                                          final ByteString value)
255
283
                                          throws IOException {
256
284
    writeTag(WireFormat.MESSAGE_SET_ITEM, WireFormat.WIRETYPE_START_GROUP);
257
285
    writeUInt32(WireFormat.MESSAGE_SET_TYPE_ID, fieldNumber);
259
287
    writeTag(WireFormat.MESSAGE_SET_ITEM, WireFormat.WIRETYPE_END_GROUP);
260
288
  }
261
289
 
262
 
  /**
263
 
   * Write a field of arbitrary type, including tag, to the stream.
264
 
   *
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
270
 
   *               this field.
271
 
   */
272
 
  public void writeField(Descriptors.FieldDescriptor.Type type,
273
 
                         int number,
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);
279
 
    } else {
280
 
      writeTag(number, WireFormat.getWireFormatForFieldType(type));
281
 
      writeFieldNoTag(type, value);
282
 
    }
283
 
  }
284
 
 
285
 
  /**
286
 
   * Write a field of arbitrary type, without its tag, to the stream.
287
 
   *
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
292
 
   *               this field.
293
 
   */
294
 
  public void writeFieldNoTag(Descriptors.FieldDescriptor.Type type,
295
 
                              Object value) throws IOException {
296
 
    switch (type) {
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;
314
 
 
315
 
      case ENUM:
316
 
        writeEnumNoTag(((Descriptors.EnumValueDescriptor) value).getNumber());
317
 
        break;
318
 
    }
319
 
  }
320
 
 
321
290
  // -----------------------------------------------------------------
322
291
 
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));
326
295
  }
327
296
 
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));
331
300
  }
332
301
 
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);
336
305
  }
337
306
 
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);
341
310
  }
342
311
 
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);
347
316
    } else {
351
320
  }
352
321
 
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);
356
325
  }
357
326
 
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);
361
330
  }
362
331
 
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);
366
335
  }
367
336
 
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);
376
345
  }
377
346
 
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);
381
350
  }
382
351
 
383
 
  /** Write a group represented by an {@link UnknownFieldSet}. */
384
 
  public void writeUnknownGroupNoTag(UnknownFieldSet value)
 
352
  /**
 
353
   * Write a group represented by an {@link UnknownFieldSet}.
 
354
   *
 
355
   * @deprecated UnknownFieldSet now implements MessageLite, so you can just
 
356
   *             call {@link #writeGroupNoTag}.
 
357
   */
 
358
  @Deprecated
 
359
  public void writeUnknownGroupNoTag(final MessageLite value)
385
360
      throws IOException {
386
 
    value.writeTo(this);
 
361
    writeGroupNoTag(value);
387
362
  }
388
363
 
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);
393
368
  }
394
369
 
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);
400
375
  }
401
376
 
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);
405
380
  }
406
381
 
408
383
   * Write an enum field to the stream.  Caller is responsible
409
384
   * for converting the enum value to its numeric value.
410
385
   */
411
 
  public void writeEnumNoTag(int value) throws IOException {
 
386
  public void writeEnumNoTag(final int value) throws IOException {
412
387
    writeRawVarint32(value);
413
388
  }
414
389
 
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);
418
393
  }
419
394
 
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);
423
398
  }
424
399
 
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));
428
403
  }
429
404
 
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));
433
408
  }
434
409
 
438
413
   * Compute the number of bytes that would be needed to encode a
439
414
   * {@code double} field, including tag.
440
415
   */
441
 
  public static int computeDoubleSize(int fieldNumber, double value) {
 
416
  public static int computeDoubleSize(final int fieldNumber,
 
417
                                      final double value) {
442
418
    return computeTagSize(fieldNumber) + computeDoubleSizeNoTag(value);
443
419
  }
444
420
 
446
422
   * Compute the number of bytes that would be needed to encode a
447
423
   * {@code float} field, including tag.
448
424
   */
449
 
  public static int computeFloatSize(int fieldNumber, float value) {
 
425
  public static int computeFloatSize(final int fieldNumber, final float value) {
450
426
    return computeTagSize(fieldNumber) + computeFloatSizeNoTag(value);
451
427
  }
452
428
 
454
430
   * Compute the number of bytes that would be needed to encode a
455
431
   * {@code uint64} field, including tag.
456
432
   */
457
 
  public static int computeUInt64Size(int fieldNumber, long value) {
 
433
  public static int computeUInt64Size(final int fieldNumber, final long value) {
458
434
    return computeTagSize(fieldNumber) + computeUInt64SizeNoTag(value);
459
435
  }
460
436
 
462
438
   * Compute the number of bytes that would be needed to encode an
463
439
   * {@code int64} field, including tag.
464
440
   */
465
 
  public static int computeInt64Size(int fieldNumber, long value) {
 
441
  public static int computeInt64Size(final int fieldNumber, final long value) {
466
442
    return computeTagSize(fieldNumber) + computeInt64SizeNoTag(value);
467
443
  }
468
444
 
470
446
   * Compute the number of bytes that would be needed to encode an
471
447
   * {@code int32} field, including tag.
472
448
   */
473
 
  public static int computeInt32Size(int fieldNumber, int value) {
 
449
  public static int computeInt32Size(final int fieldNumber, final int value) {
474
450
    return computeTagSize(fieldNumber) + computeInt32SizeNoTag(value);
475
451
  }
476
452
 
478
454
   * Compute the number of bytes that would be needed to encode a
479
455
   * {@code fixed64} field, including tag.
480
456
   */
481
 
  public static int computeFixed64Size(int fieldNumber, long value) {
 
457
  public static int computeFixed64Size(final int fieldNumber,
 
458
                                       final long value) {
482
459
    return computeTagSize(fieldNumber) + computeFixed64SizeNoTag(value);
483
460
  }
484
461
 
486
463
   * Compute the number of bytes that would be needed to encode a
487
464
   * {@code fixed32} field, including tag.
488
465
   */
489
 
  public static int computeFixed32Size(int fieldNumber, int value) {
 
466
  public static int computeFixed32Size(final int fieldNumber,
 
467
                                       final int value) {
490
468
    return computeTagSize(fieldNumber) + computeFixed32SizeNoTag(value);
491
469
  }
492
470
 
494
472
   * Compute the number of bytes that would be needed to encode a
495
473
   * {@code bool} field, including tag.
496
474
   */
497
 
  public static int computeBoolSize(int fieldNumber, boolean value) {
 
475
  public static int computeBoolSize(final int fieldNumber,
 
476
                                    final boolean value) {
498
477
    return computeTagSize(fieldNumber) + computeBoolSizeNoTag(value);
499
478
  }
500
479
 
502
481
   * Compute the number of bytes that would be needed to encode a
503
482
   * {@code string} field, including tag.
504
483
   */
505
 
  public static int computeStringSize(int fieldNumber, String value) {
 
484
  public static int computeStringSize(final int fieldNumber,
 
485
                                      final String value) {
506
486
    return computeTagSize(fieldNumber) + computeStringSizeNoTag(value);
507
487
  }
508
488
 
510
490
   * Compute the number of bytes that would be needed to encode a
511
491
   * {@code group} field, including tag.
512
492
   */
513
 
  public static int computeGroupSize(int fieldNumber, Message value) {
 
493
  public static int computeGroupSize(final int fieldNumber,
 
494
                                     final MessageLite value) {
514
495
    return computeTagSize(fieldNumber) * 2 + computeGroupSizeNoTag(value);
515
496
  }
516
497
 
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
520
501
   * tag.
 
502
   *
 
503
   * @deprecated UnknownFieldSet now implements MessageLite, so you can just
 
504
   *             call {@link #computeGroupSize}.
521
505
   */
522
 
  public static int computeUnknownGroupSize(int fieldNumber,
523
 
                                            UnknownFieldSet value) {
524
 
    return computeTagSize(fieldNumber) * 2 +
525
 
        computeUnknownGroupSizeNoTag(value);
 
506
  @Deprecated
 
507
  public static int computeUnknownGroupSize(final int fieldNumber,
 
508
                                            final MessageLite value) {
 
509
    return computeGroupSize(fieldNumber, value);
526
510
  }
527
511
 
528
512
  /**
529
513
   * Compute the number of bytes that would be needed to encode an
530
514
   * embedded message field, including tag.
531
515
   */
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);
534
519
  }
535
520
 
537
522
   * Compute the number of bytes that would be needed to encode a
538
523
   * {@code bytes} field, including tag.
539
524
   */
540
 
  public static int computeBytesSize(int fieldNumber, ByteString value) {
 
525
  public static int computeBytesSize(final int fieldNumber,
 
526
                                     final ByteString value) {
541
527
    return computeTagSize(fieldNumber) + computeBytesSizeNoTag(value);
542
528
  }
543
529
 
545
531
   * Compute the number of bytes that would be needed to encode a
546
532
   * {@code uint32} field, including tag.
547
533
   */
548
 
  public static int computeUInt32Size(int fieldNumber, int value) {
 
534
  public static int computeUInt32Size(final int fieldNumber, final int value) {
549
535
    return computeTagSize(fieldNumber) + computeUInt32SizeNoTag(value);
550
536
  }
551
537
 
554
540
   * enum field, including tag.  Caller is responsible for converting the
555
541
   * enum value to its numeric value.
556
542
   */
557
 
  public static int computeEnumSize(int fieldNumber, int value) {
 
543
  public static int computeEnumSize(final int fieldNumber, final int value) {
558
544
    return computeTagSize(fieldNumber) + computeEnumSizeNoTag(value);
559
545
  }
560
546
 
562
548
   * Compute the number of bytes that would be needed to encode an
563
549
   * {@code sfixed32} field, including tag.
564
550
   */
565
 
  public static int computeSFixed32Size(int fieldNumber, int value) {
 
551
  public static int computeSFixed32Size(final int fieldNumber,
 
552
                                        final int value) {
566
553
    return computeTagSize(fieldNumber) + computeSFixed32SizeNoTag(value);
567
554
  }
568
555
 
570
557
   * Compute the number of bytes that would be needed to encode an
571
558
   * {@code sfixed64} field, including tag.
572
559
   */
573
 
  public static int computeSFixed64Size(int fieldNumber, long value) {
 
560
  public static int computeSFixed64Size(final int fieldNumber,
 
561
                                        final long value) {
574
562
    return computeTagSize(fieldNumber) + computeSFixed64SizeNoTag(value);
575
563
  }
576
564
 
578
566
   * Compute the number of bytes that would be needed to encode an
579
567
   * {@code sint32} field, including tag.
580
568
   */
581
 
  public static int computeSInt32Size(int fieldNumber, int value) {
 
569
  public static int computeSInt32Size(final int fieldNumber, final int value) {
582
570
    return computeTagSize(fieldNumber) + computeSInt32SizeNoTag(value);
583
571
  }
584
572
 
586
574
   * Compute the number of bytes that would be needed to encode an
587
575
   * {@code sint64} field, including tag.
588
576
   */
589
 
  public static int computeSInt64Size(int fieldNumber, long value) {
 
577
  public static int computeSInt64Size(final int fieldNumber, final long value) {
590
578
    return computeTagSize(fieldNumber) + computeSInt64SizeNoTag(value);
591
579
  }
592
580
 
596
584
   * the wire format differs from normal fields.
597
585
   */
598
586
  public static int computeMessageSetExtensionSize(
599
 
      int fieldNumber, Message value) {
 
587
      final int fieldNumber, final MessageLite value) {
600
588
    return computeTagSize(WireFormat.MESSAGE_SET_ITEM) * 2 +
601
589
           computeUInt32Size(WireFormat.MESSAGE_SET_TYPE_ID, fieldNumber) +
602
590
           computeMessageSize(WireFormat.MESSAGE_SET_MESSAGE, value);
608
596
   * historical reasons, the wire format differs from normal fields.
609
597
   */
610
598
  public static int computeRawMessageSetExtensionSize(
611
 
      int fieldNumber, ByteString value) {
 
599
      final int fieldNumber, final ByteString value) {
612
600
    return computeTagSize(WireFormat.MESSAGE_SET_ITEM) * 2 +
613
601
           computeUInt32Size(WireFormat.MESSAGE_SET_TYPE_ID, fieldNumber) +
614
602
           computeBytesSize(WireFormat.MESSAGE_SET_MESSAGE, value);
620
608
   * Compute the number of bytes that would be needed to encode a
621
609
   * {@code double} field, including tag.
622
610
   */
623
 
  public static int computeDoubleSizeNoTag(double value) {
 
611
  public static int computeDoubleSizeNoTag(final double value) {
624
612
    return LITTLE_ENDIAN_64_SIZE;
625
613
  }
626
614
 
628
616
   * Compute the number of bytes that would be needed to encode a
629
617
   * {@code float} field, including tag.
630
618
   */
631
 
  public static int computeFloatSizeNoTag(float value) {
 
619
  public static int computeFloatSizeNoTag(final float value) {
632
620
    return LITTLE_ENDIAN_32_SIZE;
633
621
  }
634
622
 
636
624
   * Compute the number of bytes that would be needed to encode a
637
625
   * {@code uint64} field, including tag.
638
626
   */
639
 
  public static int computeUInt64SizeNoTag(long value) {
 
627
  public static int computeUInt64SizeNoTag(final long value) {
640
628
    return computeRawVarint64Size(value);
641
629
  }
642
630
 
644
632
   * Compute the number of bytes that would be needed to encode an
645
633
   * {@code int64} field, including tag.
646
634
   */
647
 
  public static int computeInt64SizeNoTag(long value) {
 
635
  public static int computeInt64SizeNoTag(final long value) {
648
636
    return computeRawVarint64Size(value);
649
637
  }
650
638
 
652
640
   * Compute the number of bytes that would be needed to encode an
653
641
   * {@code int32} field, including tag.
654
642
   */
655
 
  public static int computeInt32SizeNoTag(int value) {
 
643
  public static int computeInt32SizeNoTag(final int value) {
656
644
    if (value >= 0) {
657
645
      return computeRawVarint32Size(value);
658
646
    } else {
665
653
   * Compute the number of bytes that would be needed to encode a
666
654
   * {@code fixed64} field.
667
655
   */
668
 
  public static int computeFixed64SizeNoTag(long value) {
 
656
  public static int computeFixed64SizeNoTag(final long value) {
669
657
    return LITTLE_ENDIAN_64_SIZE;
670
658
  }
671
659
 
673
661
   * Compute the number of bytes that would be needed to encode a
674
662
   * {@code fixed32} field.
675
663
   */
676
 
  public static int computeFixed32SizeNoTag(int value) {
 
664
  public static int computeFixed32SizeNoTag(final int value) {
677
665
    return LITTLE_ENDIAN_32_SIZE;
678
666
  }
679
667
 
681
669
   * Compute the number of bytes that would be needed to encode a
682
670
   * {@code bool} field.
683
671
   */
684
 
  public static int computeBoolSizeNoTag(boolean value) {
 
672
  public static int computeBoolSizeNoTag(final boolean value) {
685
673
    return 1;
686
674
  }
687
675
 
689
677
   * Compute the number of bytes that would be needed to encode a
690
678
   * {@code string} field.
691
679
   */
692
 
  public static int computeStringSizeNoTag(String value) {
 
680
  public static int computeStringSizeNoTag(final String value) {
693
681
    try {
694
 
      byte[] bytes = value.getBytes("UTF-8");
 
682
      final byte[] bytes = value.getBytes("UTF-8");
695
683
      return computeRawVarint32Size(bytes.length) +
696
684
             bytes.length;
697
 
    } catch (java.io.UnsupportedEncodingException e) {
 
685
    } catch (UnsupportedEncodingException e) {
698
686
      throw new RuntimeException("UTF-8 not supported.", e);
699
687
    }
700
688
  }
703
691
   * Compute the number of bytes that would be needed to encode a
704
692
   * {@code group} field.
705
693
   */
706
 
  public static int computeGroupSizeNoTag(Message value) {
 
694
  public static int computeGroupSizeNoTag(final MessageLite value) {
707
695
    return value.getSerializedSize();
708
696
  }
709
697
 
711
699
   * Compute the number of bytes that would be needed to encode a
712
700
   * {@code group} field represented by an {@code UnknownFieldSet}, including
713
701
   * tag.
 
702
   *
 
703
   * @deprecated UnknownFieldSet now implements MessageLite, so you can just
 
704
   *             call {@link #computeUnknownGroupSizeNoTag}.
714
705
   */
715
 
  public static int computeUnknownGroupSizeNoTag(UnknownFieldSet value) {
716
 
    return value.getSerializedSize();
 
706
  @Deprecated
 
707
  public static int computeUnknownGroupSizeNoTag(final MessageLite value) {
 
708
    return computeGroupSizeNoTag(value);
717
709
  }
718
710
 
719
711
  /**
720
712
   * Compute the number of bytes that would be needed to encode an embedded
721
713
   * message field.
722
714
   */
723
 
  public static int computeMessageSizeNoTag(Message value) {
724
 
    int size = value.getSerializedSize();
 
715
  public static int computeMessageSizeNoTag(final MessageLite value) {
 
716
    final int size = value.getSerializedSize();
725
717
    return computeRawVarint32Size(size) + size;
726
718
  }
727
719
 
729
721
   * Compute the number of bytes that would be needed to encode a
730
722
   * {@code bytes} field.
731
723
   */
732
 
  public static int computeBytesSizeNoTag(ByteString value) {
 
724
  public static int computeBytesSizeNoTag(final ByteString value) {
733
725
    return computeRawVarint32Size(value.size()) +
734
726
           value.size();
735
727
  }
738
730
   * Compute the number of bytes that would be needed to encode a
739
731
   * {@code uint32} field.
740
732
   */
741
 
  public static int computeUInt32SizeNoTag(int value) {
 
733
  public static int computeUInt32SizeNoTag(final int value) {
742
734
    return computeRawVarint32Size(value);
743
735
  }
744
736
 
745
737
  /**
746
 
   * Compute the number of bytes that would be needed to encode an enum field.  
 
738
   * Compute the number of bytes that would be needed to encode an enum field.
747
739
   * Caller is responsible for converting the enum value to its numeric value.
748
740
   */
749
 
  public static int computeEnumSizeNoTag(int value) {
 
741
  public static int computeEnumSizeNoTag(final int value) {
750
742
    return computeRawVarint32Size(value);
751
743
  }
752
744
 
754
746
   * Compute the number of bytes that would be needed to encode an
755
747
   * {@code sfixed32} field.
756
748
   */
757
 
  public static int computeSFixed32SizeNoTag(int value) {
 
749
  public static int computeSFixed32SizeNoTag(final int value) {
758
750
    return LITTLE_ENDIAN_32_SIZE;
759
751
  }
760
752
 
762
754
   * Compute the number of bytes that would be needed to encode an
763
755
   * {@code sfixed64} field.
764
756
   */
765
 
  public static int computeSFixed64SizeNoTag(long value) {
 
757
  public static int computeSFixed64SizeNoTag(final long value) {
766
758
    return LITTLE_ENDIAN_64_SIZE;
767
759
  }
768
760
 
770
762
   * Compute the number of bytes that would be needed to encode an
771
763
   * {@code sint32} field.
772
764
   */
773
 
  public static int computeSInt32SizeNoTag(int value) {
 
765
  public static int computeSInt32SizeNoTag(final int value) {
774
766
    return computeRawVarint32Size(encodeZigZag32(value));
775
767
  }
776
768
 
778
770
   * Compute the number of bytes that would be needed to encode an
779
771
   * {@code sint64} field.
780
772
   */
781
 
  public static int computeSInt64SizeNoTag(long value) {
 
773
  public static int computeSInt64SizeNoTag(final long value) {
782
774
    return computeRawVarint64Size(encodeZigZag64(value));
783
775
  }
784
776
 
785
 
  /**
786
 
   * Compute the number of bytes that would be needed to encode a
787
 
   * field of arbitrary type, including tag, to the stream.
788
 
   *
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
794
 
   *               this field.
795
 
   */
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) {
801
 
      tagSize *= 2;
802
 
    }
803
 
    return tagSize + computeFieldSizeNoTag(type, value);
804
 
  }
805
 
 
806
 
  /**
807
 
   * Compute the number of bytes that would be needed to encode a
808
 
   * field of arbitrary type, excluding tag, to the stream.
809
 
   *
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
815
 
   *               this field.
816
 
   */
817
 
  public static int computeFieldSizeNoTag(
818
 
      Descriptors.FieldDescriptor.Type type, Object value) {
819
 
    switch (type) {
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);
837
 
 
838
 
      case ENUM:
839
 
        return computeEnumSizeNoTag(
840
 
            ((Descriptors.EnumValueDescriptor)value).getNumber());
841
 
    }
842
 
 
843
 
    throw new RuntimeException(
844
 
      "There is no way to get here, but the compiler thinks otherwise.");
845
 
  }
846
 
 
847
777
  // =================================================================
848
778
 
849
779
  /**
906
836
   * this exception will be thrown.
907
837
   */
908
838
  public static class OutOfSpaceException extends IOException {
 
839
    private static final long serialVersionUID = -6947486886997889499L;
 
840
 
909
841
    OutOfSpaceException() {
910
842
      super("CodedOutputStream was writing to a flat byte array and ran " +
911
843
            "out of space.");
913
845
  }
914
846
 
915
847
  /** Write a single byte. */
916
 
  public void writeRawByte(byte value) throws IOException {
 
848
  public void writeRawByte(final byte value) throws IOException {
917
849
    if (position == limit) {
918
850
      refreshBuffer();
919
851
    }
922
854
  }
923
855
 
924
856
  /** Write a single byte, represented by an integer value. */
925
 
  public void writeRawByte(int value) throws IOException {
 
857
  public void writeRawByte(final int value) throws IOException {
926
858
    writeRawByte((byte) value);
927
859
  }
928
860
 
929
861
  /** Write an array of bytes. */
930
 
  public void writeRawBytes(byte[] value) throws IOException {
 
862
  public void writeRawBytes(final byte[] value) throws IOException {
931
863
    writeRawBytes(value, 0, value.length);
932
864
  }
933
865
 
934
866
  /** Write part of an array of bytes. */
935
 
  public void writeRawBytes(byte[] value, int offset, int length)
 
867
  public void writeRawBytes(final byte[] value, int offset, int length)
936
868
                            throws IOException {
937
869
    if (limit - position >= length) {
938
870
      // We have room in the current buffer.
941
873
    } else {
942
874
      // Write extends past current buffer.  Fill the rest of this buffer and
943
875
      // flush.
944
 
      int bytesWritten = limit - position;
 
876
      final int bytesWritten = limit - position;
945
877
      System.arraycopy(value, offset, buffer, position, bytesWritten);
946
878
      offset += bytesWritten;
947
879
      length -= bytesWritten;
963
895
  }
964
896
 
965
897
  /** Encode and write a tag. */
966
 
  public void writeTag(int fieldNumber, int wireType) throws IOException {
 
898
  public void writeTag(final int fieldNumber, final int wireType)
 
899
                       throws IOException {
967
900
    writeRawVarint32(WireFormat.makeTag(fieldNumber, wireType));
968
901
  }
969
902
 
970
903
  /** Compute the number of bytes that would be needed to encode a tag. */
971
 
  public static int computeTagSize(int fieldNumber) {
 
904
  public static int computeTagSize(final int fieldNumber) {
972
905
    return computeRawVarint32Size(WireFormat.makeTag(fieldNumber, 0));
973
906
  }
974
907
 
993
926
   * {@code value} is treated as unsigned, so it won't be sign-extended if
994
927
   * negative.
995
928
   */
996
 
  public static int computeRawVarint32Size(int value) {
 
929
  public static int computeRawVarint32Size(final int value) {
997
930
    if ((value & (0xffffffff <<  7)) == 0) return 1;
998
931
    if ((value & (0xffffffff << 14)) == 0) return 2;
999
932
    if ((value & (0xffffffff << 21)) == 0) return 3;
1015
948
  }
1016
949
 
1017
950
  /** Compute the number of bytes that would be needed to encode a varint. */
1018
 
  public static int computeRawVarint64Size(long value) {
 
951
  public static int computeRawVarint64Size(final long value) {
1019
952
    if ((value & (0xffffffffffffffffL <<  7)) == 0) return 1;
1020
953
    if ((value & (0xffffffffffffffffL << 14)) == 0) return 2;
1021
954
    if ((value & (0xffffffffffffffffL << 21)) == 0) return 3;
1029
962
  }
1030
963
 
1031
964
  /** Write a little-endian 32-bit integer. */
1032
 
  public void writeRawLittleEndian32(int value) throws IOException {
 
965
  public void writeRawLittleEndian32(final int value) throws IOException {
1033
966
    writeRawByte((value      ) & 0xFF);
1034
967
    writeRawByte((value >>  8) & 0xFF);
1035
968
    writeRawByte((value >> 16) & 0xFF);
1039
972
  public static final int LITTLE_ENDIAN_32_SIZE = 4;
1040
973
 
1041
974
  /** Write a little-endian 64-bit integer. */
1042
 
  public void writeRawLittleEndian64(long value) throws IOException {
 
975
  public void writeRawLittleEndian64(final long value) throws IOException {
1043
976
    writeRawByte((int)(value      ) & 0xFF);
1044
977
    writeRawByte((int)(value >>  8) & 0xFF);
1045
978
    writeRawByte((int)(value >> 16) & 0xFF);
1062
995
   * @return An unsigned 32-bit integer, stored in a signed int because
1063
996
   *         Java has no explicit unsigned support.
1064
997
   */
1065
 
  public static int encodeZigZag32(int n) {
 
998
  public static int encodeZigZag32(final int n) {
1066
999
    // Note:  the right-shift must be arithmetic
1067
1000
    return (n << 1) ^ (n >> 31);
1068
1001
  }
1077
1010
   * @return An unsigned 64-bit integer, stored in a signed int because
1078
1011
   *         Java has no explicit unsigned support.
1079
1012
   */
1080
 
  public static long encodeZigZag64(long n) {
 
1013
  public static long encodeZigZag64(final long n) {
1081
1014
    // Note:  the right-shift must be arithmetic
1082
1015
    return (n << 1) ^ (n >> 63);
1083
1016
  }