Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(287)

Side by Side Diff: third_party/protobuf/js/binary/writer.js

Issue 1983203003: Update third_party/protobuf to protobuf-v3.0.0-beta-3 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: owners Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Protocol Buffers - Google's data interchange format 1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved. 2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/ 3 // https://developers.google.com/protocol-buffers/
4 // 4 //
5 // Redistribution and use in source and binary forms, with or without 5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are 6 // modification, are permitted provided that the following conditions are
7 // met: 7 // met:
8 // 8 //
9 // * Redistributions of source code must retain the above copyright 9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer. 10 // notice, this list of conditions and the following disclaimer.
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 * browsers that do not support them. 53 * browsers that do not support them.
54 * 54 *
55 * @author aappleby@google.com (Austin Appleby) 55 * @author aappleby@google.com (Austin Appleby)
56 */ 56 */
57 57
58 goog.provide('jspb.BinaryWriter'); 58 goog.provide('jspb.BinaryWriter');
59 59
60 goog.require('goog.asserts'); 60 goog.require('goog.asserts');
61 goog.require('goog.crypt.base64'); 61 goog.require('goog.crypt.base64');
62 goog.require('jspb.BinaryConstants'); 62 goog.require('jspb.BinaryConstants');
63 goog.require('jspb.BinaryEncoder');
63 goog.require('jspb.arith.Int64'); 64 goog.require('jspb.arith.Int64');
64 goog.require('jspb.arith.UInt64'); 65 goog.require('jspb.arith.UInt64');
65 goog.require('jspb.utils'); 66 goog.require('jspb.utils');
66 67
67 goog.forwardDeclare('jspb.Message');
68
69 68
70 69
71 /** 70 /**
72 * BinaryWriter implements encoders for all the wire types specified in 71 * BinaryWriter implements encoders for all the wire types specified in
73 * https://developers.google.com/protocol-buffers/docs/encoding. 72 * https://developers.google.com/protocol-buffers/docs/encoding.
74 * 73 *
75 * @constructor 74 * @constructor
76 * @struct 75 * @struct
77 */ 76 */
78 jspb.BinaryWriter = function() { 77 jspb.BinaryWriter = function() {
79 /** 78 /**
80 * Blocks of serialized data that will be concatenated once all messages have 79 * Blocks of serialized data that will be concatenated once all messages have
81 * been written. 80 * been written.
82 * @private {!Array<!Uint8Array|!Array<number>>} 81 * @private {!Array<!Uint8Array|!Array<number>>}
83 */ 82 */
84 this.blocks_ = []; 83 this.blocks_ = [];
85 84
86 /** 85 /**
87 * Total number of bytes in the blocks_ array. Does _not_ include the temp 86 * Total number of bytes in the blocks_ array. Does _not_ include bytes in
88 * buffer. 87 * the encoder below.
89 * @private {number} 88 * @private {number}
90 */ 89 */
91 this.totalLength_ = 0; 90 this.totalLength_ = 0;
92 91
93 /** 92 /**
94 * Temporary buffer holding a message that we're still serializing. When we 93 * Binary encoder holding pieces of a message that we're still serializing.
95 * get to a stopping point (either the start of a new submessage, or when we 94 * When we get to a stopping point (either the start of a new submessage, or
96 * need to append a raw Uint8Array), the temp buffer will be added to the 95 * when we need to append a raw Uint8Array), the encoder's buffer will be
97 * block array above and a new temp buffer will be created. 96 * added to the block array above and the encoder will be reset.
98 * @private {!Array.<number>} 97 * @private {!jspb.BinaryEncoder}
99 */ 98 */
100 this.temp_ = []; 99 this.encoder_ = new jspb.BinaryEncoder();
101 100
102 /** 101 /**
103 * A stack of bookmarks containing the parent blocks for each message started 102 * A stack of bookmarks containing the parent blocks for each message started
104 * via beginSubMessage(), needed as bookkeeping for endSubMessage(). 103 * via beginSubMessage(), needed as bookkeeping for endSubMessage().
105 * TODO(aappleby): Deprecated, users should be calling writeMessage(). 104 * TODO(aappleby): Deprecated, users should be calling writeMessage().
106 * @private {!Array.<!jspb.BinaryWriter.Bookmark_>} 105 * @private {!Array.<!Array.<number>>}
107 */ 106 */
108 this.bookmarks_ = []; 107 this.bookmarks_ = [];
109 }; 108 };
110 109
111 110
112 /** 111 /**
113 * @typedef {{block: !Array.<number>, length: number}}
114 * @private
115 */
116 jspb.BinaryWriter.Bookmark_;
117
118
119 /**
120 * Saves the current temp buffer in the blocks_ array and starts a new one.
121 * @return {!Array.<number>} Returns a reference to the old temp buffer.
122 * @private
123 */
124 jspb.BinaryWriter.prototype.saveTempBuffer_ = function() {
125 var oldTemp = this.temp_;
126 this.blocks_.push(this.temp_);
127 this.totalLength_ += this.temp_.length;
128 this.temp_ = [];
129 return oldTemp;
130 };
131
132
133 /**
134 * Append a typed array of bytes onto the buffer. 112 * Append a typed array of bytes onto the buffer.
135 * 113 *
136 * @param {!Uint8Array} arr The byte array to append. 114 * @param {!Uint8Array} arr The byte array to append.
137 * @private 115 * @private
138 */ 116 */
139 jspb.BinaryWriter.prototype.appendUint8Array_ = function(arr) { 117 jspb.BinaryWriter.prototype.appendUint8Array_ = function(arr) {
140 if (this.temp_.length) { 118 var temp = this.encoder_.end();
141 this.saveTempBuffer_(); 119 this.blocks_.push(temp);
142 }
143 this.blocks_.push(arr); 120 this.blocks_.push(arr);
144 this.totalLength_ += arr.length; 121 this.totalLength_ += temp.length + arr.length;
145 }; 122 };
146 123
147 124
148 /** 125 /**
149 * Append an untyped array of bytes onto the buffer. 126 * Begins a new message by writing the field header and returning a bookmark
150 * 127 * which we will use to patch in the message length to in endDelimited_ below.
151 * @param {!Array.<number>} arr The byte array to append. 128 * @param {number} field
129 * @return {!Array.<number>}
152 * @private 130 * @private
153 */ 131 */
154 jspb.BinaryWriter.prototype.appendArray_ = function(arr) { 132 jspb.BinaryWriter.prototype.beginDelimited_ = function(field) {
155 if (this.temp_.length) { 133 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
156 this.saveTempBuffer_(); 134 var bookmark = this.encoder_.end();
157 } 135 this.blocks_.push(bookmark);
158 this.temp_ = arr; 136 this.totalLength_ += bookmark.length;
137 bookmark.push(this.totalLength_);
138 return bookmark;
159 }; 139 };
160 140
161 141
162 /** 142 /**
163 * Begins a length-delimited section by writing the field header to the current 143 * Ends a message by encoding the _change_ in length of the buffer to the
164 * temp buffer and then saving it in the block array. Returns the saved block, 144 * parent block and adds the number of bytes needed to encode that length to
165 * which we will append the length to in endDelimited_ below. 145 * the total byte length.
166 * @param {number} field 146 * @param {!Array.<number>} bookmark
167 * @return {!jspb.BinaryWriter.Bookmark_}
168 * @private 147 * @private
169 */ 148 */
170 jspb.BinaryWriter.prototype.beginDelimited_ = function(field) { 149 jspb.BinaryWriter.prototype.endDelimited_ = function(bookmark) {
171 this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); 150 var oldLength = bookmark.pop();
172 return {block: this.saveTempBuffer_(), length: this.totalLength_}; 151 var messageLength = this.totalLength_ + this.encoder_.length() - oldLength;
152 goog.asserts.assert(messageLength >= 0);
153
154 while (messageLength > 127) {
155 bookmark.push((messageLength & 0x7f) | 0x80);
156 messageLength = messageLength >>> 7;
157 this.totalLength_++;
158 }
159
160 bookmark.push(messageLength);
161 this.totalLength_++;
173 }; 162 };
174 163
175 164
176 /** 165 /**
177 * Ends a length-delimited block by encoding the _change_ in length of the 166 * Writes a pre-serialized message to the buffer.
178 * buffer to the parent block and adds the number of bytes needed to encode 167 * @param {!Uint8Array} bytes The array of bytes to write.
179 * that length to the total byte length. Note that 'parentLength' _must_ be the 168 * @param {number} start The start of the range to write.
180 * total length _after_ the field header was written in beginDelimited_ above. 169 * @param {number} end The end of the range to write.
181 * @param {!jspb.BinaryWriter.Bookmark_} bookmark
182 * @private
183 */ 170 */
184 jspb.BinaryWriter.prototype.endDelimited_ = function(bookmark) { 171 jspb.BinaryWriter.prototype.writeSerializedMessage = function(
185 var messageLength = this.totalLength_ + this.temp_.length - bookmark.length; 172 bytes, start, end) {
186 goog.asserts.assert(messageLength >= 0); 173 this.appendUint8Array_(bytes.subarray(start, end));
187
188 var bytes = 1;
189 while (messageLength > 127) {
190 bookmark.block.push((messageLength & 0x7f) | 0x80);
191 messageLength = messageLength >>> 7;
192 bytes++;
193 }
194
195 bookmark.block.push(messageLength);
196 this.totalLength_ += bytes;
197 }; 174 };
198 175
199 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 */
184 jspb.BinaryWriter.prototype.maybeWriteSerializedMessage = function(
185 bytes, start, end) {
186 if (bytes != null && start != null && end != null) {
187 this.writeSerializedMessage(bytes, start, end);
188 }
189 };
190
191
200 /** 192 /**
201 * Resets the writer, throwing away any accumulated buffers. 193 * Resets the writer, throwing away any accumulated buffers.
202 */ 194 */
203 jspb.BinaryWriter.prototype.reset = function() { 195 jspb.BinaryWriter.prototype.reset = function() {
204 this.blocks_ = []; 196 this.blocks_ = [];
205 this.temp_ = []; 197 this.encoder_.end();
206 this.totalLength_ = 0; 198 this.totalLength_ = 0;
207 this.bookmarks_ = []; 199 this.bookmarks_ = [];
208 }; 200 };
209 201
210 202
211 /** 203 /**
212 * Converts the encoded data into a Uint8Array. 204 * Converts the encoded data into a Uint8Array.
213 * @return {!Uint8Array} 205 * @return {!Uint8Array}
214 */ 206 */
215 jspb.BinaryWriter.prototype.getResultBuffer = function() { 207 jspb.BinaryWriter.prototype.getResultBuffer = function() {
216 goog.asserts.assert(this.bookmarks_.length == 0); 208 goog.asserts.assert(this.bookmarks_.length == 0);
217 209
218 var flat = new Uint8Array(this.totalLength_ + this.temp_.length); 210 var flat = new Uint8Array(this.totalLength_ + this.encoder_.length());
219 211
220 var blocks = this.blocks_; 212 var blocks = this.blocks_;
221 var blockCount = blocks.length; 213 var blockCount = blocks.length;
222 var offset = 0; 214 var offset = 0;
223 215
224 for (var i = 0; i < blockCount; i++) { 216 for (var i = 0; i < blockCount; i++) {
225 var block = blocks[i]; 217 var block = blocks[i];
226 flat.set(block, offset); 218 flat.set(block, offset);
227 offset += block.length; 219 offset += block.length;
228 } 220 }
229 221
230 flat.set(this.temp_, offset); 222 var tail = this.encoder_.end();
231 offset += this.temp_.length; 223 flat.set(tail, offset);
224 offset += tail.length;
232 225
233 // Post condition: `flattened` must have had every byte written. 226 // Post condition: `flattened` must have had every byte written.
234 goog.asserts.assert(offset == flat.length); 227 goog.asserts.assert(offset == flat.length);
235 228
236 // Replace our block list with the flattened block, which lets GC reclaim 229 // Replace our block list with the flattened block, which lets GC reclaim
237 // the temp blocks sooner. 230 // the temp blocks sooner.
238 this.blocks_ = [flat]; 231 this.blocks_ = [flat];
239 this.temp_ = [];
240 232
241 return flat; 233 return flat;
242 }; 234 };
243 235
244 236
245 /** 237 /**
246 * Converts the encoded data into a bas64-encoded string. 238 * Converts the encoded data into a bas64-encoded string.
247 * @return {string} 239 * @return {string}
248 */ 240 */
249 jspb.BinaryWriter.prototype.getResultBase64String = function() { 241 jspb.BinaryWriter.prototype.getResultBase64String = function() {
(...skipping 16 matching lines...) Expand all
266 * Finishes a sub-message and packs it into the parent messages' buffer. 258 * Finishes a sub-message and packs it into the parent messages' buffer.
267 * TODO(aappleby): Deprecated. Move callers to writeMessage(). 259 * TODO(aappleby): Deprecated. Move callers to writeMessage().
268 */ 260 */
269 jspb.BinaryWriter.prototype.endSubMessage = function() { 261 jspb.BinaryWriter.prototype.endSubMessage = function() {
270 goog.asserts.assert(this.bookmarks_.length >= 0); 262 goog.asserts.assert(this.bookmarks_.length >= 0);
271 this.endDelimited_(this.bookmarks_.pop()); 263 this.endDelimited_(this.bookmarks_.pop());
272 }; 264 };
273 265
274 266
275 /** 267 /**
276 * Encodes a 32-bit unsigned integer into its wire-format varint representation
277 * and stores it in the buffer.
278 * @param {number} value The integer to convert.
279 */
280 jspb.BinaryWriter.prototype.rawWriteUnsignedVarint32 = function(value) {
281 goog.asserts.assert(value == Math.floor(value));
282
283 while (value > 127) {
284 this.temp_.push((value & 0x7f) | 0x80);
285 value = value >>> 7;
286 }
287
288 this.temp_.push(value);
289 };
290
291
292 /**
293 * Encodes a 32-bit signed integer into its wire-format varint representation
294 * and stores it in the buffer.
295 * @param {number} value The integer to convert.
296 */
297 jspb.BinaryWriter.prototype.rawWriteSignedVarint32 = function(value) {
298 goog.asserts.assert(value == Math.floor(value));
299 if (value >= 0) {
300 this.rawWriteUnsignedVarint32(value);
301 return;
302 }
303
304 // Write nine bytes with a _signed_ right shift so we preserve the sign bit.
305 for (var i = 0; i < 9; i++) {
306 this.temp_.push((value & 0x7f) | 0x80);
307 value = value >> 7;
308 }
309
310 // The above loop writes out 63 bits, so the last byte is always the sign bit
311 // which is always set for negative numbers.
312 this.temp_.push(1);
313 };
314
315
316 /**
317 * Encodes an unsigned 64-bit integer in 32:32 split representation into its
318 * wire-format varint representation and stores it in the buffer.
319 * @param {number} lowBits The low 32 bits of the int.
320 * @param {number} highBits The high 32 bits of the int.
321 */
322 jspb.BinaryWriter.prototype.rawWriteSplitVarint =
323 function(lowBits, highBits) {
324 // Break the binary representation into chunks of 7 bits, set the 8th bit
325 // in each chunk if it's not the final chunk, and append to the result.
326 while (highBits > 0 || lowBits > 127) {
327 this.temp_.push((lowBits & 0x7f) | 0x80);
328 lowBits = ((lowBits >>> 7) | (highBits << 25)) >>> 0;
329 highBits = highBits >>> 7;
330 }
331 this.temp_.push(lowBits);
332 };
333
334
335 /**
336 * Encodes a JavaScript integer into its wire-format varint representation and
337 * stores it in the buffer. Due to the way the varint encoding works this
338 * behaves correctly for both signed and unsigned integers, though integers
339 * that are not representable in 64 bits will still be truncated.
340 * @param {number} value The integer to convert.
341 */
342 jspb.BinaryWriter.prototype.rawWriteVarint = function(value) {
343 goog.asserts.assert(value == Math.floor(value));
344 jspb.utils.splitInt64(value);
345 this.rawWriteSplitVarint(jspb.utils.split64Low,
346 jspb.utils.split64High);
347 };
348
349
350 /**
351 * Encodes a jspb.arith.{Int64,UInt64} instance into its wire-format
352 * varint representation and stores it in the buffer. Due to the way the varint
353 * encoding works this behaves correctly for both signed and unsigned integers,
354 * though integers that are not representable in 64 bits will still be
355 * truncated.
356 * @param {jspb.arith.Int64|jspb.arith.UInt64} value
357 */
358 jspb.BinaryWriter.prototype.rawWriteVarintFromNum = function(value) {
359 this.rawWriteSplitVarint(value.lo, value.hi);
360 };
361
362
363 /**
364 * Encodes a JavaScript integer into its wire-format, zigzag-encoded varint
365 * representation and stores it in the buffer.
366 * @param {number} value The integer to convert.
367 */
368 jspb.BinaryWriter.prototype.rawWriteZigzagVarint32 = function(value) {
369 goog.asserts.assert(value == Math.floor(value));
370 this.rawWriteUnsignedVarint32(((value << 1) ^ (value >> 31)) >>> 0);
371 };
372
373
374 /**
375 * Encodes a JavaScript integer into its wire-format, zigzag-encoded varint
376 * representation and stores it in the buffer. Integers not representable in 64
377 * bits will be truncated.
378 * @param {number} value The integer to convert.
379 */
380 jspb.BinaryWriter.prototype.rawWriteZigzagVarint = function(value) {
381 goog.asserts.assert(value == Math.floor(value));
382 jspb.utils.splitZigzag64(value);
383 this.rawWriteSplitVarint(jspb.utils.split64Low,
384 jspb.utils.split64High);
385 };
386
387
388 /**
389 * Writes a raw 8-bit unsigned integer to the buffer. Numbers outside the range
390 * [0,2^8) will be truncated.
391 * @param {number} value The value to write.
392 */
393 jspb.BinaryWriter.prototype.rawWriteUint8 = function(value) {
394 goog.asserts.assert(value == Math.floor(value));
395 goog.asserts.assert((value >= 0) && (value < 256));
396 this.temp_.push((value >>> 0) & 0xFF);
397 };
398
399
400 /**
401 * Writes a raw 16-bit unsigned integer to the buffer. Numbers outside the
402 * range [0,2^16) will be truncated.
403 * @param {number} value The value to write.
404 */
405 jspb.BinaryWriter.prototype.rawWriteUint16 = function(value) {
406 goog.asserts.assert(value == Math.floor(value));
407 goog.asserts.assert((value >= 0) && (value < 65536));
408 this.temp_.push((value >>> 0) & 0xFF);
409 this.temp_.push((value >>> 8) & 0xFF);
410 };
411
412
413 /**
414 * Writes a raw 32-bit unsigned integer to the buffer. Numbers outside the
415 * range [0,2^32) will be truncated.
416 * @param {number} value The value to write.
417 */
418 jspb.BinaryWriter.prototype.rawWriteUint32 = function(value) {
419 goog.asserts.assert(value == Math.floor(value));
420 goog.asserts.assert((value >= 0) &&
421 (value < jspb.BinaryConstants.TWO_TO_32));
422 this.temp_.push((value >>> 0) & 0xFF);
423 this.temp_.push((value >>> 8) & 0xFF);
424 this.temp_.push((value >>> 16) & 0xFF);
425 this.temp_.push((value >>> 24) & 0xFF);
426 };
427
428
429 /**
430 * Writes a raw 64-bit unsigned integer to the buffer. Numbers outside the
431 * range [0,2^64) will be truncated.
432 * @param {number} value The value to write.
433 */
434 jspb.BinaryWriter.prototype.rawWriteUint64 = function(value) {
435 goog.asserts.assert(value == Math.floor(value));
436 goog.asserts.assert((value >= 0) &&
437 (value < jspb.BinaryConstants.TWO_TO_64));
438 jspb.utils.splitUint64(value);
439 this.rawWriteUint32(jspb.utils.split64Low);
440 this.rawWriteUint32(jspb.utils.split64High);
441 };
442
443
444 /**
445 * Writes a raw 8-bit integer to the buffer. Numbers outside the range
446 * [-2^7,2^7) will be truncated.
447 * @param {number} value The value to write.
448 */
449 jspb.BinaryWriter.prototype.rawWriteInt8 = function(value) {
450 goog.asserts.assert(value == Math.floor(value));
451 goog.asserts.assert((value >= -128) && (value < 128));
452 this.temp_.push((value >>> 0) & 0xFF);
453 };
454
455
456 /**
457 * Writes a raw 16-bit integer to the buffer. Numbers outside the range
458 * [-2^15,2^15) will be truncated.
459 * @param {number} value The value to write.
460 */
461 jspb.BinaryWriter.prototype.rawWriteInt16 = function(value) {
462 goog.asserts.assert(value == Math.floor(value));
463 goog.asserts.assert((value >= -32768) && (value < 32768));
464 this.temp_.push((value >>> 0) & 0xFF);
465 this.temp_.push((value >>> 8) & 0xFF);
466 };
467
468
469 /**
470 * Writes a raw 32-bit integer to the buffer. Numbers outside the range
471 * [-2^31,2^31) will be truncated.
472 * @param {number} value The value to write.
473 */
474 jspb.BinaryWriter.prototype.rawWriteInt32 = function(value) {
475 goog.asserts.assert(value == Math.floor(value));
476 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
477 (value < jspb.BinaryConstants.TWO_TO_31));
478 this.temp_.push((value >>> 0) & 0xFF);
479 this.temp_.push((value >>> 8) & 0xFF);
480 this.temp_.push((value >>> 16) & 0xFF);
481 this.temp_.push((value >>> 24) & 0xFF);
482 };
483
484
485 /**
486 * Writes a raw 64-bit integer to the buffer. Numbers outside the range
487 * [-2^63,2^63) will be truncated.
488 * @param {number} value The value to write.
489 */
490 jspb.BinaryWriter.prototype.rawWriteInt64 = function(value) {
491 goog.asserts.assert(value == Math.floor(value));
492 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
493 (value < jspb.BinaryConstants.TWO_TO_63));
494 jspb.utils.splitInt64(value);
495 this.rawWriteUint32(jspb.utils.split64Low);
496 this.rawWriteUint32(jspb.utils.split64High);
497 };
498
499
500 /**
501 * Writes a raw single-precision floating point value to the buffer. Numbers
502 * requiring more than 32 bits of precision will be truncated.
503 * @param {number} value The value to write.
504 */
505 jspb.BinaryWriter.prototype.rawWriteFloat = function(value) {
506 jspb.utils.splitFloat32(value);
507 this.rawWriteUint32(jspb.utils.split64Low);
508 };
509
510
511 /**
512 * Writes a raw double-precision floating point value to the buffer. As this is
513 * the native format used by JavaScript, no precision will be lost.
514 * @param {number} value The value to write.
515 */
516 jspb.BinaryWriter.prototype.rawWriteDouble = function(value) {
517 jspb.utils.splitFloat64(value);
518 this.rawWriteUint32(jspb.utils.split64Low);
519 this.rawWriteUint32(jspb.utils.split64High);
520 };
521
522
523 /**
524 * Writes a raw boolean value to the buffer as a varint.
525 * @param {boolean} value The value to write.
526 */
527 jspb.BinaryWriter.prototype.rawWriteBool = function(value) {
528 goog.asserts.assert(goog.isBoolean(value));
529 this.temp_.push(~~value);
530 };
531
532
533 /**
534 * Writes an raw enum value to the buffer as a varint.
535 * @param {number} value The value to write.
536 */
537 jspb.BinaryWriter.prototype.rawWriteEnum = function(value) {
538 goog.asserts.assert(value == Math.floor(value));
539 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
540 (value < jspb.BinaryConstants.TWO_TO_31));
541 this.rawWriteSignedVarint32(value);
542 };
543
544
545 /**
546 * Writes a raw string value to the buffer.
547 * @param {string} string The string to write.
548 */
549 jspb.BinaryWriter.prototype.rawWriteUtf8String = function(string) {
550 for (var i = 0; i < string.length; i++) {
551 this.temp_.push(string.charCodeAt(i));
552 }
553 };
554
555
556 /**
557 * Writes an arbitrary raw byte array to the buffer.
558 * @param {!Uint8Array} bytes The array of bytes to write.
559 */
560 jspb.BinaryWriter.prototype.rawWriteBytes = function(bytes) {
561 this.appendUint8Array_(bytes);
562 };
563
564
565 /**
566 * Writes an arbitrary raw byte array to the buffer.
567 * @param {!Uint8Array} bytes The array of bytes to write.
568 * @param {number} start The start of the range to write.
569 * @param {number} end The end of the range to write.
570 */
571 jspb.BinaryWriter.prototype.rawWriteByteRange = function(bytes, start, end) {
572 this.appendUint8Array_(bytes.subarray(start, end));
573 };
574
575
576 /**
577 * Writes a 64-bit hash string (8 characters @ 8 bits of data each) to the
578 * buffer as a varint.
579 * @param {string} hash The hash to write.
580 */
581 jspb.BinaryWriter.prototype.rawWriteVarintHash64 = function(hash) {
582 jspb.utils.splitHash64(hash);
583 this.rawWriteSplitVarint(jspb.utils.split64Low,
584 jspb.utils.split64High);
585 };
586
587
588 /**
589 * Writes a 64-bit hash string (8 characters @ 8 bits of data each) to the
590 * buffer as a fixed64.
591 * @param {string} hash The hash to write.
592 */
593 jspb.BinaryWriter.prototype.rawWriteFixedHash64 = function(hash) {
594 jspb.utils.splitHash64(hash);
595 this.rawWriteUint32(jspb.utils.split64Low);
596 this.rawWriteUint32(jspb.utils.split64High);
597 };
598
599
600 /**
601 * Encodes a (field number, wire type) tuple into a wire-format field header 268 * Encodes a (field number, wire type) tuple into a wire-format field header
602 * and stores it in the buffer as a varint. 269 * and stores it in the buffer as a varint.
603 * @param {number} field The field number. 270 * @param {number} field The field number.
604 * @param {number} wireType The wire-type of the field, as specified in the 271 * @param {number} wireType The wire-type of the field, as specified in the
605 * protocol buffer documentation. 272 * protocol buffer documentation.
606 * @private 273 * @private
607 */ 274 */
608 jspb.BinaryWriter.prototype.rawWriteFieldHeader_ = 275 jspb.BinaryWriter.prototype.writeFieldHeader_ =
609 function(field, wireType) { 276 function(field, wireType) {
610 goog.asserts.assert(field >= 1 && field == Math.floor(field)); 277 goog.asserts.assert(field >= 1 && field == Math.floor(field));
611 var x = field * 8 + wireType; 278 var x = field * 8 + wireType;
612 this.rawWriteUnsignedVarint32(x); 279 this.encoder_.writeUnsignedVarint32(x);
613 }; 280 };
614 281
615 282
616 /** 283 /**
617 * Writes a field of any valid scalar type to the binary stream. 284 * Writes a field of any valid scalar type to the binary stream.
618 * @param {jspb.BinaryConstants.FieldType} fieldType 285 * @param {jspb.BinaryConstants.FieldType} fieldType
619 * @param {number} field 286 * @param {number} field
620 * @param {jspb.AnyFieldType} value 287 * @param {jspb.AnyFieldType} value
621 */ 288 */
622 jspb.BinaryWriter.prototype.writeAny = function(fieldType, field, value) { 289 jspb.BinaryWriter.prototype.writeAny = function(fieldType, field, value) {
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
690 357
691 358
692 /** 359 /**
693 * Writes a varint field to the buffer without range checking. 360 * Writes a varint field to the buffer without range checking.
694 * @param {number} field The field number. 361 * @param {number} field The field number.
695 * @param {number?} value The value to write. 362 * @param {number?} value The value to write.
696 * @private 363 * @private
697 */ 364 */
698 jspb.BinaryWriter.prototype.writeUnsignedVarint32_ = function(field, value) { 365 jspb.BinaryWriter.prototype.writeUnsignedVarint32_ = function(field, value) {
699 if (value == null) return; 366 if (value == null) return;
700 this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); 367 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
701 this.rawWriteSignedVarint32(value); 368 this.encoder_.writeUnsignedVarint32(value);
702 }; 369 };
703 370
704 371
705 /** 372 /**
706 * Writes a varint field to the buffer without range checking. 373 * Writes a varint field to the buffer without range checking.
707 * @param {number} field The field number. 374 * @param {number} field The field number.
708 * @param {number?} value The value to write. 375 * @param {number?} value The value to write.
709 * @private 376 * @private
710 */ 377 */
711 jspb.BinaryWriter.prototype.writeSignedVarint32_ = function(field, value) { 378 jspb.BinaryWriter.prototype.writeSignedVarint32_ = function(field, value) {
712 if (value == null) return; 379 if (value == null) return;
713 this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); 380 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
714 this.rawWriteSignedVarint32(value); 381 this.encoder_.writeSignedVarint32(value);
715 }; 382 };
716 383
717 384
385 /**
386 * Writes a varint field to the buffer without range checking.
387 * @param {number} field The field number.
388 * @param {number?} value The value to write.
389 * @private
390 */
391 jspb.BinaryWriter.prototype.writeUnsignedVarint64_ = function(field, value) {
392 if (value == null) return;
393 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
394 this.encoder_.writeUnsignedVarint64(value);
395 };
396
397
718 /** 398 /**
719 * Writes a varint field to the buffer without range checking. 399 * Writes a varint field to the buffer without range checking.
720 * @param {number} field The field number. 400 * @param {number} field The field number.
721 * @param {number?} value The value to write. 401 * @param {number?} value The value to write.
722 * @private 402 * @private
723 */ 403 */
724 jspb.BinaryWriter.prototype.writeVarint_ = function(field, value) { 404 jspb.BinaryWriter.prototype.writeSignedVarint64_ = function(field, value) {
725 if (value == null) return; 405 if (value == null) return;
726 this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); 406 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
727 this.rawWriteVarint(value); 407 this.encoder_.writeSignedVarint64(value);
728 }; 408 };
729 409
730 410
731 /** 411 /**
732 * Writes a zigzag varint field to the buffer without range checking. 412 * Writes a zigzag varint field to the buffer without range checking.
733 * @param {number} field The field number. 413 * @param {number} field The field number.
734 * @param {number?} value The value to write. 414 * @param {number?} value The value to write.
735 * @private 415 * @private
736 */ 416 */
737 jspb.BinaryWriter.prototype.writeZigzagVarint32_ = function(field, value) { 417 jspb.BinaryWriter.prototype.writeZigzagVarint32_ = function(field, value) {
738 if (value == null) return; 418 if (value == null) return;
739 this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); 419 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
740 this.rawWriteZigzagVarint32(value); 420 this.encoder_.writeZigzagVarint32(value);
741 }; 421 };
742 422
743 423
744 /** 424 /**
745 * Writes a zigzag varint field to the buffer without range checking. 425 * Writes a zigzag varint field to the buffer without range checking.
746 * @param {number} field The field number. 426 * @param {number} field The field number.
747 * @param {number?} value The value to write. 427 * @param {number?} value The value to write.
748 * @private 428 * @private
749 */ 429 */
750 jspb.BinaryWriter.prototype.writeZigzagVarint_ = function(field, value) { 430 jspb.BinaryWriter.prototype.writeZigzagVarint64_ = function(field, value) {
751 if (value == null) return; 431 if (value == null) return;
752 this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); 432 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
753 this.rawWriteZigzagVarint(value); 433 this.encoder_.writeZigzagVarint64(value);
754 }; 434 };
755 435
756 436
757 /** 437 /**
758 * Writes an int32 field to the buffer. Numbers outside the range [-2^31,2^31) 438 * Writes an int32 field to the buffer. Numbers outside the range [-2^31,2^31)
759 * will be truncated. 439 * will be truncated.
760 * @param {number} field The field number. 440 * @param {number} field The field number.
761 * @param {number?} value The value to write. 441 * @param {number?} value The value to write.
762 */ 442 */
763 jspb.BinaryWriter.prototype.writeInt32 = function(field, value) { 443 jspb.BinaryWriter.prototype.writeInt32 = function(field, value) {
(...skipping 22 matching lines...) Expand all
786 /** 466 /**
787 * Writes an int64 field to the buffer. Numbers outside the range [-2^63,2^63) 467 * Writes an int64 field to the buffer. Numbers outside the range [-2^63,2^63)
788 * will be truncated. 468 * will be truncated.
789 * @param {number} field The field number. 469 * @param {number} field The field number.
790 * @param {number?} value The value to write. 470 * @param {number?} value The value to write.
791 */ 471 */
792 jspb.BinaryWriter.prototype.writeInt64 = function(field, value) { 472 jspb.BinaryWriter.prototype.writeInt64 = function(field, value) {
793 if (value == null) return; 473 if (value == null) return;
794 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) && 474 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
795 (value < jspb.BinaryConstants.TWO_TO_63)); 475 (value < jspb.BinaryConstants.TWO_TO_63));
796 this.writeVarint_(field, value); 476 this.writeSignedVarint64_(field, value);
797 }; 477 };
798 478
799 479
800 /** 480 /**
801 * Writes a int64 field (with value as a string) to the buffer. 481 * Writes a int64 field (with value as a string) to the buffer.
802 * @param {number} field The field number. 482 * @param {number} field The field number.
803 * @param {string?} value The value to write. 483 * @param {string?} value The value to write.
804 */ 484 */
805 jspb.BinaryWriter.prototype.writeInt64String = function(field, value) { 485 jspb.BinaryWriter.prototype.writeInt64String = function(field, value) {
806 if (value == null) return; 486 if (value == null) return;
807 var num = jspb.arith.Int64.fromString(value); 487 var num = jspb.arith.Int64.fromString(value);
808 this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); 488 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
809 this.rawWriteVarintFromNum(num); 489 this.encoder_.writeSplitVarint64(num.lo, num.hi);
810 }; 490 };
811 491
812 492
813 /** 493 /**
814 * Writes a uint32 field to the buffer. Numbers outside the range [0,2^32) 494 * Writes a uint32 field to the buffer. Numbers outside the range [0,2^32)
815 * will be truncated. 495 * will be truncated.
816 * @param {number} field The field number. 496 * @param {number} field The field number.
817 * @param {number?} value The value to write. 497 * @param {number?} value The value to write.
818 */ 498 */
819 jspb.BinaryWriter.prototype.writeUint32 = function(field, value) { 499 jspb.BinaryWriter.prototype.writeUint32 = function(field, value) {
(...skipping 22 matching lines...) Expand all
842 /** 522 /**
843 * Writes a uint64 field to the buffer. Numbers outside the range [0,2^64) 523 * Writes a uint64 field to the buffer. Numbers outside the range [0,2^64)
844 * will be truncated. 524 * will be truncated.
845 * @param {number} field The field number. 525 * @param {number} field The field number.
846 * @param {number?} value The value to write. 526 * @param {number?} value The value to write.
847 */ 527 */
848 jspb.BinaryWriter.prototype.writeUint64 = function(field, value) { 528 jspb.BinaryWriter.prototype.writeUint64 = function(field, value) {
849 if (value == null) return; 529 if (value == null) return;
850 goog.asserts.assert((value >= 0) && 530 goog.asserts.assert((value >= 0) &&
851 (value < jspb.BinaryConstants.TWO_TO_64)); 531 (value < jspb.BinaryConstants.TWO_TO_64));
852 this.writeVarint_(field, value); 532 this.writeUnsignedVarint64_(field, value);
853 }; 533 };
854 534
855 535
856 /** 536 /**
857 * Writes a uint64 field (with value as a string) to the buffer. 537 * Writes a uint64 field (with value as a string) to the buffer.
858 * @param {number} field The field number. 538 * @param {number} field The field number.
859 * @param {string?} value The value to write. 539 * @param {string?} value The value to write.
860 */ 540 */
861 jspb.BinaryWriter.prototype.writeUint64String = function(field, value) { 541 jspb.BinaryWriter.prototype.writeUint64String = function(field, value) {
862 if (value == null) return; 542 if (value == null) return;
863 var num = jspb.arith.UInt64.fromString(value); 543 var num = jspb.arith.UInt64.fromString(value);
864 this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); 544 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
865 this.rawWriteVarintFromNum(num); 545 this.encoder_.writeSplitVarint64(num.lo, num.hi);
866 }; 546 };
867 547
868 548
869 /** 549 /**
870 * Writes a sint32 field to the buffer. Numbers outside the range [-2^31,2^31) 550 * Writes a sint32 field to the buffer. Numbers outside the range [-2^31,2^31)
871 * will be truncated. 551 * will be truncated.
872 * @param {number} field The field number. 552 * @param {number} field The field number.
873 * @param {number?} value The value to write. 553 * @param {number?} value The value to write.
874 */ 554 */
875 jspb.BinaryWriter.prototype.writeSint32 = function(field, value) { 555 jspb.BinaryWriter.prototype.writeSint32 = function(field, value) {
876 if (value == null) return; 556 if (value == null) return;
877 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) && 557 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
878 (value < jspb.BinaryConstants.TWO_TO_31)); 558 (value < jspb.BinaryConstants.TWO_TO_31));
879 this.writeZigzagVarint32_(field, value); 559 this.writeZigzagVarint32_(field, value);
880 }; 560 };
881 561
882 562
883 /** 563 /**
884 * Writes a sint64 field to the buffer. Numbers outside the range [-2^63,2^63) 564 * Writes a sint64 field to the buffer. Numbers outside the range [-2^63,2^63)
885 * will be truncated. 565 * will be truncated.
886 * @param {number} field The field number. 566 * @param {number} field The field number.
887 * @param {number?} value The value to write. 567 * @param {number?} value The value to write.
888 */ 568 */
889 jspb.BinaryWriter.prototype.writeSint64 = function(field, value) { 569 jspb.BinaryWriter.prototype.writeSint64 = function(field, value) {
890 if (value == null) return; 570 if (value == null) return;
891 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) && 571 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
892 (value < jspb.BinaryConstants.TWO_TO_63)); 572 (value < jspb.BinaryConstants.TWO_TO_63));
893 this.writeZigzagVarint_(field, value); 573 this.writeZigzagVarint64_(field, value);
894 }; 574 };
895 575
896 576
897 /** 577 /**
898 * Writes a fixed32 field to the buffer. Numbers outside the range [0,2^32) 578 * Writes a fixed32 field to the buffer. Numbers outside the range [0,2^32)
899 * will be truncated. 579 * will be truncated.
900 * @param {number} field The field number. 580 * @param {number} field The field number.
901 * @param {number?} value The value to write. 581 * @param {number?} value The value to write.
902 */ 582 */
903 jspb.BinaryWriter.prototype.writeFixed32 = function(field, value) { 583 jspb.BinaryWriter.prototype.writeFixed32 = function(field, value) {
904 if (value == null) return; 584 if (value == null) return;
905 goog.asserts.assert((value >= 0) && 585 goog.asserts.assert((value >= 0) &&
906 (value < jspb.BinaryConstants.TWO_TO_32)); 586 (value < jspb.BinaryConstants.TWO_TO_32));
907 this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32); 587 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32);
908 this.rawWriteUint32(value); 588 this.encoder_.writeUint32(value);
909 }; 589 };
910 590
911 591
912 /** 592 /**
913 * Writes a fixed64 field to the buffer. Numbers outside the range [0,2^64) 593 * Writes a fixed64 field to the buffer. Numbers outside the range [0,2^64)
914 * will be truncated. 594 * will be truncated.
915 * @param {number} field The field number. 595 * @param {number} field The field number.
916 * @param {number?} value The value to write. 596 * @param {number?} value The value to write.
917 */ 597 */
918 jspb.BinaryWriter.prototype.writeFixed64 = function(field, value) { 598 jspb.BinaryWriter.prototype.writeFixed64 = function(field, value) {
919 if (value == null) return; 599 if (value == null) return;
920 goog.asserts.assert((value >= 0) && 600 goog.asserts.assert((value >= 0) &&
921 (value < jspb.BinaryConstants.TWO_TO_64)); 601 (value < jspb.BinaryConstants.TWO_TO_64));
922 this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64); 602 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
923 this.rawWriteUint64(value); 603 this.encoder_.writeUint64(value);
924 }; 604 };
925 605
926 606
927 /** 607 /**
928 * Writes a sfixed32 field to the buffer. Numbers outside the range 608 * Writes a sfixed32 field to the buffer. Numbers outside the range
929 * [-2^31,2^31) will be truncated. 609 * [-2^31,2^31) will be truncated.
930 * @param {number} field The field number. 610 * @param {number} field The field number.
931 * @param {number?} value The value to write. 611 * @param {number?} value The value to write.
932 */ 612 */
933 jspb.BinaryWriter.prototype.writeSfixed32 = function(field, value) { 613 jspb.BinaryWriter.prototype.writeSfixed32 = function(field, value) {
934 if (value == null) return; 614 if (value == null) return;
935 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) && 615 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
936 (value < jspb.BinaryConstants.TWO_TO_31)); 616 (value < jspb.BinaryConstants.TWO_TO_31));
937 this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32); 617 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32);
938 this.rawWriteInt32(value); 618 this.encoder_.writeInt32(value);
939 }; 619 };
940 620
941 621
942 /** 622 /**
943 * Writes a sfixed64 field to the buffer. Numbers outside the range 623 * Writes a sfixed64 field to the buffer. Numbers outside the range
944 * [-2^63,2^63) will be truncated. 624 * [-2^63,2^63) will be truncated.
945 * @param {number} field The field number. 625 * @param {number} field The field number.
946 * @param {number?} value The value to write. 626 * @param {number?} value The value to write.
947 */ 627 */
948 jspb.BinaryWriter.prototype.writeSfixed64 = function(field, value) { 628 jspb.BinaryWriter.prototype.writeSfixed64 = function(field, value) {
949 if (value == null) return; 629 if (value == null) return;
950 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) && 630 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
951 (value < jspb.BinaryConstants.TWO_TO_63)); 631 (value < jspb.BinaryConstants.TWO_TO_63));
952 this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64); 632 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
953 this.rawWriteInt64(value); 633 this.encoder_.writeInt64(value);
954 }; 634 };
955 635
956 636
957 /** 637 /**
958 * Writes a single-precision floating point field to the buffer. Numbers 638 * Writes a single-precision floating point field to the buffer. Numbers
959 * requiring more than 32 bits of precision will be truncated. 639 * requiring more than 32 bits of precision will be truncated.
960 * @param {number} field The field number. 640 * @param {number} field The field number.
961 * @param {number?} value The value to write. 641 * @param {number?} value The value to write.
962 */ 642 */
963 jspb.BinaryWriter.prototype.writeFloat = function(field, value) { 643 jspb.BinaryWriter.prototype.writeFloat = function(field, value) {
964 if (value == null) return; 644 if (value == null) return;
965 this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32); 645 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32);
966 this.rawWriteFloat(value); 646 this.encoder_.writeFloat(value);
967 }; 647 };
968 648
969 649
970 /** 650 /**
971 * Writes a double-precision floating point field to the buffer. As this is the 651 * Writes a double-precision floating point field to the buffer. As this is the
972 * native format used by JavaScript, no precision will be lost. 652 * native format used by JavaScript, no precision will be lost.
973 * @param {number} field The field number. 653 * @param {number} field The field number.
974 * @param {number?} value The value to write. 654 * @param {number?} value The value to write.
975 */ 655 */
976 jspb.BinaryWriter.prototype.writeDouble = function(field, value) { 656 jspb.BinaryWriter.prototype.writeDouble = function(field, value) {
977 if (value == null) return; 657 if (value == null) return;
978 this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64); 658 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
979 this.rawWriteDouble(value); 659 this.encoder_.writeDouble(value);
980 }; 660 };
981 661
982 662
983 /** 663 /**
984 * Writes a boolean field to the buffer. 664 * Writes a boolean field to the buffer.
985 * @param {number} field The field number. 665 * @param {number} field The field number.
986 * @param {boolean?} value The value to write. 666 * @param {boolean?} value The value to write.
987 */ 667 */
988 jspb.BinaryWriter.prototype.writeBool = function(field, value) { 668 jspb.BinaryWriter.prototype.writeBool = function(field, value) {
989 if (value == null) return; 669 if (value == null) return;
990 goog.asserts.assert(goog.isBoolean(value)); 670 goog.asserts.assert(goog.isBoolean(value));
991 this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); 671 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
992 this.temp_.push(~~value); 672 this.encoder_.writeBool(value);
993 }; 673 };
994 674
995 675
996 /** 676 /**
997 * Writes an enum field to the buffer. 677 * Writes an enum field to the buffer.
998 * @param {number} field The field number. 678 * @param {number} field The field number.
999 * @param {number?} value The value to write. 679 * @param {number?} value The value to write.
1000 */ 680 */
1001 jspb.BinaryWriter.prototype.writeEnum = function(field, value) { 681 jspb.BinaryWriter.prototype.writeEnum = function(field, value) {
1002 if (value == null) return; 682 if (value == null) return;
1003 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) && 683 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
1004 (value < jspb.BinaryConstants.TWO_TO_31)); 684 (value < jspb.BinaryConstants.TWO_TO_31));
1005 this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); 685 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
1006 this.rawWriteSignedVarint32(value); 686 this.encoder_.writeSignedVarint32(value);
1007 }; 687 };
1008 688
1009 689
1010 /** 690 /**
1011 * Writes a string field to the buffer. 691 * Writes a string field to the buffer.
1012 * @param {number} field The field number. 692 * @param {number} field The field number.
1013 * @param {string?} value The string to write. 693 * @param {string?} value The string to write.
1014 */ 694 */
1015 jspb.BinaryWriter.prototype.writeString = function(field, value) { 695 jspb.BinaryWriter.prototype.writeString = function(field, value) {
1016 if (value == null) return; 696 if (value == null) return;
1017 this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); 697 var bookmark = this.beginDelimited_(field);
1018 698 this.encoder_.writeString(value);
1019 // Conversion loop swiped from goog.crypt.stringToUtf8ByteArray. Note that 699 this.endDelimited_(bookmark);
1020 // 'bytes' will be at least as long as 'value', but could be longer if we
1021 // need to unpack unicode characters.
1022 var bytes = [];
1023 for (var i = 0; i < value.length; i++) {
1024 var c = value.charCodeAt(i);
1025 if (c < 128) {
1026 bytes.push(c);
1027 } else if (c < 2048) {
1028 bytes.push((c >> 6) | 192);
1029 bytes.push((c & 63) | 128);
1030 } else {
1031 bytes.push((c >> 12) | 224);
1032 bytes.push(((c >> 6) & 63) | 128);
1033 bytes.push((c & 63) | 128);
1034 }
1035 }
1036
1037 this.rawWriteUnsignedVarint32(bytes.length);
1038 this.appendArray_(bytes);
1039 }; 700 };
1040 701
1041 702
1042 /** 703 /**
1043 * Writes an arbitrary byte field to the buffer. Note - to match the behavior 704 * Writes an arbitrary byte field to the buffer. Note - to match the behavior
1044 * of the C++ implementation, empty byte arrays _are_ serialized. 705 * of the C++ implementation, empty byte arrays _are_ serialized.
1045 *
1046 * If 'value' is null, this method will try and copy the pre-serialized value
1047 * in 'opt_buffer' if present.
1048 *
1049 * @param {number} field The field number. 706 * @param {number} field The field number.
1050 * @param {jspb.ByteSource} value The array of bytes to write. 707 * @param {?jspb.ByteSource} value The array of bytes to write.
1051 * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
1052 * @param {?number=} opt_start The starting point in the above buffer.
1053 * @param {?number=} opt_end The ending point in the above buffer.
1054 * @param {boolean=} opt_stringIsRawBytes If `value` is a string, interpret it
1055 * as a series of raw bytes (codepoints 0--255 inclusive) rather than base64
1056 * data.
1057 */ 708 */
1058 jspb.BinaryWriter.prototype.writeBytes = 709 jspb.BinaryWriter.prototype.writeBytes = function(field, value) {
1059 function(field, value, opt_buffer, opt_start, opt_end, 710 if (value == null) return;
1060 opt_stringIsRawBytes) { 711 var bytes = jspb.utils.byteSourceToUint8Array(value);
1061 if (value != null) { 712 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
1062 this.rawWriteFieldHeader_(field, 713 this.encoder_.writeUnsignedVarint32(bytes.length);
1063 jspb.BinaryConstants.WireType.DELIMITED); 714 this.appendUint8Array_(bytes);
1064 this.rawWriteUnsignedVarint32(value.length);
1065 this.rawWriteBytes(
1066 jspb.utils.byteSourceToUint8Array(value, opt_stringIsRawBytes));
1067 } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
1068 this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
1069 }
1070 };
1071
1072
1073 /**
1074 * Writes an arbitrary byte field to the buffer, with `opt_stringIsRawBytes`
1075 * flag implicitly true.
1076 * @param {number} field
1077 * @param {jspb.ByteSource} value The array of bytes to write.
1078 */
1079 jspb.BinaryWriter.prototype.writeBytesRawString = function(field, value) {
1080 this.writeBytes(field, value, null, null, null, true);
1081 }; 715 };
1082 716
1083 717
1084 /** 718 /**
1085 * Writes a message to the buffer. 719 * Writes a message to the buffer.
1086 *
1087 * If 'value' is null, this method will try and copy the pre-serialized value
1088 * in 'opt_buffer' if present.
1089 *
1090 * @template MessageType 720 * @template MessageType
1091 * @param {number} field The field number. 721 * @param {number} field The field number.
1092 * @param {?MessageType} value The message to write. 722 * @param {?MessageType} value The message to write.
1093 * @param {!jspb.WriterFunction} writerCallback Will be invoked with the value 723 * @param {!jspb.WriterFunction} writerCallback Will be invoked with the value
1094 * to write and the writer to write it with. 724 * to write and the writer to write it with.
1095 * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
1096 * @param {?number=} opt_start The starting point in the above buffer.
1097 * @param {?number=} opt_end The ending point in the above buffer.
1098 */ 725 */
1099 jspb.BinaryWriter.prototype.writeMessage = 726 jspb.BinaryWriter.prototype.writeMessage = function(
1100 function(field, value, writerCallback, opt_buffer, opt_start, opt_end) { 727 field, value, writerCallback) {
1101 if (value !== null) { 728 if (value == null) return;
1102 var bookmark = this.beginDelimited_(field); 729 var bookmark = this.beginDelimited_(field);
1103 730 writerCallback(value, this);
1104 writerCallback(value, this); 731 this.endDelimited_(bookmark);
1105
1106 this.endDelimited_(bookmark);
1107 } else if (opt_buffer && (opt_start != null) && (opt_end != null)) {
1108 this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
1109 }
1110 }; 732 };
1111 733
1112 734
1113 /** 735 /**
1114 * Writes a group message to the buffer. 736 * Writes a group message to the buffer.
1115 * 737 *
1116 * @template MessageType 738 * @template MessageType
1117 * @param {number} field The field number. 739 * @param {number} field The field number.
1118 * @param {?MessageType} value The message to write, wrapped with START_GROUP / 740 * @param {?MessageType} value The message to write, wrapped with START_GROUP /
1119 * END_GROUP tags. Will be a no-op if 'value' is null. 741 * END_GROUP tags. Will be a no-op if 'value' is null.
1120 * @param {!jspb.WriterFunction} writerCallback Will be invoked with the value 742 * @param {!jspb.WriterFunction} writerCallback Will be invoked with the value
1121 * to write and the writer to write it with. 743 * to write and the writer to write it with.
1122 */ 744 */
1123 jspb.BinaryWriter.prototype.writeGroup = 745 jspb.BinaryWriter.prototype.writeGroup = function(
1124 function(field, value, writerCallback) { 746 field, value, writerCallback) {
1125 if (value) { 747 if (value == null) return;
1126 this.rawWriteFieldHeader_( 748 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.START_GROUP);
1127 field, jspb.BinaryConstants.WireType.START_GROUP); 749 writerCallback(value, this);
1128 writerCallback(value, this); 750 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.END_GROUP);
1129 this.rawWriteFieldHeader_(
1130 field, jspb.BinaryConstants.WireType.END_GROUP);
1131 }
1132 }; 751 };
1133 752
1134 753
1135 /** 754 /**
1136 * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to 755 * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
1137 * the buffer. 756 * the buffer.
1138 * @param {number} field The field number. 757 * @param {number} field The field number.
1139 * @param {string?} value The hash string. 758 * @param {string?} value The hash string.
1140 */ 759 */
1141 jspb.BinaryWriter.prototype.writeFixedHash64 = function(field, value) { 760 jspb.BinaryWriter.prototype.writeFixedHash64 = function(field, value) {
1142 if (value == null) return; 761 if (value == null) return;
1143 goog.asserts.assert(value.length == 8); 762 goog.asserts.assert(value.length == 8);
1144 this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64); 763 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
1145 this.rawWriteFixedHash64(value); 764 this.encoder_.writeFixedHash64(value);
1146 }; 765 };
1147 766
1148 767
1149 /** 768 /**
1150 * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to 769 * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
1151 * the buffer. 770 * the buffer.
1152 * @param {number} field The field number. 771 * @param {number} field The field number.
1153 * @param {string?} value The hash string. 772 * @param {string?} value The hash string.
1154 */ 773 */
1155 jspb.BinaryWriter.prototype.writeVarintHash64 = function(field, value) { 774 jspb.BinaryWriter.prototype.writeVarintHash64 = function(field, value) {
1156 if (value == null) return; 775 if (value == null) return;
1157 goog.asserts.assert(value.length == 8); 776 goog.asserts.assert(value.length == 8);
1158 this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); 777 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
1159 this.rawWriteVarintHash64(value); 778 this.encoder_.writeVarintHash64(value);
1160 }; 779 };
1161 780
1162 781
1163 /** 782 /**
1164 * Writes an array of numbers to the buffer as a repeated varint field. 783 * Writes an array of numbers to the buffer as a repeated varint field.
1165 * @param {number} field The field number. 784 * @param {number} field The field number.
1166 * @param {?Array.<number>} value The array of ints to write. 785 * @param {?Array.<number>} value The array of ints to write.
1167 * @private 786 * @private
1168 */ 787 */
1169 jspb.BinaryWriter.prototype.writeRepeatedUnsignedVarint32_ = 788 jspb.BinaryWriter.prototype.writeRepeatedUnsignedVarint32_ =
(...skipping 19 matching lines...) Expand all
1189 } 808 }
1190 }; 809 };
1191 810
1192 811
1193 /** 812 /**
1194 * Writes an array of numbers to the buffer as a repeated varint field. 813 * Writes an array of numbers to the buffer as a repeated varint field.
1195 * @param {number} field The field number. 814 * @param {number} field The field number.
1196 * @param {?Array.<number>} value The array of ints to write. 815 * @param {?Array.<number>} value The array of ints to write.
1197 * @private 816 * @private
1198 */ 817 */
1199 jspb.BinaryWriter.prototype.writeRepeatedVarint_ = function(field, value) { 818 jspb.BinaryWriter.prototype.writeRepeatedUnsignedVarint64_ =
819 function(field, value) {
1200 if (value == null) return; 820 if (value == null) return;
1201 for (var i = 0; i < value.length; i++) { 821 for (var i = 0; i < value.length; i++) {
1202 this.writeVarint_(field, value[i]); 822 this.writeUnsignedVarint64_(field, value[i]);
1203 } 823 }
1204 }; 824 };
1205 825
826
827 /**
828 * Writes an array of numbers to the buffer as a repeated varint field.
829 * @param {number} field The field number.
830 * @param {?Array.<number>} value The array of ints to write.
831 * @private
832 */
833 jspb.BinaryWriter.prototype.writeRepeatedSignedVarint64_ =
834 function(field, value) {
835 if (value == null) return;
836 for (var i = 0; i < value.length; i++) {
837 this.writeSignedVarint64_(field, value[i]);
838 }
839 };
840
1206 841
1207 /** 842 /**
1208 * Writes an array of numbers to the buffer as a repeated zigzag field. 843 * Writes an array of numbers to the buffer as a repeated zigzag field.
1209 * @param {number} field The field number. 844 * @param {number} field The field number.
1210 * @param {?Array.<number>} value The array of ints to write. 845 * @param {?Array.<number>} value The array of ints to write.
1211 * @private 846 * @private
1212 */ 847 */
1213 jspb.BinaryWriter.prototype.writeRepeatedZigzag32_ = function(field, value) { 848 jspb.BinaryWriter.prototype.writeRepeatedZigzag32_ = function(field, value) {
1214 if (value == null) return; 849 if (value == null) return;
1215 for (var i = 0; i < value.length; i++) { 850 for (var i = 0; i < value.length; i++) {
1216 this.writeZigzagVarint32_(field, value[i]); 851 this.writeZigzagVarint32_(field, value[i]);
1217 } 852 }
1218 }; 853 };
1219 854
1220 855
1221 /** 856 /**
1222 * Writes an array of numbers to the buffer as a repeated zigzag field. 857 * Writes an array of numbers to the buffer as a repeated zigzag field.
1223 * @param {number} field The field number. 858 * @param {number} field The field number.
1224 * @param {?Array.<number>} value The array of ints to write. 859 * @param {?Array.<number>} value The array of ints to write.
1225 * @private 860 * @private
1226 */ 861 */
1227 jspb.BinaryWriter.prototype.writeRepeatedZigzag_ = function(field, value) { 862 jspb.BinaryWriter.prototype.writeRepeatedZigzag_ = function(field, value) {
1228 if (value == null) return; 863 if (value == null) return;
1229 for (var i = 0; i < value.length; i++) { 864 for (var i = 0; i < value.length; i++) {
1230 this.writeZigzagVarint_(field, value[i]); 865 this.writeZigzagVarint64_(field, value[i]);
1231 } 866 }
1232 }; 867 };
1233 868
1234 869
1235 /** 870 /**
1236 * Writes an array of numbers to the buffer as a repeated 32-bit int field. 871 * Writes an array of numbers to the buffer as a repeated 32-bit int field.
1237 * @param {number} field The field number. 872 * @param {number} field The field number.
1238 * @param {?Array.<number>} value The array of ints to write. 873 * @param {?Array.<number>} value The array of ints to write.
1239 */ 874 */
1240 jspb.BinaryWriter.prototype.writeRepeatedInt32 = 875 jspb.BinaryWriter.prototype.writeRepeatedInt32 =
(...skipping 14 matching lines...) Expand all
1255 } 890 }
1256 }; 891 };
1257 892
1258 893
1259 /** 894 /**
1260 * Writes an array of numbers to the buffer as a repeated 64-bit int field. 895 * Writes an array of numbers to the buffer as a repeated 64-bit int field.
1261 * @param {number} field The field number. 896 * @param {number} field The field number.
1262 * @param {?Array.<number>} value The array of ints to write. 897 * @param {?Array.<number>} value The array of ints to write.
1263 */ 898 */
1264 jspb.BinaryWriter.prototype.writeRepeatedInt64 = 899 jspb.BinaryWriter.prototype.writeRepeatedInt64 =
1265 jspb.BinaryWriter.prototype.writeRepeatedVarint_; 900 jspb.BinaryWriter.prototype.writeRepeatedSignedVarint64_;
1266 901
1267 902
1268 /** 903 /**
1269 * Writes an array of numbers formatted as strings to the buffer as a repeated 904 * Writes an array of numbers formatted as strings to the buffer as a repeated
1270 * 64-bit int field. 905 * 64-bit int field.
1271 * @param {number} field The field number. 906 * @param {number} field The field number.
1272 * @param {?Array.<string>} value The array of ints to write. 907 * @param {?Array.<string>} value The array of ints to write.
1273 */ 908 */
1274 jspb.BinaryWriter.prototype.writeRepeatedInt64String = 909 jspb.BinaryWriter.prototype.writeRepeatedInt64String =
1275 function(field, value) { 910 function(field, value) {
(...skipping 29 matching lines...) Expand all
1305 }; 940 };
1306 941
1307 942
1308 /** 943 /**
1309 * Writes an array numbers to the buffer as a repeated unsigned 64-bit int 944 * Writes an array numbers to the buffer as a repeated unsigned 64-bit int
1310 * field. 945 * field.
1311 * @param {number} field The field number. 946 * @param {number} field The field number.
1312 * @param {?Array.<number>} value The array of ints to write. 947 * @param {?Array.<number>} value The array of ints to write.
1313 */ 948 */
1314 jspb.BinaryWriter.prototype.writeRepeatedUint64 = 949 jspb.BinaryWriter.prototype.writeRepeatedUint64 =
1315 jspb.BinaryWriter.prototype.writeRepeatedVarint_; 950 jspb.BinaryWriter.prototype.writeRepeatedUnsignedVarint64_;
1316 951
1317 952
1318 /** 953 /**
1319 * Writes an array of numbers formatted as strings to the buffer as a repeated 954 * Writes an array of numbers formatted as strings to the buffer as a repeated
1320 * unsigned 64-bit int field. 955 * unsigned 64-bit int field.
1321 * @param {number} field The field number. 956 * @param {number} field The field number.
1322 * @param {?Array.<string>} value The array of ints to write. 957 * @param {?Array.<string>} value The array of ints to write.
1323 */ 958 */
1324 jspb.BinaryWriter.prototype.writeRepeatedUint64String = 959 jspb.BinaryWriter.prototype.writeRepeatedUint64String =
1325 function(field, value) { 960 function(field, value) {
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
1462 jspb.BinaryWriter.prototype.writeRepeatedString = function(field, value) { 1097 jspb.BinaryWriter.prototype.writeRepeatedString = function(field, value) {
1463 if (value == null) return; 1098 if (value == null) return;
1464 for (var i = 0; i < value.length; i++) { 1099 for (var i = 0; i < value.length; i++) {
1465 this.writeString(field, value[i]); 1100 this.writeString(field, value[i]);
1466 } 1101 }
1467 }; 1102 };
1468 1103
1469 1104
1470 /** 1105 /**
1471 * Writes an array of arbitrary byte fields to the buffer. 1106 * Writes an array of arbitrary byte fields to the buffer.
1472 *
1473 * If 'value' is null, this method will try and copy the pre-serialized value
1474 * in 'opt_buffer' if present.
1475 *
1476 * @param {number} field The field number. 1107 * @param {number} field The field number.
1477 * @param {?Array.<!Uint8Array|string>} value 1108 * @param {?Array.<!jspb.ByteSource>} value The arrays of arrays of bytes to
1478 * The arrays of arrays of bytes to write. 1109 * write.
1479 * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
1480 * @param {?number=} opt_start The starting point in the above buffer.
1481 * @param {?number=} opt_end The ending point in the above buffer.
1482 * @param {boolean=} opt_stringIsRawBytes Any values that are strings are
1483 * interpreted as raw bytes rather than base64 data.
1484 */ 1110 */
1485 jspb.BinaryWriter.prototype.writeRepeatedBytes = 1111 jspb.BinaryWriter.prototype.writeRepeatedBytes = function(field, value) {
1486 function(field, value, opt_buffer, opt_start, opt_end, 1112 if (value == null) return;
1487 opt_stringIsRawBytes) { 1113 for (var i = 0; i < value.length; i++) {
1488 if (value != null) { 1114 this.writeBytes(field, value[i]);
1489 for (var i = 0; i < value.length; i++) {
1490 this.writeBytes(field, value[i], null, null, null, opt_stringIsRawBytes);
1491 }
1492 } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
1493 this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
1494 } 1115 }
1495 }; 1116 };
1496 1117
1497 1118
1498 /** 1119 /**
1499 * Writes an array of arbitrary byte fields to the buffer, with
1500 * `opt_stringIsRawBytes` implicitly true.
1501 * @param {number} field
1502 * @param {?Array.<string>} value
1503 */
1504 jspb.BinaryWriter.prototype.writeRepeatedBytesRawString =
1505 function(field, value) {
1506 this.writeRepeatedBytes(field, value, null, null, null, true);
1507 };
1508
1509
1510 /**
1511 * Writes an array of messages to the buffer. 1120 * Writes an array of messages to the buffer.
1512 *
1513 * If 'value' is null, this method will try and copy the pre-serialized value
1514 * in 'opt_buffer' if present.
1515 *
1516 * @template MessageType 1121 * @template MessageType
1517 * @param {number} field The field number. 1122 * @param {number} field The field number.
1518 * @param {?Array.<!MessageType>} value The array of messages to 1123 * @param {?Array.<MessageType>} value The array of messages to
1519 * write. 1124 * write.
1520 * @param {!jspb.WriterFunction} writerCallback Will be invoked with the value 1125 * @param {!jspb.WriterFunction} writerCallback Will be invoked with the value
1521 * to write and the writer to write it with. 1126 * to write and the writer to write it with.
1522 * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
1523 * @param {?number=} opt_start The starting point in the above buffer.
1524 * @param {?number=} opt_end The ending point in the above buffer.
1525 */ 1127 */
1526 jspb.BinaryWriter.prototype.writeRepeatedMessage = 1128 jspb.BinaryWriter.prototype.writeRepeatedMessage = function(
1527 function(field, value, writerCallback, opt_buffer, opt_start, opt_end) { 1129 field, value, writerCallback) {
1528 if (value) { 1130 if (value == null) return;
1529 for (var i = 0; i < value.length; i++) { 1131 for (var i = 0; i < value.length; i++) {
1530 var bookmark = this.beginDelimited_(field); 1132 var bookmark = this.beginDelimited_(field);
1531 1133 writerCallback(value[i], this);
1532 writerCallback(value[i], this); 1134 this.endDelimited_(bookmark);
1533
1534 this.endDelimited_(bookmark);
1535 }
1536 } else if (opt_buffer && (opt_start != null) && (opt_end != null)) {
1537 this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
1538 } 1135 }
1539 }; 1136 };
1540 1137
1541 1138
1542 /** 1139 /**
1543 * Writes an array of group messages to the buffer. 1140 * Writes an array of group messages to the buffer.
1544 *
1545 * @template MessageType 1141 * @template MessageType
1546 * @param {number} field The field number. 1142 * @param {number} field The field number.
1547 * @param {?Array.<!MessageType>} value The array of messages to 1143 * @param {?Array.<MessageType>} value The array of messages to
1548 * write. 1144 * write.
1549 * @param {!jspb.WriterFunction} writerCallback Will be invoked with the value 1145 * @param {!jspb.WriterFunction} writerCallback Will be invoked with the value
1550 * to write and the writer to write it with. 1146 * to write and the writer to write it with.
1551 */ 1147 */
1552 jspb.BinaryWriter.prototype.writeRepeatedGroup = 1148 jspb.BinaryWriter.prototype.writeRepeatedGroup = function(
1553 function(field, value, writerCallback) { 1149 field, value, writerCallback) {
1554 if (value) { 1150 if (value == null) return;
1555 for (var i = 0; i < value.length; i++) { 1151 for (var i = 0; i < value.length; i++) {
1556 this.rawWriteFieldHeader_( 1152 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.START_GROUP);
1557 field, jspb.BinaryConstants.WireType.START_GROUP); 1153 writerCallback(value[i], this);
1558 writerCallback(value[i], this); 1154 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.END_GROUP);
1559 this.rawWriteFieldHeader_(
1560 field, jspb.BinaryConstants.WireType.END_GROUP);
1561 }
1562 } 1155 }
1563 }; 1156 };
1564 1157
1565 1158
1566 /** 1159 /**
1567 * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to 1160 * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
1568 * the buffer. 1161 * the buffer.
1569 * @param {number} field The field number. 1162 * @param {number} field The field number.
1570 * @param {?Array.<string>} value The array of hashes to write. 1163 * @param {?Array.<string>} value The array of hashes to write.
1571 */ 1164 */
(...skipping 16 matching lines...) Expand all
1588 function(field, value) { 1181 function(field, value) {
1589 if (value == null) return; 1182 if (value == null) return;
1590 for (var i = 0; i < value.length; i++) { 1183 for (var i = 0; i < value.length; i++) {
1591 this.writeVarintHash64(field, value[i]); 1184 this.writeVarintHash64(field, value[i]);
1592 } 1185 }
1593 }; 1186 };
1594 1187
1595 1188
1596 /** 1189 /**
1597 * Writes an array of numbers to the buffer as a packed varint field. 1190 * Writes an array of numbers to the buffer as a packed varint field.
1598 *
1599 * If 'value' is null, this method will try and copy the pre-serialized value
1600 * in 'opt_buffer' if present.
1601 *
1602 * @param {number} field The field number. 1191 * @param {number} field The field number.
1603 * @param {?Array.<number>} value The array of ints to write. 1192 * @param {?Array.<number>} value The array of ints to write.
1604 * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
1605 * @param {?number=} opt_start The starting point in the above buffer.
1606 * @param {?number=} opt_end The ending point in the above buffer.
1607 * @private 1193 * @private
1608 */ 1194 */
1609 jspb.BinaryWriter.prototype.writePackedUnsignedVarint32_ = 1195 jspb.BinaryWriter.prototype.writePackedUnsignedVarint32_ = function(
1610 function(field, value, opt_buffer, opt_start, opt_end) { 1196 field, value) {
1611 if (value != null && value.length) { 1197 if (value == null || !value.length) return;
1612 var bookmark = this.beginDelimited_(field); 1198 var bookmark = this.beginDelimited_(field);
1613 for (var i = 0; i < value.length; i++) { 1199 for (var i = 0; i < value.length; i++) {
1614 this.rawWriteUnsignedVarint32(value[i]); 1200 this.encoder_.writeUnsignedVarint32(value[i]);
1615 }
1616 this.endDelimited_(bookmark);
1617 } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
1618 this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
1619 } 1201 }
1202 this.endDelimited_(bookmark);
1620 }; 1203 };
1621 1204
1622 1205
1623 /** 1206 /**
1624 * Writes an array of numbers to the buffer as a packed varint field. 1207 * Writes an array of numbers to the buffer as a packed varint field.
1625 *
1626 * If 'value' is null, this method will try and copy the pre-serialized value
1627 * in 'opt_buffer' if present.
1628 *
1629 * @param {number} field The field number. 1208 * @param {number} field The field number.
1630 * @param {?Array.<number>} value The array of ints to write. 1209 * @param {?Array.<number>} value The array of ints to write.
1631 * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
1632 * @param {?number=} opt_start The starting point in the above buffer.
1633 * @param {?number=} opt_end The ending point in the above buffer.
1634 * @private 1210 * @private
1635 */ 1211 */
1636 jspb.BinaryWriter.prototype.writePackedSignedVarint32_ = 1212 jspb.BinaryWriter.prototype.writePackedSignedVarint32_ = function(
1637 function(field, value, opt_buffer, opt_start, opt_end) { 1213 field, value) {
1638 if (value != null && value.length) { 1214 if (value == null || !value.length) return;
1639 var bookmark = this.beginDelimited_(field); 1215 var bookmark = this.beginDelimited_(field);
1640 for (var i = 0; i < value.length; i++) { 1216 for (var i = 0; i < value.length; i++) {
1641 this.rawWriteSignedVarint32(value[i]); 1217 this.encoder_.writeSignedVarint32(value[i]);
1642 }
1643 this.endDelimited_(bookmark);
1644 } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
1645 this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
1646 } 1218 }
1219 this.endDelimited_(bookmark);
1647 }; 1220 };
1648 1221
1649 1222
1650 /** 1223 /**
1651 * Writes an array of numbers to the buffer as a packed varint field. 1224 * Writes an array of numbers to the buffer as a packed varint field.
1652 *
1653 * If 'value' is null, this method will try and copy the pre-serialized value
1654 * in 'opt_buffer' if present.
1655 *
1656 * @param {number} field The field number. 1225 * @param {number} field The field number.
1657 * @param {?Array.<number>} value The array of ints to write. 1226 * @param {?Array.<number>} value The array of ints to write.
1658 * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
1659 * @param {?number=} opt_start The starting point in the above buffer.
1660 * @param {?number=} opt_end The ending point in the above buffer.
1661 * @private 1227 * @private
1662 */ 1228 */
1663 jspb.BinaryWriter.prototype.writePackedVarint_ = 1229 jspb.BinaryWriter.prototype.writePackedUnsignedVarint64_ = function(
1664 function(field, value, opt_buffer, opt_start, opt_end) { 1230 field, value) {
1665 if (value != null && value.length) { 1231 if (value == null || !value.length) return;
1666 var bookmark = this.beginDelimited_(field); 1232 var bookmark = this.beginDelimited_(field);
1667 for (var i = 0; i < value.length; i++) { 1233 for (var i = 0; i < value.length; i++) {
1668 this.rawWriteVarint(value[i]); 1234 this.encoder_.writeUnsignedVarint64(value[i]);
1669 }
1670 this.endDelimited_(bookmark);
1671 } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
1672 this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
1673 } 1235 }
1236 this.endDelimited_(bookmark);
1237 };
1238
1239
1240 /**
1241 * Writes an array of numbers to the buffer as a packed varint field.
1242 * @param {number} field The field number.
1243 * @param {?Array.<number>} value The array of ints to write.
1244 * @private
1245 */
1246 jspb.BinaryWriter.prototype.writePackedSignedVarint64_ = function(
1247 field, value) {
1248 if (value == null || !value.length) return;
1249 var bookmark = this.beginDelimited_(field);
1250 for (var i = 0; i < value.length; i++) {
1251 this.encoder_.writeSignedVarint64(value[i]);
1252 }
1253 this.endDelimited_(bookmark);
1674 }; 1254 };
1675 1255
1676 1256
1677 /** 1257 /**
1678 * Writes an array of numbers to the buffer as a packed zigzag field. 1258 * Writes an array of numbers to the buffer as a packed zigzag field.
1679 *
1680 * If 'value' is null, this method will try and copy the pre-serialized value
1681 * in 'opt_buffer' if present.
1682 *
1683 * @param {number} field The field number. 1259 * @param {number} field The field number.
1684 * @param {?Array.<number>} value The array of ints to write. 1260 * @param {?Array.<number>} value The array of ints to write.
1685 * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
1686 * @param {?number=} opt_start The starting point in the above buffer.
1687 * @param {?number=} opt_end The ending point in the above buffer.
1688 * @private 1261 * @private
1689 */ 1262 */
1690 jspb.BinaryWriter.prototype.writePackedZigzag_ = 1263 jspb.BinaryWriter.prototype.writePackedZigzag32_ = function(field, value) {
1691 function(field, value, opt_buffer, opt_start, opt_end) { 1264 if (value == null || !value.length) return;
1692 if (value != null && value.length) { 1265 var bookmark = this.beginDelimited_(field);
1693 var bookmark = this.beginDelimited_(field); 1266 for (var i = 0; i < value.length; i++) {
1694 for (var i = 0; i < value.length; i++) { 1267 this.encoder_.writeZigzagVarint32(value[i]);
1695 this.rawWriteZigzagVarint(value[i]);
1696 }
1697 this.endDelimited_(bookmark);
1698 } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
1699 this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
1700 } 1268 }
1269 this.endDelimited_(bookmark);
1270 };
1271
1272
1273 /**
1274 * Writes an array of numbers to the buffer as a packed zigzag field.
1275 * @param {number} field The field number.
1276 * @param {?Array.<number>} value The array of ints to write.
1277 * @private
1278 */
1279 jspb.BinaryWriter.prototype.writePackedZigzag64_ = function(field, value) {
1280 if (value == null || !value.length) return;
1281 var bookmark = this.beginDelimited_(field);
1282 for (var i = 0; i < value.length; i++) {
1283 this.encoder_.writeZigzagVarint64(value[i]);
1284 }
1285 this.endDelimited_(bookmark);
1701 }; 1286 };
1702 1287
1703 1288
1704 /** 1289 /**
1705 * Writes an array of numbers to the buffer as a packed 32-bit int field. 1290 * Writes an array of numbers to the buffer as a packed 32-bit int field.
1706 *
1707 * If 'value' is null, this method will try and copy the pre-serialized value
1708 * in 'opt_buffer' if present.
1709 *
1710 * @param {number} field The field number. 1291 * @param {number} field The field number.
1711 * @param {?Array.<number>} value The array of ints to write. 1292 * @param {?Array.<number>} value The array of ints to write.
1712 * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
1713 * @param {?number=} opt_start The starting point in the above buffer.
1714 * @param {?number=} opt_end The ending point in the above buffer.
1715 */ 1293 */
1716 jspb.BinaryWriter.prototype.writePackedInt32 = 1294 jspb.BinaryWriter.prototype.writePackedInt32 =
1717 jspb.BinaryWriter.prototype.writePackedSignedVarint32_; 1295 jspb.BinaryWriter.prototype.writePackedSignedVarint32_;
1718 1296
1719 1297
1720 /** 1298 /**
1721 * Writes an array of numbers represented as strings to the buffer as a packed 1299 * Writes an array of numbers represented as strings to the buffer as a packed
1722 * 32-bit int field. 1300 * 32-bit int field.
1723 * @param {number} field 1301 * @param {number} field
1724 * @param {?Array.<string>} value 1302 * @param {?Array.<string>} value
1725 */ 1303 */
1726 jspb.BinaryWriter.prototype.writePackedInt32String = function(field, value) { 1304 jspb.BinaryWriter.prototype.writePackedInt32String = function(field, value) {
1727 if (value == null || !value.length) return; 1305 if (value == null || !value.length) return;
1728 var bookmark = this.beginDelimited_(field); 1306 var bookmark = this.beginDelimited_(field);
1729 for (var i = 0; i < value.length; i++) { 1307 for (var i = 0; i < value.length; i++) {
1730 this.rawWriteSignedVarint32(parseInt(value[i], 10)); 1308 this.encoder_.writeSignedVarint32(parseInt(value[i], 10));
1731 } 1309 }
1732 this.endDelimited_(bookmark); 1310 this.endDelimited_(bookmark);
1733 }; 1311 };
1734 1312
1735 1313
1736 /** 1314 /**
1737 * Writes an array of numbers to the buffer as a packed 64-bit int field. 1315 * Writes an array of numbers to the buffer as a packed 64-bit int field.
1738 * @param {number} field The field number. 1316 * @param {number} field The field number.
1739 * @param {?Array.<number>} value The array of ints to write. 1317 * @param {?Array.<number>} value The array of ints to write.
1740 * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
1741 * @param {?number=} opt_start The starting point in the above buffer.
1742 * @param {?number=} opt_end The ending point in the above buffer.
1743 */ 1318 */
1744 jspb.BinaryWriter.prototype.writePackedInt64 = 1319 jspb.BinaryWriter.prototype.writePackedInt64 =
1745 jspb.BinaryWriter.prototype.writePackedVarint_; 1320 jspb.BinaryWriter.prototype.writePackedSignedVarint64_;
1746 1321
1747 1322
1748 /** 1323 /**
1749 * Writes an array of numbers represented as strings to the buffer as a packed 1324 * Writes an array of numbers represented as strings to the buffer as a packed
1750 * 64-bit int field. 1325 * 64-bit int field.
1751 * @param {number} field 1326 * @param {number} field
1752 * @param {?Array.<string>} value 1327 * @param {?Array.<string>} value
1753 */ 1328 */
1754 jspb.BinaryWriter.prototype.writePackedInt64String = 1329 jspb.BinaryWriter.prototype.writePackedInt64String =
1755 function(field, value) { 1330 function(field, value) {
1756 if (value == null || !value.length) return; 1331 if (value == null || !value.length) return;
1757 var bookmark = this.beginDelimited_(field); 1332 var bookmark = this.beginDelimited_(field);
1758 for (var i = 0; i < value.length; i++) { 1333 for (var i = 0; i < value.length; i++) {
1759 var num = jspb.arith.Int64.fromString(value[i]); 1334 var num = jspb.arith.Int64.fromString(value[i]);
1760 this.rawWriteVarintFromNum(num); 1335 this.encoder_.writeSplitVarint64(num.lo, num.hi);
1761 } 1336 }
1762 this.endDelimited_(bookmark); 1337 this.endDelimited_(bookmark);
1763 }; 1338 };
1764 1339
1765 1340
1766 /** 1341 /**
1767 * Writes an array numbers to the buffer as a packed unsigned 32-bit int field. 1342 * Writes an array numbers to the buffer as a packed unsigned 32-bit int field.
1768 *
1769 * If 'value' is null, this method will try and copy the pre-serialized value
1770 * in 'opt_buffer' if present.
1771 *
1772 * @param {number} field The field number. 1343 * @param {number} field The field number.
1773 * @param {?Array.<number>} value The array of ints to write. 1344 * @param {?Array.<number>} value The array of ints to write.
1774 * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
1775 * @param {?number=} opt_start The starting point in the above buffer.
1776 * @param {?number=} opt_end The ending point in the above buffer.
1777 */ 1345 */
1778 jspb.BinaryWriter.prototype.writePackedUint32 = 1346 jspb.BinaryWriter.prototype.writePackedUint32 =
1779 jspb.BinaryWriter.prototype.writePackedUnsignedVarint32_; 1347 jspb.BinaryWriter.prototype.writePackedUnsignedVarint32_;
1780 1348
1781 1349
1782 /** 1350 /**
1783 * Writes an array of numbers represented as strings to the buffer as a packed 1351 * Writes an array of numbers represented as strings to the buffer as a packed
1784 * unsigned 32-bit int field. 1352 * unsigned 32-bit int field.
1785 * @param {number} field 1353 * @param {number} field
1786 * @param {?Array.<string>} value 1354 * @param {?Array.<string>} value
1787 */ 1355 */
1788 jspb.BinaryWriter.prototype.writePackedUint32String = 1356 jspb.BinaryWriter.prototype.writePackedUint32String =
1789 function(field, value) { 1357 function(field, value) {
1790 if (value == null || !value.length) return; 1358 if (value == null || !value.length) return;
1791 var bookmark = this.beginDelimited_(field); 1359 var bookmark = this.beginDelimited_(field);
1792 for (var i = 0; i < value.length; i++) { 1360 for (var i = 0; i < value.length; i++) {
1793 this.rawWriteUnsignedVarint32(parseInt(value[i], 10)); 1361 this.encoder_.writeUnsignedVarint32(parseInt(value[i], 10));
1794 } 1362 }
1795 this.endDelimited_(bookmark); 1363 this.endDelimited_(bookmark);
1796 }; 1364 };
1797 1365
1798 1366
1799 /** 1367 /**
1800 * Writes an array numbers to the buffer as a packed unsigned 64-bit int field. 1368 * Writes an array numbers to the buffer as a packed unsigned 64-bit int field.
1801 *
1802 * If 'value' is null, this method will try and copy the pre-serialized value
1803 * in 'opt_buffer' if present.
1804 *
1805 * @param {number} field The field number. 1369 * @param {number} field The field number.
1806 * @param {?Array.<number>} value The array of ints to write. 1370 * @param {?Array.<number>} value The array of ints to write.
1807 * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
1808 * @param {?number=} opt_start The starting point in the above buffer.
1809 * @param {?number=} opt_end The ending point in the above buffer.
1810 */ 1371 */
1811 jspb.BinaryWriter.prototype.writePackedUint64 = 1372 jspb.BinaryWriter.prototype.writePackedUint64 =
1812 jspb.BinaryWriter.prototype.writePackedVarint_; 1373 jspb.BinaryWriter.prototype.writePackedUnsignedVarint64_;
1813 1374
1814 1375
1815 /** 1376 /**
1816 * Writes an array of numbers represented as strings to the buffer as a packed 1377 * Writes an array of numbers represented as strings to the buffer as a packed
1817 * unsigned 64-bit int field. 1378 * unsigned 64-bit int field.
1818 * @param {number} field 1379 * @param {number} field
1819 * @param {?Array.<string>} value 1380 * @param {?Array.<string>} value
1820 */ 1381 */
1821 jspb.BinaryWriter.prototype.writePackedUint64String = 1382 jspb.BinaryWriter.prototype.writePackedUint64String =
1822 function(field, value) { 1383 function(field, value) {
1823 if (value == null || !value.length) return; 1384 if (value == null || !value.length) return;
1824 var bookmark = this.beginDelimited_(field); 1385 var bookmark = this.beginDelimited_(field);
1825 for (var i = 0; i < value.length; i++) { 1386 for (var i = 0; i < value.length; i++) {
1826 var num = jspb.arith.UInt64.fromString(value[i]); 1387 var num = jspb.arith.UInt64.fromString(value[i]);
1827 this.rawWriteVarintFromNum(num); 1388 this.encoder_.writeSplitVarint64(num.lo, num.hi);
1828 } 1389 }
1829 this.endDelimited_(bookmark); 1390 this.endDelimited_(bookmark);
1830 }; 1391 };
1831 1392
1832 1393
1833 /** 1394 /**
1834 * Writes an array numbers to the buffer as a packed signed 32-bit int field. 1395 * Writes an array numbers to the buffer as a packed signed 32-bit int field.
1835 *
1836 * If 'value' is null, this method will try and copy the pre-serialized value
1837 * in 'opt_buffer' if present.
1838 *
1839 * @param {number} field The field number. 1396 * @param {number} field The field number.
1840 * @param {?Array.<number>} value The array of ints to write. 1397 * @param {?Array.<number>} value The array of ints to write.
1841 * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
1842 * @param {?number=} opt_start The starting point in the above buffer.
1843 * @param {?number=} opt_end The ending point in the above buffer.
1844 */ 1398 */
1845 jspb.BinaryWriter.prototype.writePackedSint32 = 1399 jspb.BinaryWriter.prototype.writePackedSint32 =
1846 jspb.BinaryWriter.prototype.writePackedZigzag_; 1400 jspb.BinaryWriter.prototype.writePackedZigzag32_;
1847 1401
1848 1402
1849 /** 1403 /**
1850 * Writes an array numbers to the buffer as a packed signed 64-bit int field. 1404 * Writes an array numbers to the buffer as a packed signed 64-bit int field.
1851 *
1852 * If 'value' is null, this method will try and copy the pre-serialized value
1853 * in 'opt_buffer' if present.
1854 *
1855 * @param {number} field The field number. 1405 * @param {number} field The field number.
1856 * @param {?Array.<number>} value The array of ints to write. 1406 * @param {?Array.<number>} value The array of ints to write.
1857 * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
1858 * @param {?number=} opt_start The starting point in the above buffer.
1859 * @param {?number=} opt_end The ending point in the above buffer.
1860 */ 1407 */
1861 jspb.BinaryWriter.prototype.writePackedSint64 = 1408 jspb.BinaryWriter.prototype.writePackedSint64 =
1862 jspb.BinaryWriter.prototype.writePackedZigzag_; 1409 jspb.BinaryWriter.prototype.writePackedZigzag64_;
1863 1410
1864 1411
1865 /** 1412 /**
1866 * Writes an array of numbers to the buffer as a packed fixed32 field. 1413 * Writes an array of numbers to the buffer as a packed fixed32 field.
1867 *
1868 * If 'value' is null, this method will try and copy the pre-serialized value
1869 * in 'opt_buffer' if present.
1870 *
1871 * @param {number} field The field number. 1414 * @param {number} field The field number.
1872 * @param {?Array.<number>} value The array of ints to write. 1415 * @param {?Array.<number>} value The array of ints to write.
1873 * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
1874 * @param {?number=} opt_start The starting point in the above buffer.
1875 * @param {?number=} opt_end The ending point in the above buffer.
1876 */ 1416 */
1877 jspb.BinaryWriter.prototype.writePackedFixed32 = 1417 jspb.BinaryWriter.prototype.writePackedFixed32 = function(field, value) {
1878 function(field, value, opt_buffer, opt_start, opt_end) { 1418 if (value == null || !value.length) return;
1879 if (value != null && value.length) { 1419 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
1880 this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); 1420 this.encoder_.writeUnsignedVarint32(value.length * 4);
1881 this.rawWriteUnsignedVarint32(value.length * 4); 1421 for (var i = 0; i < value.length; i++) {
1882 for (var i = 0; i < value.length; i++) { 1422 this.encoder_.writeUint32(value[i]);
1883 this.rawWriteUint32(value[i]);
1884 }
1885 } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
1886 this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
1887 } 1423 }
1888 }; 1424 };
1889 1425
1890 1426
1891 /** 1427 /**
1892 * Writes an array of numbers to the buffer as a packed fixed64 field. 1428 * Writes an array of numbers to the buffer as a packed fixed64 field.
1893 *
1894 * If 'value' is null, this method will try and copy the pre-serialized value
1895 * in 'opt_buffer' if present.
1896 *
1897 * @param {number} field The field number. 1429 * @param {number} field The field number.
1898 * @param {?Array.<number>} value The array of ints to write. 1430 * @param {?Array.<number>} value The array of ints to write.
1899 * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
1900 * @param {?number=} opt_start The starting point in the above buffer.
1901 * @param {?number=} opt_end The ending point in the above buffer.
1902 */ 1431 */
1903 jspb.BinaryWriter.prototype.writePackedFixed64 = 1432 jspb.BinaryWriter.prototype.writePackedFixed64 = function(field, value) {
1904 function(field, value, opt_buffer, opt_start, opt_end) { 1433 if (value == null || !value.length) return;
1905 if (value != null && value.length) { 1434 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
1906 this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); 1435 this.encoder_.writeUnsignedVarint32(value.length * 8);
1907 this.rawWriteUnsignedVarint32(value.length * 8); 1436 for (var i = 0; i < value.length; i++) {
1908 for (var i = 0; i < value.length; i++) { 1437 this.encoder_.writeUint64(value[i]);
1909 this.rawWriteUint64(value[i]);
1910 }
1911 } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
1912 this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
1913 } 1438 }
1914 }; 1439 };
1915 1440
1916 1441
1917 /** 1442 /**
1918 * Writes an array of numbers to the buffer as a packed sfixed32 field. 1443 * Writes an array of numbers to the buffer as a packed sfixed32 field.
1919 *
1920 * If 'value' is null, this method will try and copy the pre-serialized value
1921 * in 'opt_buffer' if present.
1922 *
1923 * @param {number} field The field number. 1444 * @param {number} field The field number.
1924 * @param {?Array.<number>} value The array of ints to write. 1445 * @param {?Array.<number>} value The array of ints to write.
1925 * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
1926 * @param {?number=} opt_start The starting point in the above buffer.
1927 * @param {?number=} opt_end The ending point in the above buffer.
1928 */ 1446 */
1929 jspb.BinaryWriter.prototype.writePackedSfixed32 = 1447 jspb.BinaryWriter.prototype.writePackedSfixed32 = function(field, value) {
1930 function(field, value, opt_buffer, opt_start, opt_end) { 1448 if (value == null || !value.length) return;
1931 if (value != null && value.length) { 1449 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
1932 this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); 1450 this.encoder_.writeUnsignedVarint32(value.length * 4);
1933 this.rawWriteUnsignedVarint32(value.length * 4); 1451 for (var i = 0; i < value.length; i++) {
1934 for (var i = 0; i < value.length; i++) { 1452 this.encoder_.writeInt32(value[i]);
1935 this.rawWriteInt32(value[i]);
1936 }
1937 } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
1938 this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
1939 } 1453 }
1940 }; 1454 };
1941 1455
1942 1456
1943 /** 1457 /**
1944 * Writes an array of numbers to the buffer as a packed sfixed64 field. 1458 * Writes an array of numbers to the buffer as a packed sfixed64 field.
1945 *
1946 * If 'value' is null, this method will try and copy the pre-serialized value
1947 * in 'opt_buffer' if present.
1948 *
1949 * @param {number} field The field number. 1459 * @param {number} field The field number.
1950 * @param {?Array.<number>} value The array of ints to write. 1460 * @param {?Array.<number>} value The array of ints to write.
1951 * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
1952 * @param {?number=} opt_start The starting point in the above buffer.
1953 * @param {?number=} opt_end The ending point in the above buffer.
1954 */ 1461 */
1955 jspb.BinaryWriter.prototype.writePackedSfixed64 = 1462 jspb.BinaryWriter.prototype.writePackedSfixed64 = function(field, value) {
1956 function(field, value, opt_buffer, opt_start, opt_end) { 1463 if (value == null || !value.length) return;
1957 if (value != null) { 1464 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
1958 this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); 1465 this.encoder_.writeUnsignedVarint32(value.length * 8);
1959 this.rawWriteUnsignedVarint32(value.length * 8); 1466 for (var i = 0; i < value.length; i++) {
1960 for (var i = 0; i < value.length; i++) { 1467 this.encoder_.writeInt64(value[i]);
1961 this.rawWriteInt64(value[i]);
1962 }
1963 } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
1964 this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
1965 } 1468 }
1966 }; 1469 };
1967 1470
1968 1471
1969 /** 1472 /**
1970 * Writes an array of numbers to the buffer as a packed float field. 1473 * Writes an array of numbers to the buffer as a packed float field.
1971 *
1972 * If 'value' is null, this method will try and copy the pre-serialized value
1973 * in 'opt_buffer' if present.
1974 *
1975 * @param {number} field The field number. 1474 * @param {number} field The field number.
1976 * @param {?Array.<number>} value The array of ints to write. 1475 * @param {?Array.<number>} value The array of ints to write.
1977 * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
1978 * @param {?number=} opt_start The starting point in the above buffer.
1979 * @param {?number=} opt_end The ending point in the above buffer.
1980 */ 1476 */
1981 jspb.BinaryWriter.prototype.writePackedFloat = 1477 jspb.BinaryWriter.prototype.writePackedFloat = function(field, value) {
1982 function(field, value, opt_buffer, opt_start, opt_end) { 1478 if (value == null || !value.length) return;
1983 if (value != null && value.length) { 1479 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
1984 this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); 1480 this.encoder_.writeUnsignedVarint32(value.length * 4);
1985 this.rawWriteUnsignedVarint32(value.length * 4); 1481 for (var i = 0; i < value.length; i++) {
1986 for (var i = 0; i < value.length; i++) { 1482 this.encoder_.writeFloat(value[i]);
1987 this.rawWriteFloat(value[i]);
1988 }
1989 } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
1990 this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
1991 } 1483 }
1992 }; 1484 };
1993 1485
1994 1486
1995 /** 1487 /**
1996 * Writes an array of numbers to the buffer as a packed double field. 1488 * Writes an array of numbers to the buffer as a packed double field.
1997 *
1998 * If 'value' is null, this method will try and copy the pre-serialized value
1999 * in 'opt_buffer' if present.
2000 *
2001 * @param {number} field The field number. 1489 * @param {number} field The field number.
2002 * @param {?Array.<number>} value The array of ints to write. 1490 * @param {?Array.<number>} value The array of ints to write.
2003 * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
2004 * @param {?number=} opt_start The starting point in the above buffer.
2005 * @param {?number=} opt_end The ending point in the above buffer.
2006 */ 1491 */
2007 jspb.BinaryWriter.prototype.writePackedDouble = 1492 jspb.BinaryWriter.prototype.writePackedDouble = function(field, value) {
2008 function(field, value, opt_buffer, opt_start, opt_end) { 1493 if (value == null || !value.length) return;
2009 if (value != null && value.length) { 1494 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
2010 this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); 1495 this.encoder_.writeUnsignedVarint32(value.length * 8);
2011 this.rawWriteUnsignedVarint32(value.length * 8); 1496 for (var i = 0; i < value.length; i++) {
2012 for (var i = 0; i < value.length; i++) { 1497 this.encoder_.writeDouble(value[i]);
2013 this.rawWriteDouble(value[i]);
2014 }
2015 } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
2016 this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
2017 } 1498 }
2018 }; 1499 };
2019 1500
2020 1501
2021 /** 1502 /**
2022 * Writes an array of booleans to the buffer as a packed bool field. 1503 * Writes an array of booleans to the buffer as a packed bool field.
2023 *
2024 * If 'value' is null, this method will try and copy the pre-serialized value
2025 * in 'opt_buffer' if present.
2026 *
2027 * @param {number} field The field number. 1504 * @param {number} field The field number.
2028 * @param {?Array.<boolean>} value The array of ints to write. 1505 * @param {?Array.<boolean>} value The array of ints to write.
2029 * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
2030 * @param {?number=} opt_start The starting point in the above buffer.
2031 * @param {?number=} opt_end The ending point in the above buffer.
2032 */ 1506 */
2033 jspb.BinaryWriter.prototype.writePackedBool = 1507 jspb.BinaryWriter.prototype.writePackedBool = function(field, value) {
2034 function(field, value, opt_buffer, opt_start, opt_end) { 1508 if (value == null || !value.length) return;
2035 if (value != null && value.length) { 1509 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
2036 this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); 1510 this.encoder_.writeUnsignedVarint32(value.length);
2037 this.rawWriteUnsignedVarint32(value.length); 1511 for (var i = 0; i < value.length; i++) {
2038 for (var i = 0; i < value.length; i++) { 1512 this.encoder_.writeBool(value[i]);
2039 this.rawWriteBool(value[i]);
2040 }
2041 } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
2042 this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
2043 } 1513 }
2044 }; 1514 };
2045 1515
2046 1516
2047 /** 1517 /**
2048 * Writes an array of enums to the buffer as a packed enum field. 1518 * Writes an array of enums to the buffer as a packed enum field.
2049 *
2050 * If 'value' is null, this method will try and copy the pre-serialized value
2051 * in 'opt_buffer' if present.
2052 *
2053 * @param {number} field The field number. 1519 * @param {number} field The field number.
2054 * @param {?Array.<number>} value The array of ints to write. 1520 * @param {?Array.<number>} value The array of ints to write.
2055 * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
2056 * @param {?number=} opt_start The starting point in the above buffer.
2057 * @param {?number=} opt_end The ending point in the above buffer.
2058 */ 1521 */
2059 jspb.BinaryWriter.prototype.writePackedEnum = 1522 jspb.BinaryWriter.prototype.writePackedEnum = function(field, value) {
2060 function(field, value, opt_buffer, opt_start, opt_end) { 1523 if (value == null || !value.length) return;
2061 if (value != null && value.length) { 1524 var bookmark = this.beginDelimited_(field);
2062 var bookmark = this.beginDelimited_(field); 1525 for (var i = 0; i < value.length; i++) {
2063 for (var i = 0; i < value.length; i++) { 1526 this.encoder_.writeEnum(value[i]);
2064 this.rawWriteEnum(value[i]);
2065 }
2066 this.endDelimited_(bookmark);
2067 } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
2068 this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
2069 } 1527 }
1528 this.endDelimited_(bookmark);
2070 }; 1529 };
2071 1530
2072 1531
2073 /** 1532 /**
2074 * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to 1533 * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
2075 * the buffer. 1534 * the buffer.
2076 *
2077 * If 'value' is null, this method will try and copy the pre-serialized value
2078 * in 'opt_buffer' if present.
2079 *
2080 * @param {number} field The field number. 1535 * @param {number} field The field number.
2081 * @param {?Array.<string>} value The array of hashes to write. 1536 * @param {?Array.<string>} value The array of hashes to write.
2082 * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
2083 * @param {?number=} opt_start The starting point in the above buffer.
2084 * @param {?number=} opt_end The ending point in the above buffer.
2085 */ 1537 */
2086 jspb.BinaryWriter.prototype.writePackedFixedHash64 = 1538 jspb.BinaryWriter.prototype.writePackedFixedHash64 = function(field, value) {
2087 function(field, value, opt_buffer, opt_start, opt_end) { 1539 if (value == null || !value.length) return;
2088 if (value != null && value.length) { 1540 this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
2089 this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); 1541 this.encoder_.writeUnsignedVarint32(value.length * 8);
2090 this.rawWriteUnsignedVarint32(value.length * 8); 1542 for (var i = 0; i < value.length; i++) {
2091 for (var i = 0; i < value.length; i++) { 1543 this.encoder_.writeFixedHash64(value[i]);
2092 this.rawWriteFixedHash64(value[i]);
2093 }
2094 } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
2095 this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
2096 } 1544 }
2097 }; 1545 };
2098 1546
2099 1547
2100 /** 1548 /**
2101 * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to 1549 * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
2102 * the buffer. 1550 * the buffer.
2103 *
2104 * If 'value' is null, this method will try and copy the pre-serialized value
2105 * in 'opt_buffer' if present.
2106 *
2107 * @param {number} field The field number. 1551 * @param {number} field The field number.
2108 * @param {?Array.<string>} value The array of hashes to write. 1552 * @param {?Array.<string>} value The array of hashes to write.
2109 * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
2110 * @param {?number=} opt_start The starting point in the above buffer.
2111 * @param {?number=} opt_end The ending point in the above buffer.
2112 */ 1553 */
2113 jspb.BinaryWriter.prototype.writePackedVarintHash64 = 1554 jspb.BinaryWriter.prototype.writePackedVarintHash64 = function(field, value) {
2114 function(field, value, opt_buffer, opt_start, opt_end) { 1555 if (value == null || !value.length) return;
2115 if (value != null && value.length) { 1556 var bookmark = this.beginDelimited_(field);
2116 var bookmark = this.beginDelimited_(field); 1557 for (var i = 0; i < value.length; i++) {
2117 for (var i = 0; i < value.length; i++) { 1558 this.encoder_.writeVarintHash64(value[i]);
2118 this.rawWriteVarintHash64(value[i]);
2119 }
2120 this.endDelimited_(bookmark);
2121 } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
2122 this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
2123 } 1559 }
1560 this.endDelimited_(bookmark);
2124 }; 1561 };
OLDNEW
« no previous file with comments | « third_party/protobuf/js/binary/utils_test.js ('k') | third_party/protobuf/js/commonjs/export.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698