blob: 287d29c3f310171247fcec5443c90530535f8ec4 [file] [log] [blame]
Brian Silverman9c614bc2016-02-15 20:20:02 -05001// 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/**
32 * @fileoverview This file contains utilities for encoding Javascript objects
33 * into binary, wire-format protocol buffers (in the form of Uint8Arrays) that
34 * a server can consume directly.
35 *
36 * jspb's BinaryWriter class defines methods for efficiently encoding
37 * Javascript objects into binary, wire-format protocol buffers and supports
38 * all the fundamental field types used in protocol buffers.
39 *
40 * Major caveat 1 - Users of this library _must_ keep their Javascript proto
41 * parsing code in sync with the original .proto file - presumably you'll be
42 * using the typed jspb code generator, but if you bypass that you'll need
43 * to keep things in sync by hand.
44 *
45 * Major caveat 2 - Javascript is unable to accurately represent integers
46 * larger than 2^53 due to its use of a double-precision floating point format
47 * for all numbers. BinaryWriter does not make any special effort to preserve
48 * precision for values above this limit - if you need to pass 64-bit integers
49 * (hash codes, for example) between the client and server without precision
50 * loss, do _not_ use this library.
51 *
52 * Major caveat 3 - This class uses typed arrays and must not be used on older
53 * browsers that do not support them.
54 *
55 * @author aappleby@google.com (Austin Appleby)
56 */
57
58goog.provide('jspb.BinaryWriter');
59
60goog.require('goog.asserts');
61goog.require('goog.crypt.base64');
62goog.require('jspb.BinaryConstants');
Austin Schuh40c16522018-10-28 20:27:54 -070063goog.require('jspb.BinaryEncoder');
Brian Silverman9c614bc2016-02-15 20:20:02 -050064goog.require('jspb.arith.Int64');
65goog.require('jspb.arith.UInt64');
66goog.require('jspb.utils');
67
Brian Silverman9c614bc2016-02-15 20:20:02 -050068
69
70/**
71 * BinaryWriter implements encoders for all the wire types specified in
72 * https://developers.google.com/protocol-buffers/docs/encoding.
73 *
74 * @constructor
75 * @struct
76 */
77jspb.BinaryWriter = function() {
78 /**
79 * Blocks of serialized data that will be concatenated once all messages have
80 * been written.
81 * @private {!Array<!Uint8Array|!Array<number>>}
82 */
83 this.blocks_ = [];
84
85 /**
Austin Schuh40c16522018-10-28 20:27:54 -070086 * Total number of bytes in the blocks_ array. Does _not_ include bytes in
87 * the encoder below.
Brian Silverman9c614bc2016-02-15 20:20:02 -050088 * @private {number}
89 */
90 this.totalLength_ = 0;
91
92 /**
Austin Schuh40c16522018-10-28 20:27:54 -070093 * Binary encoder holding pieces of a message that we're still serializing.
94 * When we get to a stopping point (either the start of a new submessage, or
95 * when we need to append a raw Uint8Array), the encoder's buffer will be
96 * added to the block array above and the encoder will be reset.
97 * @private {!jspb.BinaryEncoder}
Brian Silverman9c614bc2016-02-15 20:20:02 -050098 */
Austin Schuh40c16522018-10-28 20:27:54 -070099 this.encoder_ = new jspb.BinaryEncoder();
Brian Silverman9c614bc2016-02-15 20:20:02 -0500100
101 /**
102 * A stack of bookmarks containing the parent blocks for each message started
103 * via beginSubMessage(), needed as bookkeeping for endSubMessage().
104 * TODO(aappleby): Deprecated, users should be calling writeMessage().
Austin Schuh40c16522018-10-28 20:27:54 -0700105 * @private {!Array<!Array<number>>}
Brian Silverman9c614bc2016-02-15 20:20:02 -0500106 */
107 this.bookmarks_ = [];
108};
109
110
111/**
Brian Silverman9c614bc2016-02-15 20:20:02 -0500112 * Append a typed array of bytes onto the buffer.
113 *
114 * @param {!Uint8Array} arr The byte array to append.
115 * @private
116 */
117jspb.BinaryWriter.prototype.appendUint8Array_ = function(arr) {
Austin Schuh40c16522018-10-28 20:27:54 -0700118 var temp = this.encoder_.end();
119 this.blocks_.push(temp);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500120 this.blocks_.push(arr);
Austin Schuh40c16522018-10-28 20:27:54 -0700121 this.totalLength_ += temp.length + arr.length;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500122};
123
124
125/**
Austin Schuh40c16522018-10-28 20:27:54 -0700126 * Begins a new message by writing the field header and returning a bookmark
127 * which we will use to patch in the message length to in endDelimited_ below.
Brian Silverman9c614bc2016-02-15 20:20:02 -0500128 * @param {number} field
Austin Schuh40c16522018-10-28 20:27:54 -0700129 * @return {!Array<number>}
Brian Silverman9c614bc2016-02-15 20:20:02 -0500130 * @private
131 */
132jspb.BinaryWriter.prototype.beginDelimited_ = function(field) {
Austin Schuh40c16522018-10-28 20:27:54 -0700133 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
134 var bookmark = this.encoder_.end();
135 this.blocks_.push(bookmark);
136 this.totalLength_ += bookmark.length;
137 bookmark.push(this.totalLength_);
138 return bookmark;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500139};
140
141
142/**
Austin Schuh40c16522018-10-28 20:27:54 -0700143 * Ends a message by encoding the _change_ in length of the buffer to the
144 * parent block and adds the number of bytes needed to encode that length to
145 * the total byte length.
146 * @param {!Array<number>} bookmark
Brian Silverman9c614bc2016-02-15 20:20:02 -0500147 * @private
148 */
149jspb.BinaryWriter.prototype.endDelimited_ = function(bookmark) {
Austin Schuh40c16522018-10-28 20:27:54 -0700150 var oldLength = bookmark.pop();
151 var messageLength = this.totalLength_ + this.encoder_.length() - oldLength;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500152 goog.asserts.assert(messageLength >= 0);
153
Brian Silverman9c614bc2016-02-15 20:20:02 -0500154 while (messageLength > 127) {
Austin Schuh40c16522018-10-28 20:27:54 -0700155 bookmark.push((messageLength & 0x7f) | 0x80);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500156 messageLength = messageLength >>> 7;
Austin Schuh40c16522018-10-28 20:27:54 -0700157 this.totalLength_++;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500158 }
159
Austin Schuh40c16522018-10-28 20:27:54 -0700160 bookmark.push(messageLength);
161 this.totalLength_++;
162};
163
164
165/**
166 * Writes a pre-serialized message to the buffer.
167 * @param {!Uint8Array} bytes The array of bytes to write.
168 * @param {number} start The start of the range to write.
169 * @param {number} end The end of the range to write.
170 */
171jspb.BinaryWriter.prototype.writeSerializedMessage = function(
172 bytes, start, end) {
173 this.appendUint8Array_(bytes.subarray(start, end));
174};
175
176
177/**
178 * Writes a pre-serialized message to the buffer if the message and endpoints
179 * are non-null.
180 * @param {?Uint8Array} bytes The array of bytes to write.
181 * @param {?number} start The start of the range to write.
182 * @param {?number} end The end of the range to write.
183 */
184jspb.BinaryWriter.prototype.maybeWriteSerializedMessage = function(
185 bytes, start, end) {
186 if (bytes != null && start != null && end != null) {
187 this.writeSerializedMessage(bytes, start, end);
188 }
Brian Silverman9c614bc2016-02-15 20:20:02 -0500189};
190
191
192/**
193 * Resets the writer, throwing away any accumulated buffers.
194 */
195jspb.BinaryWriter.prototype.reset = function() {
196 this.blocks_ = [];
Austin Schuh40c16522018-10-28 20:27:54 -0700197 this.encoder_.end();
Brian Silverman9c614bc2016-02-15 20:20:02 -0500198 this.totalLength_ = 0;
199 this.bookmarks_ = [];
200};
201
202
203/**
204 * Converts the encoded data into a Uint8Array.
205 * @return {!Uint8Array}
206 */
207jspb.BinaryWriter.prototype.getResultBuffer = function() {
208 goog.asserts.assert(this.bookmarks_.length == 0);
209
Austin Schuh40c16522018-10-28 20:27:54 -0700210 var flat = new Uint8Array(this.totalLength_ + this.encoder_.length());
Brian Silverman9c614bc2016-02-15 20:20:02 -0500211
212 var blocks = this.blocks_;
213 var blockCount = blocks.length;
214 var offset = 0;
215
216 for (var i = 0; i < blockCount; i++) {
217 var block = blocks[i];
218 flat.set(block, offset);
219 offset += block.length;
220 }
221
Austin Schuh40c16522018-10-28 20:27:54 -0700222 var tail = this.encoder_.end();
223 flat.set(tail, offset);
224 offset += tail.length;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500225
226 // Post condition: `flattened` must have had every byte written.
227 goog.asserts.assert(offset == flat.length);
228
229 // Replace our block list with the flattened block, which lets GC reclaim
230 // the temp blocks sooner.
231 this.blocks_ = [flat];
Brian Silverman9c614bc2016-02-15 20:20:02 -0500232
233 return flat;
234};
235
236
237/**
Austin Schuh40c16522018-10-28 20:27:54 -0700238 * Converts the encoded data into a base64-encoded string.
239 * @param {boolean=} opt_webSafe True indicates we should use a websafe
240 * alphabet, which does not require escaping for use in URLs.
Brian Silverman9c614bc2016-02-15 20:20:02 -0500241 * @return {string}
242 */
Austin Schuh40c16522018-10-28 20:27:54 -0700243jspb.BinaryWriter.prototype.getResultBase64String = function(opt_webSafe) {
244 return goog.crypt.base64.encodeByteArray(this.getResultBuffer(), opt_webSafe);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500245};
246
247
248/**
249 * Begins a new sub-message. The client must call endSubMessage() when they're
250 * done.
251 * TODO(aappleby): Deprecated. Move callers to writeMessage().
252 * @param {number} field The field number of the sub-message.
253 */
254jspb.BinaryWriter.prototype.beginSubMessage = function(field) {
255 this.bookmarks_.push(this.beginDelimited_(field));
256};
257
258
259/**
260 * Finishes a sub-message and packs it into the parent messages' buffer.
261 * TODO(aappleby): Deprecated. Move callers to writeMessage().
262 */
263jspb.BinaryWriter.prototype.endSubMessage = function() {
264 goog.asserts.assert(this.bookmarks_.length >= 0);
265 this.endDelimited_(this.bookmarks_.pop());
266};
267
268
269/**
Brian Silverman9c614bc2016-02-15 20:20:02 -0500270 * Encodes a (field number, wire type) tuple into a wire-format field header
271 * and stores it in the buffer as a varint.
272 * @param {number} field The field number.
273 * @param {number} wireType The wire-type of the field, as specified in the
274 * protocol buffer documentation.
275 * @private
276 */
Austin Schuh40c16522018-10-28 20:27:54 -0700277jspb.BinaryWriter.prototype.writeFieldHeader_ =
Brian Silverman9c614bc2016-02-15 20:20:02 -0500278 function(field, wireType) {
279 goog.asserts.assert(field >= 1 && field == Math.floor(field));
280 var x = field * 8 + wireType;
Austin Schuh40c16522018-10-28 20:27:54 -0700281 this.encoder_.writeUnsignedVarint32(x);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500282};
283
284
285/**
286 * Writes a field of any valid scalar type to the binary stream.
287 * @param {jspb.BinaryConstants.FieldType} fieldType
288 * @param {number} field
289 * @param {jspb.AnyFieldType} value
290 */
291jspb.BinaryWriter.prototype.writeAny = function(fieldType, field, value) {
292 var fieldTypes = jspb.BinaryConstants.FieldType;
293 switch (fieldType) {
294 case fieldTypes.DOUBLE:
295 this.writeDouble(field, /** @type {number} */(value));
296 return;
297 case fieldTypes.FLOAT:
298 this.writeFloat(field, /** @type {number} */(value));
299 return;
300 case fieldTypes.INT64:
301 this.writeInt64(field, /** @type {number} */(value));
302 return;
303 case fieldTypes.UINT64:
304 this.writeUint64(field, /** @type {number} */(value));
305 return;
306 case fieldTypes.INT32:
307 this.writeInt32(field, /** @type {number} */(value));
308 return;
309 case fieldTypes.FIXED64:
310 this.writeFixed64(field, /** @type {number} */(value));
311 return;
312 case fieldTypes.FIXED32:
313 this.writeFixed32(field, /** @type {number} */(value));
314 return;
315 case fieldTypes.BOOL:
316 this.writeBool(field, /** @type {boolean} */(value));
317 return;
318 case fieldTypes.STRING:
319 this.writeString(field, /** @type {string} */(value));
320 return;
321 case fieldTypes.GROUP:
322 goog.asserts.fail('Group field type not supported in writeAny()');
323 return;
324 case fieldTypes.MESSAGE:
325 goog.asserts.fail('Message field type not supported in writeAny()');
326 return;
327 case fieldTypes.BYTES:
328 this.writeBytes(field, /** @type {?Uint8Array} */(value));
329 return;
330 case fieldTypes.UINT32:
331 this.writeUint32(field, /** @type {number} */(value));
332 return;
333 case fieldTypes.ENUM:
334 this.writeEnum(field, /** @type {number} */(value));
335 return;
336 case fieldTypes.SFIXED32:
337 this.writeSfixed32(field, /** @type {number} */(value));
338 return;
339 case fieldTypes.SFIXED64:
340 this.writeSfixed64(field, /** @type {number} */(value));
341 return;
342 case fieldTypes.SINT32:
343 this.writeSint32(field, /** @type {number} */(value));
344 return;
345 case fieldTypes.SINT64:
346 this.writeSint64(field, /** @type {number} */(value));
347 return;
348 case fieldTypes.FHASH64:
349 this.writeFixedHash64(field, /** @type {string} */(value));
350 return;
351 case fieldTypes.VHASH64:
352 this.writeVarintHash64(field, /** @type {string} */(value));
353 return;
354 default:
355 goog.asserts.fail('Invalid field type in writeAny()');
356 return;
357 }
358};
359
360
361/**
362 * Writes a varint field to the buffer without range checking.
363 * @param {number} field The field number.
364 * @param {number?} value The value to write.
365 * @private
366 */
367jspb.BinaryWriter.prototype.writeUnsignedVarint32_ = function(field, value) {
368 if (value == null) return;
Austin Schuh40c16522018-10-28 20:27:54 -0700369 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
370 this.encoder_.writeUnsignedVarint32(value);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500371};
372
373
374/**
375 * Writes a varint field to the buffer without range checking.
376 * @param {number} field The field number.
377 * @param {number?} value The value to write.
378 * @private
379 */
380jspb.BinaryWriter.prototype.writeSignedVarint32_ = function(field, value) {
381 if (value == null) return;
Austin Schuh40c16522018-10-28 20:27:54 -0700382 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
383 this.encoder_.writeSignedVarint32(value);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500384};
385
386
387/**
388 * Writes a varint field to the buffer without range checking.
389 * @param {number} field The field number.
390 * @param {number?} value The value to write.
391 * @private
392 */
Austin Schuh40c16522018-10-28 20:27:54 -0700393jspb.BinaryWriter.prototype.writeUnsignedVarint64_ = function(field, value) {
Brian Silverman9c614bc2016-02-15 20:20:02 -0500394 if (value == null) return;
Austin Schuh40c16522018-10-28 20:27:54 -0700395 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
396 this.encoder_.writeUnsignedVarint64(value);
397};
398
399
400/**
401 * Writes a varint field to the buffer without range checking.
402 * @param {number} field The field number.
403 * @param {number?} value The value to write.
404 * @private
405 */
406jspb.BinaryWriter.prototype.writeSignedVarint64_ = function(field, value) {
407 if (value == null) return;
408 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
409 this.encoder_.writeSignedVarint64(value);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500410};
411
412
413/**
414 * Writes a zigzag varint field to the buffer without range checking.
415 * @param {number} field The field number.
416 * @param {number?} value The value to write.
417 * @private
418 */
419jspb.BinaryWriter.prototype.writeZigzagVarint32_ = function(field, value) {
420 if (value == null) return;
Austin Schuh40c16522018-10-28 20:27:54 -0700421 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
422 this.encoder_.writeZigzagVarint32(value);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500423};
424
425
426/**
427 * Writes a zigzag varint field to the buffer without range checking.
428 * @param {number} field The field number.
429 * @param {number?} value The value to write.
430 * @private
431 */
Austin Schuh40c16522018-10-28 20:27:54 -0700432jspb.BinaryWriter.prototype.writeZigzagVarint64_ = function(field, value) {
Brian Silverman9c614bc2016-02-15 20:20:02 -0500433 if (value == null) return;
Austin Schuh40c16522018-10-28 20:27:54 -0700434 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
435 this.encoder_.writeZigzagVarint64(value);
436};
437
438
439/**
440 * Writes a zigzag varint field to the buffer without range checking.
441 * @param {number} field The field number.
442 * @param {string?} value The value to write.
443 * @private
444 */
445jspb.BinaryWriter.prototype.writeZigzagVarint64String_ = function(
446 field, value) {
447 if (value == null) return;
448 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
449 this.encoder_.writeZigzagVarint64String(value);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500450};
451
452
453/**
454 * Writes an int32 field to the buffer. Numbers outside the range [-2^31,2^31)
455 * will be truncated.
456 * @param {number} field The field number.
457 * @param {number?} value The value to write.
458 */
459jspb.BinaryWriter.prototype.writeInt32 = function(field, value) {
460 if (value == null) return;
461 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
462 (value < jspb.BinaryConstants.TWO_TO_31));
463 this.writeSignedVarint32_(field, value);
464};
465
466
467/**
468 * Writes an int32 field represented as a string to the buffer. Numbers outside
469 * the range [-2^31,2^31) will be truncated.
470 * @param {number} field The field number.
471 * @param {string?} value The value to write.
472 */
473jspb.BinaryWriter.prototype.writeInt32String = function(field, value) {
474 if (value == null) return;
475 var intValue = /** {number} */ parseInt(value, 10);
476 goog.asserts.assert((intValue >= -jspb.BinaryConstants.TWO_TO_31) &&
477 (intValue < jspb.BinaryConstants.TWO_TO_31));
478 this.writeSignedVarint32_(field, intValue);
479};
480
481
482/**
483 * Writes an int64 field to the buffer. Numbers outside the range [-2^63,2^63)
484 * will be truncated.
485 * @param {number} field The field number.
486 * @param {number?} value The value to write.
487 */
488jspb.BinaryWriter.prototype.writeInt64 = function(field, value) {
489 if (value == null) return;
490 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
491 (value < jspb.BinaryConstants.TWO_TO_63));
Austin Schuh40c16522018-10-28 20:27:54 -0700492 this.writeSignedVarint64_(field, value);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500493};
494
495
496/**
497 * Writes a int64 field (with value as a string) to the buffer.
498 * @param {number} field The field number.
499 * @param {string?} value The value to write.
500 */
501jspb.BinaryWriter.prototype.writeInt64String = function(field, value) {
502 if (value == null) return;
503 var num = jspb.arith.Int64.fromString(value);
Austin Schuh40c16522018-10-28 20:27:54 -0700504 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
505 this.encoder_.writeSplitVarint64(num.lo, num.hi);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500506};
507
508
509/**
510 * Writes a uint32 field to the buffer. Numbers outside the range [0,2^32)
511 * will be truncated.
512 * @param {number} field The field number.
513 * @param {number?} value The value to write.
514 */
515jspb.BinaryWriter.prototype.writeUint32 = function(field, value) {
516 if (value == null) return;
517 goog.asserts.assert((value >= 0) &&
518 (value < jspb.BinaryConstants.TWO_TO_32));
519 this.writeUnsignedVarint32_(field, value);
520};
521
522
523/**
524 * Writes a uint32 field represented as a string to the buffer. Numbers outside
525 * the range [0,2^32) will be truncated.
526 * @param {number} field The field number.
527 * @param {string?} value The value to write.
528 */
529jspb.BinaryWriter.prototype.writeUint32String = function(field, value) {
530 if (value == null) return;
531 var intValue = /** {number} */ parseInt(value, 10);
532 goog.asserts.assert((intValue >= 0) &&
533 (intValue < jspb.BinaryConstants.TWO_TO_32));
534 this.writeUnsignedVarint32_(field, intValue);
535};
536
537
538/**
539 * Writes a uint64 field to the buffer. Numbers outside the range [0,2^64)
540 * will be truncated.
541 * @param {number} field The field number.
542 * @param {number?} value The value to write.
543 */
544jspb.BinaryWriter.prototype.writeUint64 = function(field, value) {
545 if (value == null) return;
546 goog.asserts.assert((value >= 0) &&
547 (value < jspb.BinaryConstants.TWO_TO_64));
Austin Schuh40c16522018-10-28 20:27:54 -0700548 this.writeUnsignedVarint64_(field, value);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500549};
550
551
552/**
553 * Writes a uint64 field (with value as a string) to the buffer.
554 * @param {number} field The field number.
555 * @param {string?} value The value to write.
556 */
557jspb.BinaryWriter.prototype.writeUint64String = function(field, value) {
558 if (value == null) return;
559 var num = jspb.arith.UInt64.fromString(value);
Austin Schuh40c16522018-10-28 20:27:54 -0700560 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
561 this.encoder_.writeSplitVarint64(num.lo, num.hi);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500562};
563
564
565/**
566 * Writes a sint32 field to the buffer. Numbers outside the range [-2^31,2^31)
567 * will be truncated.
568 * @param {number} field The field number.
569 * @param {number?} value The value to write.
570 */
571jspb.BinaryWriter.prototype.writeSint32 = function(field, value) {
572 if (value == null) return;
573 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
574 (value < jspb.BinaryConstants.TWO_TO_31));
575 this.writeZigzagVarint32_(field, value);
576};
577
578
579/**
580 * Writes a sint64 field to the buffer. Numbers outside the range [-2^63,2^63)
581 * will be truncated.
582 * @param {number} field The field number.
583 * @param {number?} value The value to write.
584 */
585jspb.BinaryWriter.prototype.writeSint64 = function(field, value) {
586 if (value == null) return;
587 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
588 (value < jspb.BinaryConstants.TWO_TO_63));
Austin Schuh40c16522018-10-28 20:27:54 -0700589 this.writeZigzagVarint64_(field, value);
590};
591
592
593/**
594 * Writes a sint64 field to the buffer. Numbers outside the range [-2^63,2^63)
595 * will be truncated.
596 * @param {number} field The field number.
597 * @param {string?} value The decimal string to write.
598 */
599jspb.BinaryWriter.prototype.writeSint64String = function(field, value) {
600 if (value == null) return;
601 goog.asserts.assert((+value >= -jspb.BinaryConstants.TWO_TO_63) &&
602 (+value < jspb.BinaryConstants.TWO_TO_63));
603 this.writeZigzagVarint64String_(field, value);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500604};
605
606
607/**
608 * Writes a fixed32 field to the buffer. Numbers outside the range [0,2^32)
609 * will be truncated.
610 * @param {number} field The field number.
611 * @param {number?} value The value to write.
612 */
613jspb.BinaryWriter.prototype.writeFixed32 = function(field, value) {
614 if (value == null) return;
615 goog.asserts.assert((value >= 0) &&
616 (value < jspb.BinaryConstants.TWO_TO_32));
Austin Schuh40c16522018-10-28 20:27:54 -0700617 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32);
618 this.encoder_.writeUint32(value);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500619};
620
621
622/**
623 * Writes a fixed64 field to the buffer. Numbers outside the range [0,2^64)
624 * will be truncated.
625 * @param {number} field The field number.
626 * @param {number?} value The value to write.
627 */
628jspb.BinaryWriter.prototype.writeFixed64 = function(field, value) {
629 if (value == null) return;
630 goog.asserts.assert((value >= 0) &&
631 (value < jspb.BinaryConstants.TWO_TO_64));
Austin Schuh40c16522018-10-28 20:27:54 -0700632 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
633 this.encoder_.writeUint64(value);
634};
635
636
637/**
638 * Writes a fixed64 field (with value as a string) to the buffer.
639 * @param {number} field The field number.
640 * @param {string?} value The value to write.
641 */
642jspb.BinaryWriter.prototype.writeFixed64String = function(field, value) {
643 if (value == null) return;
644 var num = jspb.arith.UInt64.fromString(value);
645 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
646 this.encoder_.writeSplitFixed64(num.lo, num.hi);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500647};
648
649
650/**
651 * Writes a sfixed32 field to the buffer. Numbers outside the range
652 * [-2^31,2^31) will be truncated.
653 * @param {number} field The field number.
654 * @param {number?} value The value to write.
655 */
656jspb.BinaryWriter.prototype.writeSfixed32 = function(field, value) {
657 if (value == null) return;
658 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
659 (value < jspb.BinaryConstants.TWO_TO_31));
Austin Schuh40c16522018-10-28 20:27:54 -0700660 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32);
661 this.encoder_.writeInt32(value);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500662};
663
664
665/**
666 * Writes a sfixed64 field to the buffer. Numbers outside the range
667 * [-2^63,2^63) will be truncated.
668 * @param {number} field The field number.
669 * @param {number?} value The value to write.
670 */
671jspb.BinaryWriter.prototype.writeSfixed64 = function(field, value) {
672 if (value == null) return;
673 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
674 (value < jspb.BinaryConstants.TWO_TO_63));
Austin Schuh40c16522018-10-28 20:27:54 -0700675 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
676 this.encoder_.writeInt64(value);
677};
678
679
680/**
681 * Writes a sfixed64 string field to the buffer. Numbers outside the range
682 * [-2^63,2^63) will be truncated.
683 * @param {number} field The field number.
684 * @param {string?} value The value to write.
685 */
686jspb.BinaryWriter.prototype.writeSfixed64String = function(field, value) {
687 if (value == null) return;
688 var num = jspb.arith.Int64.fromString(value);
689 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
690 this.encoder_.writeSplitFixed64(num.lo, num.hi);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500691};
692
693
694/**
695 * Writes a single-precision floating point field to the buffer. Numbers
696 * requiring more than 32 bits of precision will be truncated.
697 * @param {number} field The field number.
698 * @param {number?} value The value to write.
699 */
700jspb.BinaryWriter.prototype.writeFloat = function(field, value) {
701 if (value == null) return;
Austin Schuh40c16522018-10-28 20:27:54 -0700702 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32);
703 this.encoder_.writeFloat(value);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500704};
705
706
707/**
708 * Writes a double-precision floating point field to the buffer. As this is the
709 * native format used by JavaScript, no precision will be lost.
710 * @param {number} field The field number.
711 * @param {number?} value The value to write.
712 */
713jspb.BinaryWriter.prototype.writeDouble = function(field, value) {
714 if (value == null) return;
Austin Schuh40c16522018-10-28 20:27:54 -0700715 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
716 this.encoder_.writeDouble(value);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500717};
718
719
720/**
Austin Schuh40c16522018-10-28 20:27:54 -0700721 * Writes a boolean field to the buffer. We allow numbers as input
722 * because the JSPB code generator uses 0/1 instead of true/false to save space
723 * in the string representation of the proto.
Brian Silverman9c614bc2016-02-15 20:20:02 -0500724 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -0700725 * @param {boolean?|number?} value The value to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -0500726 */
727jspb.BinaryWriter.prototype.writeBool = function(field, value) {
728 if (value == null) return;
Austin Schuh40c16522018-10-28 20:27:54 -0700729 goog.asserts.assert(goog.isBoolean(value) || goog.isNumber(value));
730 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
731 this.encoder_.writeBool(value);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500732};
733
734
735/**
736 * Writes an enum field to the buffer.
737 * @param {number} field The field number.
738 * @param {number?} value The value to write.
739 */
740jspb.BinaryWriter.prototype.writeEnum = function(field, value) {
741 if (value == null) return;
742 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
743 (value < jspb.BinaryConstants.TWO_TO_31));
Austin Schuh40c16522018-10-28 20:27:54 -0700744 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
745 this.encoder_.writeSignedVarint32(value);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500746};
747
748
749/**
750 * Writes a string field to the buffer.
751 * @param {number} field The field number.
752 * @param {string?} value The string to write.
753 */
754jspb.BinaryWriter.prototype.writeString = function(field, value) {
755 if (value == null) return;
Austin Schuh40c16522018-10-28 20:27:54 -0700756 var bookmark = this.beginDelimited_(field);
757 this.encoder_.writeString(value);
758 this.endDelimited_(bookmark);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500759};
760
761
762/**
763 * Writes an arbitrary byte field to the buffer. Note - to match the behavior
764 * of the C++ implementation, empty byte arrays _are_ serialized.
Brian Silverman9c614bc2016-02-15 20:20:02 -0500765 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -0700766 * @param {?jspb.ByteSource} value The array of bytes to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -0500767 */
Austin Schuh40c16522018-10-28 20:27:54 -0700768jspb.BinaryWriter.prototype.writeBytes = function(field, value) {
769 if (value == null) return;
770 var bytes = jspb.utils.byteSourceToUint8Array(value);
771 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
772 this.encoder_.writeUnsignedVarint32(bytes.length);
773 this.appendUint8Array_(bytes);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500774};
775
776
777/**
778 * Writes a message to the buffer.
Brian Silverman9c614bc2016-02-15 20:20:02 -0500779 * @param {number} field The field number.
780 * @param {?MessageType} value The message to write.
Austin Schuh40c16522018-10-28 20:27:54 -0700781 * @param {function(MessageTypeNonNull, !jspb.BinaryWriter)} writerCallback
782 * Will be invoked with the value to write and the writer to write it with.
783 * @template MessageType
784 * Use go/closure-ttl to declare a non-nullable version of MessageType. Replace
785 * the null in blah|null with none. This is necessary because the compiler will
786 * infer MessageType to be nullable if the value parameter is nullable.
787 * @template MessageTypeNonNull :=
788 * cond(isUnknown(MessageType), unknown(),
789 * mapunion(MessageType, (X) =>
790 * cond(eq(X, 'null'), none(), X)))
791 * =:
Brian Silverman9c614bc2016-02-15 20:20:02 -0500792 */
Austin Schuh40c16522018-10-28 20:27:54 -0700793jspb.BinaryWriter.prototype.writeMessage = function(
794 field, value, writerCallback) {
795 if (value == null) return;
796 var bookmark = this.beginDelimited_(field);
797 writerCallback(value, this);
798 this.endDelimited_(bookmark);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500799};
800
801
802/**
803 * Writes a group message to the buffer.
804 *
Brian Silverman9c614bc2016-02-15 20:20:02 -0500805 * @param {number} field The field number.
806 * @param {?MessageType} value The message to write, wrapped with START_GROUP /
807 * END_GROUP tags. Will be a no-op if 'value' is null.
Austin Schuh40c16522018-10-28 20:27:54 -0700808 * @param {function(MessageTypeNonNull, !jspb.BinaryWriter)} writerCallback
809 * Will be invoked with the value to write and the writer to write it with.
810 * @template MessageType
811 * Use go/closure-ttl to declare a non-nullable version of MessageType. Replace
812 * the null in blah|null with none. This is necessary because the compiler will
813 * infer MessageType to be nullable if the value parameter is nullable.
814 * @template MessageTypeNonNull :=
815 * cond(isUnknown(MessageType), unknown(),
816 * mapunion(MessageType, (X) =>
817 * cond(eq(X, 'null'), none(), X)))
818 * =:
Brian Silverman9c614bc2016-02-15 20:20:02 -0500819 */
Austin Schuh40c16522018-10-28 20:27:54 -0700820jspb.BinaryWriter.prototype.writeGroup = function(
821 field, value, writerCallback) {
822 if (value == null) return;
823 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.START_GROUP);
824 writerCallback(value, this);
825 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.END_GROUP);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500826};
827
828
829/**
830 * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
831 * the buffer.
832 * @param {number} field The field number.
833 * @param {string?} value The hash string.
834 */
835jspb.BinaryWriter.prototype.writeFixedHash64 = function(field, value) {
836 if (value == null) return;
837 goog.asserts.assert(value.length == 8);
Austin Schuh40c16522018-10-28 20:27:54 -0700838 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
839 this.encoder_.writeFixedHash64(value);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500840};
841
842
843/**
844 * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
845 * the buffer.
846 * @param {number} field The field number.
847 * @param {string?} value The hash string.
848 */
849jspb.BinaryWriter.prototype.writeVarintHash64 = function(field, value) {
850 if (value == null) return;
851 goog.asserts.assert(value.length == 8);
Austin Schuh40c16522018-10-28 20:27:54 -0700852 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
853 this.encoder_.writeVarintHash64(value);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500854};
855
856
857/**
Austin Schuh40c16522018-10-28 20:27:54 -0700858 * Writes an array of numbers to the buffer as a repeated 32-bit int field.
Brian Silverman9c614bc2016-02-15 20:20:02 -0500859 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -0700860 * @param {?Array<number>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -0500861 */
Austin Schuh40c16522018-10-28 20:27:54 -0700862jspb.BinaryWriter.prototype.writeRepeatedInt32 = function(field, value) {
Brian Silverman9c614bc2016-02-15 20:20:02 -0500863 if (value == null) return;
864 for (var i = 0; i < value.length; i++) {
865 this.writeSignedVarint32_(field, value[i]);
866 }
867};
868
869
870/**
Brian Silverman9c614bc2016-02-15 20:20:02 -0500871 * Writes an array of numbers formatted as strings to the buffer as a repeated
872 * 32-bit int field.
873 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -0700874 * @param {?Array<string>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -0500875 */
Austin Schuh40c16522018-10-28 20:27:54 -0700876jspb.BinaryWriter.prototype.writeRepeatedInt32String = function(field, value) {
Brian Silverman9c614bc2016-02-15 20:20:02 -0500877 if (value == null) return;
878 for (var i = 0; i < value.length; i++) {
879 this.writeInt32String(field, value[i]);
880 }
881};
882
883
884/**
885 * Writes an array of numbers to the buffer as a repeated 64-bit int field.
886 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -0700887 * @param {?Array<number>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -0500888 */
Austin Schuh40c16522018-10-28 20:27:54 -0700889jspb.BinaryWriter.prototype.writeRepeatedInt64 = function(field, value) {
890 if (value == null) return;
891 for (var i = 0; i < value.length; i++) {
892 this.writeSignedVarint64_(field, value[i]);
893 }
894};
Brian Silverman9c614bc2016-02-15 20:20:02 -0500895
896
897/**
898 * Writes an array of numbers formatted as strings to the buffer as a repeated
899 * 64-bit int field.
900 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -0700901 * @param {?Array<string>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -0500902 */
Austin Schuh40c16522018-10-28 20:27:54 -0700903jspb.BinaryWriter.prototype.writeRepeatedInt64String = function(field, value) {
Brian Silverman9c614bc2016-02-15 20:20:02 -0500904 if (value == null) return;
905 for (var i = 0; i < value.length; i++) {
906 this.writeInt64String(field, value[i]);
907 }
908};
909
910
911/**
912 * Writes an array numbers to the buffer as a repeated unsigned 32-bit int
913 * field.
914 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -0700915 * @param {?Array<number>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -0500916 */
Austin Schuh40c16522018-10-28 20:27:54 -0700917jspb.BinaryWriter.prototype.writeRepeatedUint32 = function(field, value) {
918 if (value == null) return;
919 for (var i = 0; i < value.length; i++) {
920 this.writeUnsignedVarint32_(field, value[i]);
921 }
922};
Brian Silverman9c614bc2016-02-15 20:20:02 -0500923
924
925/**
926 * Writes an array of numbers formatted as strings to the buffer as a repeated
927 * unsigned 32-bit int field.
928 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -0700929 * @param {?Array<string>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -0500930 */
Austin Schuh40c16522018-10-28 20:27:54 -0700931jspb.BinaryWriter.prototype.writeRepeatedUint32String = function(field, value) {
Brian Silverman9c614bc2016-02-15 20:20:02 -0500932 if (value == null) return;
933 for (var i = 0; i < value.length; i++) {
934 this.writeUint32String(field, value[i]);
935 }
936};
937
938
939/**
940 * Writes an array numbers to the buffer as a repeated unsigned 64-bit int
941 * field.
942 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -0700943 * @param {?Array<number>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -0500944 */
Austin Schuh40c16522018-10-28 20:27:54 -0700945jspb.BinaryWriter.prototype.writeRepeatedUint64 = function(field, value) {
946 if (value == null) return;
947 for (var i = 0; i < value.length; i++) {
948 this.writeUnsignedVarint64_(field, value[i]);
949 }
950};
Brian Silverman9c614bc2016-02-15 20:20:02 -0500951
952
953/**
954 * Writes an array of numbers formatted as strings to the buffer as a repeated
955 * unsigned 64-bit int field.
956 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -0700957 * @param {?Array<string>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -0500958 */
Austin Schuh40c16522018-10-28 20:27:54 -0700959jspb.BinaryWriter.prototype.writeRepeatedUint64String = function(field, value) {
Brian Silverman9c614bc2016-02-15 20:20:02 -0500960 if (value == null) return;
961 for (var i = 0; i < value.length; i++) {
962 this.writeUint64String(field, value[i]);
963 }
964};
965
966
967/**
968 * Writes an array numbers to the buffer as a repeated signed 32-bit int field.
969 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -0700970 * @param {?Array<number>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -0500971 */
Austin Schuh40c16522018-10-28 20:27:54 -0700972jspb.BinaryWriter.prototype.writeRepeatedSint32 = function(field, value) {
973 if (value == null) return;
974 for (var i = 0; i < value.length; i++) {
975 this.writeZigzagVarint32_(field, value[i]);
976 }
977};
Brian Silverman9c614bc2016-02-15 20:20:02 -0500978
979
980/**
981 * Writes an array numbers to the buffer as a repeated signed 64-bit int field.
982 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -0700983 * @param {?Array<number>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -0500984 */
Austin Schuh40c16522018-10-28 20:27:54 -0700985jspb.BinaryWriter.prototype.writeRepeatedSint64 = function(field, value) {
986 if (value == null) return;
987 for (var i = 0; i < value.length; i++) {
988 this.writeZigzagVarint64_(field, value[i]);
989 }
990};
991
992
993/**
994 * Writes an array numbers to the buffer as a repeated signed 64-bit int field.
995 * @param {number} field The field number.
996 * @param {?Array<string>} value The array of ints to write.
997 */
998jspb.BinaryWriter.prototype.writeRepeatedSint64String = function(field, value) {
999 if (value == null) return;
1000 for (var i = 0; i < value.length; i++) {
1001 this.writeZigzagVarint64String_(field, value[i]);
1002 }
1003};
Brian Silverman9c614bc2016-02-15 20:20:02 -05001004
1005
1006/**
1007 * Writes an array of numbers to the buffer as a repeated fixed32 field. This
1008 * works for both signed and unsigned fixed32s.
1009 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001010 * @param {?Array<number>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001011 */
1012jspb.BinaryWriter.prototype.writeRepeatedFixed32 = function(field, value) {
1013 if (value == null) return;
1014 for (var i = 0; i < value.length; i++) {
1015 this.writeFixed32(field, value[i]);
1016 }
1017};
1018
1019
1020/**
1021 * Writes an array of numbers to the buffer as a repeated fixed64 field. This
1022 * works for both signed and unsigned fixed64s.
1023 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001024 * @param {?Array<number>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001025 */
1026jspb.BinaryWriter.prototype.writeRepeatedFixed64 = function(field, value) {
1027 if (value == null) return;
1028 for (var i = 0; i < value.length; i++) {
1029 this.writeFixed64(field, value[i]);
1030 }
1031};
1032
1033
1034/**
Austin Schuh40c16522018-10-28 20:27:54 -07001035 * Writes an array of numbers to the buffer as a repeated fixed64 field. This
1036 * works for both signed and unsigned fixed64s.
1037 * @param {number} field The field number.
1038 * @param {?Array<string>} value The array of decimal strings to write.
1039 */
1040jspb.BinaryWriter.prototype.writeRepeatedFixed64String = function(
1041 field, value) {
1042 if (value == null) return;
1043 for (var i = 0; i < value.length; i++) {
1044 this.writeFixed64String(field, value[i]);
1045 }
1046};
1047
1048
1049/**
Brian Silverman9c614bc2016-02-15 20:20:02 -05001050 * Writes an array of numbers to the buffer as a repeated sfixed32 field.
1051 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001052 * @param {?Array<number>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001053 */
1054jspb.BinaryWriter.prototype.writeRepeatedSfixed32 = function(field, value) {
1055 if (value == null) return;
1056 for (var i = 0; i < value.length; i++) {
1057 this.writeSfixed32(field, value[i]);
1058 }
1059};
1060
1061
1062/**
1063 * Writes an array of numbers to the buffer as a repeated sfixed64 field.
1064 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001065 * @param {?Array<number>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001066 */
1067jspb.BinaryWriter.prototype.writeRepeatedSfixed64 = function(field, value) {
1068 if (value == null) return;
1069 for (var i = 0; i < value.length; i++) {
1070 this.writeSfixed64(field, value[i]);
1071 }
1072};
1073
1074
1075/**
Austin Schuh40c16522018-10-28 20:27:54 -07001076 * Writes an array of decimal strings to the buffer as a repeated sfixed64
1077 * field.
1078 * @param {number} field The field number.
1079 * @param {?Array<string>} value The array of decimal strings to write.
1080 */
1081jspb.BinaryWriter.prototype.writeRepeatedSfixed64String = function(field, value) {
1082 if (value == null) return;
1083 for (var i = 0; i < value.length; i++) {
1084 this.writeSfixed64String(field, value[i]);
1085 }
1086};
1087
1088
1089/**
Brian Silverman9c614bc2016-02-15 20:20:02 -05001090 * Writes an array of numbers to the buffer as a repeated float field.
1091 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001092 * @param {?Array<number>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001093 */
1094jspb.BinaryWriter.prototype.writeRepeatedFloat = function(field, value) {
1095 if (value == null) return;
1096 for (var i = 0; i < value.length; i++) {
1097 this.writeFloat(field, value[i]);
1098 }
1099};
1100
1101
1102/**
1103 * Writes an array of numbers to the buffer as a repeated double field.
1104 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001105 * @param {?Array<number>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001106 */
1107jspb.BinaryWriter.prototype.writeRepeatedDouble = function(field, value) {
1108 if (value == null) return;
1109 for (var i = 0; i < value.length; i++) {
1110 this.writeDouble(field, value[i]);
1111 }
1112};
1113
1114
1115/**
1116 * Writes an array of booleans to the buffer as a repeated bool field.
1117 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001118 * @param {?Array<boolean>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001119 */
1120jspb.BinaryWriter.prototype.writeRepeatedBool = function(field, value) {
1121 if (value == null) return;
1122 for (var i = 0; i < value.length; i++) {
1123 this.writeBool(field, value[i]);
1124 }
1125};
1126
1127
1128/**
1129 * Writes an array of enums to the buffer as a repeated enum field.
1130 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001131 * @param {?Array<number>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001132 */
1133jspb.BinaryWriter.prototype.writeRepeatedEnum = function(field, value) {
1134 if (value == null) return;
1135 for (var i = 0; i < value.length; i++) {
1136 this.writeEnum(field, value[i]);
1137 }
1138};
1139
1140
1141/**
1142 * Writes an array of strings to the buffer as a repeated string field.
1143 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001144 * @param {?Array<string>} value The array of strings to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001145 */
1146jspb.BinaryWriter.prototype.writeRepeatedString = function(field, value) {
1147 if (value == null) return;
1148 for (var i = 0; i < value.length; i++) {
1149 this.writeString(field, value[i]);
1150 }
1151};
1152
1153
1154/**
1155 * Writes an array of arbitrary byte fields to the buffer.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001156 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001157 * @param {?Array<!jspb.ByteSource>} value The arrays of arrays of bytes to
1158 * write.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001159 */
Austin Schuh40c16522018-10-28 20:27:54 -07001160jspb.BinaryWriter.prototype.writeRepeatedBytes = function(field, value) {
1161 if (value == null) return;
1162 for (var i = 0; i < value.length; i++) {
1163 this.writeBytes(field, value[i]);
Brian Silverman9c614bc2016-02-15 20:20:02 -05001164 }
1165};
1166
1167
1168/**
Brian Silverman9c614bc2016-02-15 20:20:02 -05001169 * Writes an array of messages to the buffer.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001170 * @template MessageType
1171 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001172 * @param {?Array<MessageType>} value The array of messages to
Brian Silverman9c614bc2016-02-15 20:20:02 -05001173 * write.
Austin Schuh40c16522018-10-28 20:27:54 -07001174 * @param {function(MessageType, !jspb.BinaryWriter)} writerCallback
1175 * Will be invoked with the value to write and the writer to write it with.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001176 */
Austin Schuh40c16522018-10-28 20:27:54 -07001177jspb.BinaryWriter.prototype.writeRepeatedMessage = function(
1178 field, value, writerCallback) {
1179 if (value == null) return;
1180 for (var i = 0; i < value.length; i++) {
1181 var bookmark = this.beginDelimited_(field);
1182 writerCallback(value[i], this);
1183 this.endDelimited_(bookmark);
Brian Silverman9c614bc2016-02-15 20:20:02 -05001184 }
1185};
1186
1187
1188/**
1189 * Writes an array of group messages to the buffer.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001190 * @template MessageType
1191 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001192 * @param {?Array<MessageType>} value The array of messages to
Brian Silverman9c614bc2016-02-15 20:20:02 -05001193 * write.
Austin Schuh40c16522018-10-28 20:27:54 -07001194 * @param {function(MessageType, !jspb.BinaryWriter)} writerCallback
1195 * Will be invoked with the value to write and the writer to write it with.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001196 */
Austin Schuh40c16522018-10-28 20:27:54 -07001197jspb.BinaryWriter.prototype.writeRepeatedGroup = function(
1198 field, value, writerCallback) {
1199 if (value == null) return;
1200 for (var i = 0; i < value.length; i++) {
1201 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.START_GROUP);
1202 writerCallback(value[i], this);
1203 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.END_GROUP);
Brian Silverman9c614bc2016-02-15 20:20:02 -05001204 }
1205};
1206
1207
1208/**
1209 * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
1210 * the buffer.
1211 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001212 * @param {?Array<string>} value The array of hashes to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001213 */
1214jspb.BinaryWriter.prototype.writeRepeatedFixedHash64 =
1215 function(field, value) {
1216 if (value == null) return;
1217 for (var i = 0; i < value.length; i++) {
1218 this.writeFixedHash64(field, value[i]);
1219 }
1220};
1221
1222
1223/**
1224 * Writes a repeated 64-bit hash string field (8 characters @ 8 bits of data
1225 * each) to the buffer.
1226 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001227 * @param {?Array<string>} value The array of hashes to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001228 */
1229jspb.BinaryWriter.prototype.writeRepeatedVarintHash64 =
1230 function(field, value) {
1231 if (value == null) return;
1232 for (var i = 0; i < value.length; i++) {
1233 this.writeVarintHash64(field, value[i]);
1234 }
1235};
1236
1237
1238/**
Brian Silverman9c614bc2016-02-15 20:20:02 -05001239 * Writes an array of numbers to the buffer as a packed 32-bit int field.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001240 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001241 * @param {?Array<number>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001242 */
Austin Schuh40c16522018-10-28 20:27:54 -07001243jspb.BinaryWriter.prototype.writePackedInt32 = function(field, value) {
1244 if (value == null || !value.length) return;
1245 var bookmark = this.beginDelimited_(field);
1246 for (var i = 0; i < value.length; i++) {
1247 this.encoder_.writeSignedVarint32(value[i]);
1248 }
1249 this.endDelimited_(bookmark);
1250};
Brian Silverman9c614bc2016-02-15 20:20:02 -05001251
1252
1253/**
1254 * Writes an array of numbers represented as strings to the buffer as a packed
1255 * 32-bit int field.
1256 * @param {number} field
Austin Schuh40c16522018-10-28 20:27:54 -07001257 * @param {?Array<string>} value
Brian Silverman9c614bc2016-02-15 20:20:02 -05001258 */
1259jspb.BinaryWriter.prototype.writePackedInt32String = function(field, value) {
1260 if (value == null || !value.length) return;
1261 var bookmark = this.beginDelimited_(field);
1262 for (var i = 0; i < value.length; i++) {
Austin Schuh40c16522018-10-28 20:27:54 -07001263 this.encoder_.writeSignedVarint32(parseInt(value[i], 10));
Brian Silverman9c614bc2016-02-15 20:20:02 -05001264 }
1265 this.endDelimited_(bookmark);
1266};
1267
1268
1269/**
1270 * Writes an array of numbers to the buffer as a packed 64-bit int field.
1271 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001272 * @param {?Array<number>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001273 */
Austin Schuh40c16522018-10-28 20:27:54 -07001274jspb.BinaryWriter.prototype.writePackedInt64 = function(field, value) {
1275 if (value == null || !value.length) return;
1276 var bookmark = this.beginDelimited_(field);
1277 for (var i = 0; i < value.length; i++) {
1278 this.encoder_.writeSignedVarint64(value[i]);
1279 }
1280 this.endDelimited_(bookmark);
1281};
Brian Silverman9c614bc2016-02-15 20:20:02 -05001282
1283
1284/**
1285 * Writes an array of numbers represented as strings to the buffer as a packed
1286 * 64-bit int field.
1287 * @param {number} field
Austin Schuh40c16522018-10-28 20:27:54 -07001288 * @param {?Array<string>} value
Brian Silverman9c614bc2016-02-15 20:20:02 -05001289 */
Austin Schuh40c16522018-10-28 20:27:54 -07001290jspb.BinaryWriter.prototype.writePackedInt64String = function(field, value) {
Brian Silverman9c614bc2016-02-15 20:20:02 -05001291 if (value == null || !value.length) return;
1292 var bookmark = this.beginDelimited_(field);
1293 for (var i = 0; i < value.length; i++) {
1294 var num = jspb.arith.Int64.fromString(value[i]);
Austin Schuh40c16522018-10-28 20:27:54 -07001295 this.encoder_.writeSplitVarint64(num.lo, num.hi);
Brian Silverman9c614bc2016-02-15 20:20:02 -05001296 }
1297 this.endDelimited_(bookmark);
1298};
1299
1300
1301/**
1302 * Writes an array numbers to the buffer as a packed unsigned 32-bit int field.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001303 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001304 * @param {?Array<number>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001305 */
Austin Schuh40c16522018-10-28 20:27:54 -07001306jspb.BinaryWriter.prototype.writePackedUint32 = function(field, value) {
1307 if (value == null || !value.length) return;
1308 var bookmark = this.beginDelimited_(field);
1309 for (var i = 0; i < value.length; i++) {
1310 this.encoder_.writeUnsignedVarint32(value[i]);
1311 }
1312 this.endDelimited_(bookmark);
1313};
Brian Silverman9c614bc2016-02-15 20:20:02 -05001314
1315
1316/**
1317 * Writes an array of numbers represented as strings to the buffer as a packed
1318 * unsigned 32-bit int field.
1319 * @param {number} field
Austin Schuh40c16522018-10-28 20:27:54 -07001320 * @param {?Array<string>} value
Brian Silverman9c614bc2016-02-15 20:20:02 -05001321 */
1322jspb.BinaryWriter.prototype.writePackedUint32String =
1323 function(field, value) {
1324 if (value == null || !value.length) return;
1325 var bookmark = this.beginDelimited_(field);
1326 for (var i = 0; i < value.length; i++) {
Austin Schuh40c16522018-10-28 20:27:54 -07001327 this.encoder_.writeUnsignedVarint32(parseInt(value[i], 10));
Brian Silverman9c614bc2016-02-15 20:20:02 -05001328 }
1329 this.endDelimited_(bookmark);
1330};
1331
1332
1333/**
1334 * Writes an array numbers to the buffer as a packed unsigned 64-bit int field.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001335 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001336 * @param {?Array<number>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001337 */
Austin Schuh40c16522018-10-28 20:27:54 -07001338jspb.BinaryWriter.prototype.writePackedUint64 = function(field, value) {
1339 if (value == null || !value.length) return;
1340 var bookmark = this.beginDelimited_(field);
1341 for (var i = 0; i < value.length; i++) {
1342 this.encoder_.writeUnsignedVarint64(value[i]);
1343 }
1344 this.endDelimited_(bookmark);
1345};
Brian Silverman9c614bc2016-02-15 20:20:02 -05001346
1347
1348/**
1349 * Writes an array of numbers represented as strings to the buffer as a packed
1350 * unsigned 64-bit int field.
1351 * @param {number} field
Austin Schuh40c16522018-10-28 20:27:54 -07001352 * @param {?Array<string>} value
Brian Silverman9c614bc2016-02-15 20:20:02 -05001353 */
1354jspb.BinaryWriter.prototype.writePackedUint64String =
1355 function(field, value) {
1356 if (value == null || !value.length) return;
1357 var bookmark = this.beginDelimited_(field);
1358 for (var i = 0; i < value.length; i++) {
1359 var num = jspb.arith.UInt64.fromString(value[i]);
Austin Schuh40c16522018-10-28 20:27:54 -07001360 this.encoder_.writeSplitVarint64(num.lo, num.hi);
Brian Silverman9c614bc2016-02-15 20:20:02 -05001361 }
1362 this.endDelimited_(bookmark);
1363};
1364
1365
1366/**
1367 * Writes an array numbers to the buffer as a packed signed 32-bit int field.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001368 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001369 * @param {?Array<number>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001370 */
Austin Schuh40c16522018-10-28 20:27:54 -07001371jspb.BinaryWriter.prototype.writePackedSint32 = function(field, value) {
1372 if (value == null || !value.length) return;
1373 var bookmark = this.beginDelimited_(field);
1374 for (var i = 0; i < value.length; i++) {
1375 this.encoder_.writeZigzagVarint32(value[i]);
1376 }
1377 this.endDelimited_(bookmark);
1378};
Brian Silverman9c614bc2016-02-15 20:20:02 -05001379
1380
1381/**
Austin Schuh40c16522018-10-28 20:27:54 -07001382 * Writes an array of numbers to the buffer as a packed signed 64-bit int field.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001383 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001384 * @param {?Array<number>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001385 */
Austin Schuh40c16522018-10-28 20:27:54 -07001386jspb.BinaryWriter.prototype.writePackedSint64 = function(field, value) {
1387 if (value == null || !value.length) return;
1388 var bookmark = this.beginDelimited_(field);
1389 for (var i = 0; i < value.length; i++) {
1390 this.encoder_.writeZigzagVarint64(value[i]);
1391 }
1392 this.endDelimited_(bookmark);
1393};
1394
1395
1396/**
1397 * Writes an array of decimal strings to the buffer as a packed signed 64-bit
1398 * int field.
1399 * @param {number} field The field number.
1400 * @param {?Array<string>} value The array of decimal strings to write.
1401 */
1402jspb.BinaryWriter.prototype.writePackedSint64String = function(field, value) {
1403 if (value == null || !value.length) return;
1404 var bookmark = this.beginDelimited_(field);
1405 for (var i = 0; i < value.length; i++) {
1406 // TODO(haberman): make lossless
1407 this.encoder_.writeZigzagVarint64(parseInt(value[i], 10));
1408 }
1409 this.endDelimited_(bookmark);
1410};
Brian Silverman9c614bc2016-02-15 20:20:02 -05001411
1412
1413/**
1414 * Writes an array of numbers to the buffer as a packed fixed32 field.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001415 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001416 * @param {?Array<number>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001417 */
Austin Schuh40c16522018-10-28 20:27:54 -07001418jspb.BinaryWriter.prototype.writePackedFixed32 = function(field, value) {
1419 if (value == null || !value.length) return;
1420 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
1421 this.encoder_.writeUnsignedVarint32(value.length * 4);
1422 for (var i = 0; i < value.length; i++) {
1423 this.encoder_.writeUint32(value[i]);
Brian Silverman9c614bc2016-02-15 20:20:02 -05001424 }
1425};
1426
1427
1428/**
1429 * Writes an array of numbers to the buffer as a packed fixed64 field.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001430 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001431 * @param {?Array<number>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001432 */
Austin Schuh40c16522018-10-28 20:27:54 -07001433jspb.BinaryWriter.prototype.writePackedFixed64 = function(field, value) {
1434 if (value == null || !value.length) return;
1435 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
1436 this.encoder_.writeUnsignedVarint32(value.length * 8);
1437 for (var i = 0; i < value.length; i++) {
1438 this.encoder_.writeUint64(value[i]);
1439 }
1440};
1441
1442
1443/**
1444 * Writes an array of numbers represented as strings to the buffer as a packed
1445 * fixed64 field.
1446 * @param {number} field The field number.
1447 * @param {?Array<string>} value The array of strings to write.
1448 */
1449jspb.BinaryWriter.prototype.writePackedFixed64String = function(field, value) {
1450 if (value == null || !value.length) return;
1451 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
1452 this.encoder_.writeUnsignedVarint32(value.length * 8);
1453 for (var i = 0; i < value.length; i++) {
1454 var num = jspb.arith.UInt64.fromString(value[i]);
1455 this.encoder_.writeSplitFixed64(num.lo, num.hi);
Brian Silverman9c614bc2016-02-15 20:20:02 -05001456 }
1457};
1458
1459
1460/**
1461 * Writes an array of numbers to the buffer as a packed sfixed32 field.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001462 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001463 * @param {?Array<number>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001464 */
Austin Schuh40c16522018-10-28 20:27:54 -07001465jspb.BinaryWriter.prototype.writePackedSfixed32 = function(field, value) {
1466 if (value == null || !value.length) return;
1467 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
1468 this.encoder_.writeUnsignedVarint32(value.length * 4);
1469 for (var i = 0; i < value.length; i++) {
1470 this.encoder_.writeInt32(value[i]);
Brian Silverman9c614bc2016-02-15 20:20:02 -05001471 }
1472};
1473
1474
1475/**
1476 * Writes an array of numbers to the buffer as a packed sfixed64 field.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001477 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001478 * @param {?Array<number>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001479 */
Austin Schuh40c16522018-10-28 20:27:54 -07001480jspb.BinaryWriter.prototype.writePackedSfixed64 = function(field, value) {
1481 if (value == null || !value.length) return;
1482 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
1483 this.encoder_.writeUnsignedVarint32(value.length * 8);
1484 for (var i = 0; i < value.length; i++) {
1485 this.encoder_.writeInt64(value[i]);
1486 }
1487};
1488
1489
1490/**
1491 * Writes an array of numbers to the buffer as a packed sfixed64 field.
1492 * @param {number} field The field number.
1493 * @param {?Array<string>} value The array of decimal strings to write.
1494 */
1495jspb.BinaryWriter.prototype.writePackedSfixed64String = function(field, value) {
1496 if (value == null || !value.length) return;
1497 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
1498 this.encoder_.writeUnsignedVarint32(value.length * 8);
1499 for (var i = 0; i < value.length; i++) {
1500 this.encoder_.writeInt64String(value[i]);
Brian Silverman9c614bc2016-02-15 20:20:02 -05001501 }
1502};
1503
1504
1505/**
1506 * Writes an array of numbers to the buffer as a packed float field.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001507 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001508 * @param {?Array<number>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001509 */
Austin Schuh40c16522018-10-28 20:27:54 -07001510jspb.BinaryWriter.prototype.writePackedFloat = function(field, value) {
1511 if (value == null || !value.length) return;
1512 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
1513 this.encoder_.writeUnsignedVarint32(value.length * 4);
1514 for (var i = 0; i < value.length; i++) {
1515 this.encoder_.writeFloat(value[i]);
Brian Silverman9c614bc2016-02-15 20:20:02 -05001516 }
1517};
1518
1519
1520/**
1521 * Writes an array of numbers to the buffer as a packed double field.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001522 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001523 * @param {?Array<number>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001524 */
Austin Schuh40c16522018-10-28 20:27:54 -07001525jspb.BinaryWriter.prototype.writePackedDouble = function(field, value) {
1526 if (value == null || !value.length) return;
1527 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
1528 this.encoder_.writeUnsignedVarint32(value.length * 8);
1529 for (var i = 0; i < value.length; i++) {
1530 this.encoder_.writeDouble(value[i]);
Brian Silverman9c614bc2016-02-15 20:20:02 -05001531 }
1532};
1533
1534
1535/**
1536 * Writes an array of booleans to the buffer as a packed bool field.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001537 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001538 * @param {?Array<boolean>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001539 */
Austin Schuh40c16522018-10-28 20:27:54 -07001540jspb.BinaryWriter.prototype.writePackedBool = function(field, value) {
1541 if (value == null || !value.length) return;
1542 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
1543 this.encoder_.writeUnsignedVarint32(value.length);
1544 for (var i = 0; i < value.length; i++) {
1545 this.encoder_.writeBool(value[i]);
Brian Silverman9c614bc2016-02-15 20:20:02 -05001546 }
1547};
1548
1549
1550/**
1551 * Writes an array of enums to the buffer as a packed enum field.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001552 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001553 * @param {?Array<number>} value The array of ints to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001554 */
Austin Schuh40c16522018-10-28 20:27:54 -07001555jspb.BinaryWriter.prototype.writePackedEnum = function(field, value) {
1556 if (value == null || !value.length) return;
1557 var bookmark = this.beginDelimited_(field);
1558 for (var i = 0; i < value.length; i++) {
1559 this.encoder_.writeEnum(value[i]);
1560 }
1561 this.endDelimited_(bookmark);
1562};
1563
1564
1565/**
1566 * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
1567 * the buffer.
1568 * @param {number} field The field number.
1569 * @param {?Array<string>} value The array of hashes to write.
1570 */
1571jspb.BinaryWriter.prototype.writePackedFixedHash64 = function(field, value) {
1572 if (value == null || !value.length) return;
1573 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
1574 this.encoder_.writeUnsignedVarint32(value.length * 8);
1575 for (var i = 0; i < value.length; i++) {
1576 this.encoder_.writeFixedHash64(value[i]);
Brian Silverman9c614bc2016-02-15 20:20:02 -05001577 }
1578};
1579
1580
1581/**
1582 * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
1583 * the buffer.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001584 * @param {number} field The field number.
Austin Schuh40c16522018-10-28 20:27:54 -07001585 * @param {?Array<string>} value The array of hashes to write.
Brian Silverman9c614bc2016-02-15 20:20:02 -05001586 */
Austin Schuh40c16522018-10-28 20:27:54 -07001587jspb.BinaryWriter.prototype.writePackedVarintHash64 = function(field, value) {
1588 if (value == null || !value.length) return;
1589 var bookmark = this.beginDelimited_(field);
1590 for (var i = 0; i < value.length; i++) {
1591 this.encoder_.writeVarintHash64(value[i]);
Brian Silverman9c614bc2016-02-15 20:20:02 -05001592 }
Austin Schuh40c16522018-10-28 20:27:54 -07001593 this.endDelimited_(bookmark);
Brian Silverman9c614bc2016-02-15 20:20:02 -05001594};