Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame^] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2008 Google Inc. All rights reserved. |
| 3 | // https://developers.google.com/protocol-buffers/ |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
| 8 | // |
| 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | /** |
| 32 | * @fileoverview This file contains utilities for encoding Javascript objects |
| 33 | * into binary, wire-format protocol buffers (in the form of Uint8Arrays) that |
| 34 | * a server can consume directly. |
| 35 | * |
| 36 | * jspb's BinaryWriter class defines methods for efficiently encoding |
| 37 | * Javascript objects into binary, wire-format protocol buffers and supports |
| 38 | * all the fundamental field types used in protocol buffers. |
| 39 | * |
| 40 | * Major caveat 1 - Users of this library _must_ keep their Javascript proto |
| 41 | * parsing code in sync with the original .proto file - presumably you'll be |
| 42 | * using the typed jspb code generator, but if you bypass that you'll need |
| 43 | * to keep things in sync by hand. |
| 44 | * |
| 45 | * Major caveat 2 - Javascript is unable to accurately represent integers |
| 46 | * larger than 2^53 due to its use of a double-precision floating point format |
| 47 | * for all numbers. BinaryWriter does not make any special effort to preserve |
| 48 | * precision for values above this limit - if you need to pass 64-bit integers |
| 49 | * (hash codes, for example) between the client and server without precision |
| 50 | * loss, do _not_ use this library. |
| 51 | * |
| 52 | * Major caveat 3 - This class uses typed arrays and must not be used on older |
| 53 | * browsers that do not support them. |
| 54 | * |
| 55 | * @author aappleby@google.com (Austin Appleby) |
| 56 | */ |
| 57 | |
| 58 | goog.provide('jspb.BinaryWriter'); |
| 59 | |
| 60 | goog.require('goog.asserts'); |
| 61 | goog.require('goog.crypt.base64'); |
| 62 | goog.require('jspb.BinaryConstants'); |
| 63 | goog.require('jspb.arith.Int64'); |
| 64 | goog.require('jspb.arith.UInt64'); |
| 65 | goog.require('jspb.utils'); |
| 66 | |
| 67 | goog.forwardDeclare('jspb.Message'); |
| 68 | |
| 69 | |
| 70 | |
| 71 | /** |
| 72 | * BinaryWriter implements encoders for all the wire types specified in |
| 73 | * https://developers.google.com/protocol-buffers/docs/encoding. |
| 74 | * |
| 75 | * @constructor |
| 76 | * @struct |
| 77 | */ |
| 78 | jspb.BinaryWriter = function() { |
| 79 | /** |
| 80 | * Blocks of serialized data that will be concatenated once all messages have |
| 81 | * been written. |
| 82 | * @private {!Array<!Uint8Array|!Array<number>>} |
| 83 | */ |
| 84 | this.blocks_ = []; |
| 85 | |
| 86 | /** |
| 87 | * Total number of bytes in the blocks_ array. Does _not_ include the temp |
| 88 | * buffer. |
| 89 | * @private {number} |
| 90 | */ |
| 91 | this.totalLength_ = 0; |
| 92 | |
| 93 | /** |
| 94 | * Temporary buffer holding a message that we're still serializing. When we |
| 95 | * get to a stopping point (either the start of a new submessage, or when we |
| 96 | * need to append a raw Uint8Array), the temp buffer will be added to the |
| 97 | * block array above and a new temp buffer will be created. |
| 98 | * @private {!Array.<number>} |
| 99 | */ |
| 100 | this.temp_ = []; |
| 101 | |
| 102 | /** |
| 103 | * A stack of bookmarks containing the parent blocks for each message started |
| 104 | * via beginSubMessage(), needed as bookkeeping for endSubMessage(). |
| 105 | * TODO(aappleby): Deprecated, users should be calling writeMessage(). |
| 106 | * @private {!Array.<!jspb.BinaryWriter.Bookmark_>} |
| 107 | */ |
| 108 | this.bookmarks_ = []; |
| 109 | }; |
| 110 | |
| 111 | |
| 112 | /** |
| 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. |
| 135 | * |
| 136 | * @param {!Uint8Array} arr The byte array to append. |
| 137 | * @private |
| 138 | */ |
| 139 | jspb.BinaryWriter.prototype.appendUint8Array_ = function(arr) { |
| 140 | if (this.temp_.length) { |
| 141 | this.saveTempBuffer_(); |
| 142 | } |
| 143 | this.blocks_.push(arr); |
| 144 | this.totalLength_ += arr.length; |
| 145 | }; |
| 146 | |
| 147 | |
| 148 | /** |
| 149 | * Append an untyped array of bytes onto the buffer. |
| 150 | * |
| 151 | * @param {!Array.<number>} arr The byte array to append. |
| 152 | * @private |
| 153 | */ |
| 154 | jspb.BinaryWriter.prototype.appendArray_ = function(arr) { |
| 155 | if (this.temp_.length) { |
| 156 | this.saveTempBuffer_(); |
| 157 | } |
| 158 | this.temp_ = arr; |
| 159 | }; |
| 160 | |
| 161 | |
| 162 | /** |
| 163 | * Begins a length-delimited section by writing the field header to the current |
| 164 | * temp buffer and then saving it in the block array. Returns the saved block, |
| 165 | * which we will append the length to in endDelimited_ below. |
| 166 | * @param {number} field |
| 167 | * @return {!jspb.BinaryWriter.Bookmark_} |
| 168 | * @private |
| 169 | */ |
| 170 | jspb.BinaryWriter.prototype.beginDelimited_ = function(field) { |
| 171 | this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); |
| 172 | return {block: this.saveTempBuffer_(), length: this.totalLength_}; |
| 173 | }; |
| 174 | |
| 175 | |
| 176 | /** |
| 177 | * Ends a length-delimited block by encoding the _change_ in length of the |
| 178 | * buffer to the parent block and adds the number of bytes needed to encode |
| 179 | * that length to the total byte length. Note that 'parentLength' _must_ be the |
| 180 | * total length _after_ the field header was written in beginDelimited_ above. |
| 181 | * @param {!jspb.BinaryWriter.Bookmark_} bookmark |
| 182 | * @private |
| 183 | */ |
| 184 | jspb.BinaryWriter.prototype.endDelimited_ = function(bookmark) { |
| 185 | var messageLength = this.totalLength_ + this.temp_.length - bookmark.length; |
| 186 | goog.asserts.assert(messageLength >= 0); |
| 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 | }; |
| 198 | |
| 199 | |
| 200 | /** |
| 201 | * Resets the writer, throwing away any accumulated buffers. |
| 202 | */ |
| 203 | jspb.BinaryWriter.prototype.reset = function() { |
| 204 | this.blocks_ = []; |
| 205 | this.temp_ = []; |
| 206 | this.totalLength_ = 0; |
| 207 | this.bookmarks_ = []; |
| 208 | }; |
| 209 | |
| 210 | |
| 211 | /** |
| 212 | * Converts the encoded data into a Uint8Array. |
| 213 | * @return {!Uint8Array} |
| 214 | */ |
| 215 | jspb.BinaryWriter.prototype.getResultBuffer = function() { |
| 216 | goog.asserts.assert(this.bookmarks_.length == 0); |
| 217 | |
| 218 | var flat = new Uint8Array(this.totalLength_ + this.temp_.length); |
| 219 | |
| 220 | var blocks = this.blocks_; |
| 221 | var blockCount = blocks.length; |
| 222 | var offset = 0; |
| 223 | |
| 224 | for (var i = 0; i < blockCount; i++) { |
| 225 | var block = blocks[i]; |
| 226 | flat.set(block, offset); |
| 227 | offset += block.length; |
| 228 | } |
| 229 | |
| 230 | flat.set(this.temp_, offset); |
| 231 | offset += this.temp_.length; |
| 232 | |
| 233 | // Post condition: `flattened` must have had every byte written. |
| 234 | goog.asserts.assert(offset == flat.length); |
| 235 | |
| 236 | // Replace our block list with the flattened block, which lets GC reclaim |
| 237 | // the temp blocks sooner. |
| 238 | this.blocks_ = [flat]; |
| 239 | this.temp_ = []; |
| 240 | |
| 241 | return flat; |
| 242 | }; |
| 243 | |
| 244 | |
| 245 | /** |
| 246 | * Converts the encoded data into a bas64-encoded string. |
| 247 | * @return {string} |
| 248 | */ |
| 249 | jspb.BinaryWriter.prototype.getResultBase64String = function() { |
| 250 | return goog.crypt.base64.encodeByteArray(this.getResultBuffer()); |
| 251 | }; |
| 252 | |
| 253 | |
| 254 | /** |
| 255 | * Begins a new sub-message. The client must call endSubMessage() when they're |
| 256 | * done. |
| 257 | * TODO(aappleby): Deprecated. Move callers to writeMessage(). |
| 258 | * @param {number} field The field number of the sub-message. |
| 259 | */ |
| 260 | jspb.BinaryWriter.prototype.beginSubMessage = function(field) { |
| 261 | this.bookmarks_.push(this.beginDelimited_(field)); |
| 262 | }; |
| 263 | |
| 264 | |
| 265 | /** |
| 266 | * Finishes a sub-message and packs it into the parent messages' buffer. |
| 267 | * TODO(aappleby): Deprecated. Move callers to writeMessage(). |
| 268 | */ |
| 269 | jspb.BinaryWriter.prototype.endSubMessage = function() { |
| 270 | goog.asserts.assert(this.bookmarks_.length >= 0); |
| 271 | this.endDelimited_(this.bookmarks_.pop()); |
| 272 | }; |
| 273 | |
| 274 | |
| 275 | /** |
| 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 |
| 602 | * and stores it in the buffer as a varint. |
| 603 | * @param {number} field The field number. |
| 604 | * @param {number} wireType The wire-type of the field, as specified in the |
| 605 | * protocol buffer documentation. |
| 606 | * @private |
| 607 | */ |
| 608 | jspb.BinaryWriter.prototype.rawWriteFieldHeader_ = |
| 609 | function(field, wireType) { |
| 610 | goog.asserts.assert(field >= 1 && field == Math.floor(field)); |
| 611 | var x = field * 8 + wireType; |
| 612 | this.rawWriteUnsignedVarint32(x); |
| 613 | }; |
| 614 | |
| 615 | |
| 616 | /** |
| 617 | * Writes a field of any valid scalar type to the binary stream. |
| 618 | * @param {jspb.BinaryConstants.FieldType} fieldType |
| 619 | * @param {number} field |
| 620 | * @param {jspb.AnyFieldType} value |
| 621 | */ |
| 622 | jspb.BinaryWriter.prototype.writeAny = function(fieldType, field, value) { |
| 623 | var fieldTypes = jspb.BinaryConstants.FieldType; |
| 624 | switch (fieldType) { |
| 625 | case fieldTypes.DOUBLE: |
| 626 | this.writeDouble(field, /** @type {number} */(value)); |
| 627 | return; |
| 628 | case fieldTypes.FLOAT: |
| 629 | this.writeFloat(field, /** @type {number} */(value)); |
| 630 | return; |
| 631 | case fieldTypes.INT64: |
| 632 | this.writeInt64(field, /** @type {number} */(value)); |
| 633 | return; |
| 634 | case fieldTypes.UINT64: |
| 635 | this.writeUint64(field, /** @type {number} */(value)); |
| 636 | return; |
| 637 | case fieldTypes.INT32: |
| 638 | this.writeInt32(field, /** @type {number} */(value)); |
| 639 | return; |
| 640 | case fieldTypes.FIXED64: |
| 641 | this.writeFixed64(field, /** @type {number} */(value)); |
| 642 | return; |
| 643 | case fieldTypes.FIXED32: |
| 644 | this.writeFixed32(field, /** @type {number} */(value)); |
| 645 | return; |
| 646 | case fieldTypes.BOOL: |
| 647 | this.writeBool(field, /** @type {boolean} */(value)); |
| 648 | return; |
| 649 | case fieldTypes.STRING: |
| 650 | this.writeString(field, /** @type {string} */(value)); |
| 651 | return; |
| 652 | case fieldTypes.GROUP: |
| 653 | goog.asserts.fail('Group field type not supported in writeAny()'); |
| 654 | return; |
| 655 | case fieldTypes.MESSAGE: |
| 656 | goog.asserts.fail('Message field type not supported in writeAny()'); |
| 657 | return; |
| 658 | case fieldTypes.BYTES: |
| 659 | this.writeBytes(field, /** @type {?Uint8Array} */(value)); |
| 660 | return; |
| 661 | case fieldTypes.UINT32: |
| 662 | this.writeUint32(field, /** @type {number} */(value)); |
| 663 | return; |
| 664 | case fieldTypes.ENUM: |
| 665 | this.writeEnum(field, /** @type {number} */(value)); |
| 666 | return; |
| 667 | case fieldTypes.SFIXED32: |
| 668 | this.writeSfixed32(field, /** @type {number} */(value)); |
| 669 | return; |
| 670 | case fieldTypes.SFIXED64: |
| 671 | this.writeSfixed64(field, /** @type {number} */(value)); |
| 672 | return; |
| 673 | case fieldTypes.SINT32: |
| 674 | this.writeSint32(field, /** @type {number} */(value)); |
| 675 | return; |
| 676 | case fieldTypes.SINT64: |
| 677 | this.writeSint64(field, /** @type {number} */(value)); |
| 678 | return; |
| 679 | case fieldTypes.FHASH64: |
| 680 | this.writeFixedHash64(field, /** @type {string} */(value)); |
| 681 | return; |
| 682 | case fieldTypes.VHASH64: |
| 683 | this.writeVarintHash64(field, /** @type {string} */(value)); |
| 684 | return; |
| 685 | default: |
| 686 | goog.asserts.fail('Invalid field type in writeAny()'); |
| 687 | return; |
| 688 | } |
| 689 | }; |
| 690 | |
| 691 | |
| 692 | /** |
| 693 | * Writes a varint field to the buffer without range checking. |
| 694 | * @param {number} field The field number. |
| 695 | * @param {number?} value The value to write. |
| 696 | * @private |
| 697 | */ |
| 698 | jspb.BinaryWriter.prototype.writeUnsignedVarint32_ = function(field, value) { |
| 699 | if (value == null) return; |
| 700 | this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); |
| 701 | this.rawWriteSignedVarint32(value); |
| 702 | }; |
| 703 | |
| 704 | |
| 705 | /** |
| 706 | * Writes a varint field to the buffer without range checking. |
| 707 | * @param {number} field The field number. |
| 708 | * @param {number?} value The value to write. |
| 709 | * @private |
| 710 | */ |
| 711 | jspb.BinaryWriter.prototype.writeSignedVarint32_ = function(field, value) { |
| 712 | if (value == null) return; |
| 713 | this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); |
| 714 | this.rawWriteSignedVarint32(value); |
| 715 | }; |
| 716 | |
| 717 | |
| 718 | /** |
| 719 | * Writes a varint field to the buffer without range checking. |
| 720 | * @param {number} field The field number. |
| 721 | * @param {number?} value The value to write. |
| 722 | * @private |
| 723 | */ |
| 724 | jspb.BinaryWriter.prototype.writeVarint_ = function(field, value) { |
| 725 | if (value == null) return; |
| 726 | this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); |
| 727 | this.rawWriteVarint(value); |
| 728 | }; |
| 729 | |
| 730 | |
| 731 | /** |
| 732 | * Writes a zigzag varint field to the buffer without range checking. |
| 733 | * @param {number} field The field number. |
| 734 | * @param {number?} value The value to write. |
| 735 | * @private |
| 736 | */ |
| 737 | jspb.BinaryWriter.prototype.writeZigzagVarint32_ = function(field, value) { |
| 738 | if (value == null) return; |
| 739 | this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); |
| 740 | this.rawWriteZigzagVarint32(value); |
| 741 | }; |
| 742 | |
| 743 | |
| 744 | /** |
| 745 | * Writes a zigzag varint field to the buffer without range checking. |
| 746 | * @param {number} field The field number. |
| 747 | * @param {number?} value The value to write. |
| 748 | * @private |
| 749 | */ |
| 750 | jspb.BinaryWriter.prototype.writeZigzagVarint_ = function(field, value) { |
| 751 | if (value == null) return; |
| 752 | this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); |
| 753 | this.rawWriteZigzagVarint(value); |
| 754 | }; |
| 755 | |
| 756 | |
| 757 | /** |
| 758 | * Writes an int32 field to the buffer. Numbers outside the range [-2^31,2^31) |
| 759 | * will be truncated. |
| 760 | * @param {number} field The field number. |
| 761 | * @param {number?} value The value to write. |
| 762 | */ |
| 763 | jspb.BinaryWriter.prototype.writeInt32 = function(field, value) { |
| 764 | if (value == null) return; |
| 765 | goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) && |
| 766 | (value < jspb.BinaryConstants.TWO_TO_31)); |
| 767 | this.writeSignedVarint32_(field, value); |
| 768 | }; |
| 769 | |
| 770 | |
| 771 | /** |
| 772 | * Writes an int32 field represented as a string to the buffer. Numbers outside |
| 773 | * the range [-2^31,2^31) will be truncated. |
| 774 | * @param {number} field The field number. |
| 775 | * @param {string?} value The value to write. |
| 776 | */ |
| 777 | jspb.BinaryWriter.prototype.writeInt32String = function(field, value) { |
| 778 | if (value == null) return; |
| 779 | var intValue = /** {number} */ parseInt(value, 10); |
| 780 | goog.asserts.assert((intValue >= -jspb.BinaryConstants.TWO_TO_31) && |
| 781 | (intValue < jspb.BinaryConstants.TWO_TO_31)); |
| 782 | this.writeSignedVarint32_(field, intValue); |
| 783 | }; |
| 784 | |
| 785 | |
| 786 | /** |
| 787 | * Writes an int64 field to the buffer. Numbers outside the range [-2^63,2^63) |
| 788 | * will be truncated. |
| 789 | * @param {number} field The field number. |
| 790 | * @param {number?} value The value to write. |
| 791 | */ |
| 792 | jspb.BinaryWriter.prototype.writeInt64 = function(field, value) { |
| 793 | if (value == null) return; |
| 794 | goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) && |
| 795 | (value < jspb.BinaryConstants.TWO_TO_63)); |
| 796 | this.writeVarint_(field, value); |
| 797 | }; |
| 798 | |
| 799 | |
| 800 | /** |
| 801 | * Writes a int64 field (with value as a string) to the buffer. |
| 802 | * @param {number} field The field number. |
| 803 | * @param {string?} value The value to write. |
| 804 | */ |
| 805 | jspb.BinaryWriter.prototype.writeInt64String = function(field, value) { |
| 806 | if (value == null) return; |
| 807 | var num = jspb.arith.Int64.fromString(value); |
| 808 | this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); |
| 809 | this.rawWriteVarintFromNum(num); |
| 810 | }; |
| 811 | |
| 812 | |
| 813 | /** |
| 814 | * Writes a uint32 field to the buffer. Numbers outside the range [0,2^32) |
| 815 | * will be truncated. |
| 816 | * @param {number} field The field number. |
| 817 | * @param {number?} value The value to write. |
| 818 | */ |
| 819 | jspb.BinaryWriter.prototype.writeUint32 = function(field, value) { |
| 820 | if (value == null) return; |
| 821 | goog.asserts.assert((value >= 0) && |
| 822 | (value < jspb.BinaryConstants.TWO_TO_32)); |
| 823 | this.writeUnsignedVarint32_(field, value); |
| 824 | }; |
| 825 | |
| 826 | |
| 827 | /** |
| 828 | * Writes a uint32 field represented as a string to the buffer. Numbers outside |
| 829 | * the range [0,2^32) will be truncated. |
| 830 | * @param {number} field The field number. |
| 831 | * @param {string?} value The value to write. |
| 832 | */ |
| 833 | jspb.BinaryWriter.prototype.writeUint32String = function(field, value) { |
| 834 | if (value == null) return; |
| 835 | var intValue = /** {number} */ parseInt(value, 10); |
| 836 | goog.asserts.assert((intValue >= 0) && |
| 837 | (intValue < jspb.BinaryConstants.TWO_TO_32)); |
| 838 | this.writeUnsignedVarint32_(field, intValue); |
| 839 | }; |
| 840 | |
| 841 | |
| 842 | /** |
| 843 | * Writes a uint64 field to the buffer. Numbers outside the range [0,2^64) |
| 844 | * will be truncated. |
| 845 | * @param {number} field The field number. |
| 846 | * @param {number?} value The value to write. |
| 847 | */ |
| 848 | jspb.BinaryWriter.prototype.writeUint64 = function(field, value) { |
| 849 | if (value == null) return; |
| 850 | goog.asserts.assert((value >= 0) && |
| 851 | (value < jspb.BinaryConstants.TWO_TO_64)); |
| 852 | this.writeVarint_(field, value); |
| 853 | }; |
| 854 | |
| 855 | |
| 856 | /** |
| 857 | * Writes a uint64 field (with value as a string) to the buffer. |
| 858 | * @param {number} field The field number. |
| 859 | * @param {string?} value The value to write. |
| 860 | */ |
| 861 | jspb.BinaryWriter.prototype.writeUint64String = function(field, value) { |
| 862 | if (value == null) return; |
| 863 | var num = jspb.arith.UInt64.fromString(value); |
| 864 | this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); |
| 865 | this.rawWriteVarintFromNum(num); |
| 866 | }; |
| 867 | |
| 868 | |
| 869 | /** |
| 870 | * Writes a sint32 field to the buffer. Numbers outside the range [-2^31,2^31) |
| 871 | * will be truncated. |
| 872 | * @param {number} field The field number. |
| 873 | * @param {number?} value The value to write. |
| 874 | */ |
| 875 | jspb.BinaryWriter.prototype.writeSint32 = function(field, value) { |
| 876 | if (value == null) return; |
| 877 | goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) && |
| 878 | (value < jspb.BinaryConstants.TWO_TO_31)); |
| 879 | this.writeZigzagVarint32_(field, value); |
| 880 | }; |
| 881 | |
| 882 | |
| 883 | /** |
| 884 | * Writes a sint64 field to the buffer. Numbers outside the range [-2^63,2^63) |
| 885 | * will be truncated. |
| 886 | * @param {number} field The field number. |
| 887 | * @param {number?} value The value to write. |
| 888 | */ |
| 889 | jspb.BinaryWriter.prototype.writeSint64 = function(field, value) { |
| 890 | if (value == null) return; |
| 891 | goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) && |
| 892 | (value < jspb.BinaryConstants.TWO_TO_63)); |
| 893 | this.writeZigzagVarint_(field, value); |
| 894 | }; |
| 895 | |
| 896 | |
| 897 | /** |
| 898 | * Writes a fixed32 field to the buffer. Numbers outside the range [0,2^32) |
| 899 | * will be truncated. |
| 900 | * @param {number} field The field number. |
| 901 | * @param {number?} value The value to write. |
| 902 | */ |
| 903 | jspb.BinaryWriter.prototype.writeFixed32 = function(field, value) { |
| 904 | if (value == null) return; |
| 905 | goog.asserts.assert((value >= 0) && |
| 906 | (value < jspb.BinaryConstants.TWO_TO_32)); |
| 907 | this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32); |
| 908 | this.rawWriteUint32(value); |
| 909 | }; |
| 910 | |
| 911 | |
| 912 | /** |
| 913 | * Writes a fixed64 field to the buffer. Numbers outside the range [0,2^64) |
| 914 | * will be truncated. |
| 915 | * @param {number} field The field number. |
| 916 | * @param {number?} value The value to write. |
| 917 | */ |
| 918 | jspb.BinaryWriter.prototype.writeFixed64 = function(field, value) { |
| 919 | if (value == null) return; |
| 920 | goog.asserts.assert((value >= 0) && |
| 921 | (value < jspb.BinaryConstants.TWO_TO_64)); |
| 922 | this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64); |
| 923 | this.rawWriteUint64(value); |
| 924 | }; |
| 925 | |
| 926 | |
| 927 | /** |
| 928 | * Writes a sfixed32 field to the buffer. Numbers outside the range |
| 929 | * [-2^31,2^31) will be truncated. |
| 930 | * @param {number} field The field number. |
| 931 | * @param {number?} value The value to write. |
| 932 | */ |
| 933 | jspb.BinaryWriter.prototype.writeSfixed32 = function(field, value) { |
| 934 | if (value == null) return; |
| 935 | goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) && |
| 936 | (value < jspb.BinaryConstants.TWO_TO_31)); |
| 937 | this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32); |
| 938 | this.rawWriteInt32(value); |
| 939 | }; |
| 940 | |
| 941 | |
| 942 | /** |
| 943 | * Writes a sfixed64 field to the buffer. Numbers outside the range |
| 944 | * [-2^63,2^63) will be truncated. |
| 945 | * @param {number} field The field number. |
| 946 | * @param {number?} value The value to write. |
| 947 | */ |
| 948 | jspb.BinaryWriter.prototype.writeSfixed64 = function(field, value) { |
| 949 | if (value == null) return; |
| 950 | goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) && |
| 951 | (value < jspb.BinaryConstants.TWO_TO_63)); |
| 952 | this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64); |
| 953 | this.rawWriteInt64(value); |
| 954 | }; |
| 955 | |
| 956 | |
| 957 | /** |
| 958 | * Writes a single-precision floating point field to the buffer. Numbers |
| 959 | * requiring more than 32 bits of precision will be truncated. |
| 960 | * @param {number} field The field number. |
| 961 | * @param {number?} value The value to write. |
| 962 | */ |
| 963 | jspb.BinaryWriter.prototype.writeFloat = function(field, value) { |
| 964 | if (value == null) return; |
| 965 | this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32); |
| 966 | this.rawWriteFloat(value); |
| 967 | }; |
| 968 | |
| 969 | |
| 970 | /** |
| 971 | * 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. |
| 973 | * @param {number} field The field number. |
| 974 | * @param {number?} value The value to write. |
| 975 | */ |
| 976 | jspb.BinaryWriter.prototype.writeDouble = function(field, value) { |
| 977 | if (value == null) return; |
| 978 | this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64); |
| 979 | this.rawWriteDouble(value); |
| 980 | }; |
| 981 | |
| 982 | |
| 983 | /** |
| 984 | * Writes a boolean field to the buffer. |
| 985 | * @param {number} field The field number. |
| 986 | * @param {boolean?} value The value to write. |
| 987 | */ |
| 988 | jspb.BinaryWriter.prototype.writeBool = function(field, value) { |
| 989 | if (value == null) return; |
| 990 | goog.asserts.assert(goog.isBoolean(value)); |
| 991 | this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); |
| 992 | this.temp_.push(~~value); |
| 993 | }; |
| 994 | |
| 995 | |
| 996 | /** |
| 997 | * Writes an enum field to the buffer. |
| 998 | * @param {number} field The field number. |
| 999 | * @param {number?} value The value to write. |
| 1000 | */ |
| 1001 | jspb.BinaryWriter.prototype.writeEnum = function(field, value) { |
| 1002 | if (value == null) return; |
| 1003 | goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) && |
| 1004 | (value < jspb.BinaryConstants.TWO_TO_31)); |
| 1005 | this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); |
| 1006 | this.rawWriteSignedVarint32(value); |
| 1007 | }; |
| 1008 | |
| 1009 | |
| 1010 | /** |
| 1011 | * Writes a string field to the buffer. |
| 1012 | * @param {number} field The field number. |
| 1013 | * @param {string?} value The string to write. |
| 1014 | */ |
| 1015 | jspb.BinaryWriter.prototype.writeString = function(field, value) { |
| 1016 | if (value == null) return; |
| 1017 | this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); |
| 1018 | |
| 1019 | // Conversion loop swiped from goog.crypt.stringToUtf8ByteArray. Note that |
| 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 | }; |
| 1040 | |
| 1041 | |
| 1042 | /** |
| 1043 | * Writes an arbitrary byte field to the buffer. Note - to match the behavior |
| 1044 | * 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. |
| 1050 | * @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 | */ |
| 1058 | jspb.BinaryWriter.prototype.writeBytes = |
| 1059 | function(field, value, opt_buffer, opt_start, opt_end, |
| 1060 | opt_stringIsRawBytes) { |
| 1061 | if (value != null) { |
| 1062 | this.rawWriteFieldHeader_(field, |
| 1063 | jspb.BinaryConstants.WireType.DELIMITED); |
| 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 | }; |
| 1082 | |
| 1083 | |
| 1084 | /** |
| 1085 | * 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 |
| 1091 | * @param {number} field The field number. |
| 1092 | * @param {?MessageType} value The message to write. |
| 1093 | * @param {!jspb.WriterFunction} writerCallback Will be invoked with the value |
| 1094 | * 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 | */ |
| 1099 | jspb.BinaryWriter.prototype.writeMessage = |
| 1100 | function(field, value, writerCallback, opt_buffer, opt_start, opt_end) { |
| 1101 | if (value !== null) { |
| 1102 | var bookmark = this.beginDelimited_(field); |
| 1103 | |
| 1104 | writerCallback(value, this); |
| 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 | }; |
| 1111 | |
| 1112 | |
| 1113 | /** |
| 1114 | * Writes a group message to the buffer. |
| 1115 | * |
| 1116 | * @template MessageType |
| 1117 | * @param {number} field The field number. |
| 1118 | * @param {?MessageType} value The message to write, wrapped with START_GROUP / |
| 1119 | * END_GROUP tags. Will be a no-op if 'value' is null. |
| 1120 | * @param {!jspb.WriterFunction} writerCallback Will be invoked with the value |
| 1121 | * to write and the writer to write it with. |
| 1122 | */ |
| 1123 | jspb.BinaryWriter.prototype.writeGroup = |
| 1124 | function(field, value, writerCallback) { |
| 1125 | if (value) { |
| 1126 | this.rawWriteFieldHeader_( |
| 1127 | field, jspb.BinaryConstants.WireType.START_GROUP); |
| 1128 | writerCallback(value, this); |
| 1129 | this.rawWriteFieldHeader_( |
| 1130 | field, jspb.BinaryConstants.WireType.END_GROUP); |
| 1131 | } |
| 1132 | }; |
| 1133 | |
| 1134 | |
| 1135 | /** |
| 1136 | * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to |
| 1137 | * the buffer. |
| 1138 | * @param {number} field The field number. |
| 1139 | * @param {string?} value The hash string. |
| 1140 | */ |
| 1141 | jspb.BinaryWriter.prototype.writeFixedHash64 = function(field, value) { |
| 1142 | if (value == null) return; |
| 1143 | goog.asserts.assert(value.length == 8); |
| 1144 | this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64); |
| 1145 | this.rawWriteFixedHash64(value); |
| 1146 | }; |
| 1147 | |
| 1148 | |
| 1149 | /** |
| 1150 | * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to |
| 1151 | * the buffer. |
| 1152 | * @param {number} field The field number. |
| 1153 | * @param {string?} value The hash string. |
| 1154 | */ |
| 1155 | jspb.BinaryWriter.prototype.writeVarintHash64 = function(field, value) { |
| 1156 | if (value == null) return; |
| 1157 | goog.asserts.assert(value.length == 8); |
| 1158 | this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); |
| 1159 | this.rawWriteVarintHash64(value); |
| 1160 | }; |
| 1161 | |
| 1162 | |
| 1163 | /** |
| 1164 | * Writes an array of numbers to the buffer as a repeated varint field. |
| 1165 | * @param {number} field The field number. |
| 1166 | * @param {?Array.<number>} value The array of ints to write. |
| 1167 | * @private |
| 1168 | */ |
| 1169 | jspb.BinaryWriter.prototype.writeRepeatedUnsignedVarint32_ = |
| 1170 | function(field, value) { |
| 1171 | if (value == null) return; |
| 1172 | for (var i = 0; i < value.length; i++) { |
| 1173 | this.writeUnsignedVarint32_(field, value[i]); |
| 1174 | } |
| 1175 | }; |
| 1176 | |
| 1177 | |
| 1178 | /** |
| 1179 | * Writes an array of numbers to the buffer as a repeated varint field. |
| 1180 | * @param {number} field The field number. |
| 1181 | * @param {?Array.<number>} value The array of ints to write. |
| 1182 | * @private |
| 1183 | */ |
| 1184 | jspb.BinaryWriter.prototype.writeRepeatedSignedVarint32_ = |
| 1185 | function(field, value) { |
| 1186 | if (value == null) return; |
| 1187 | for (var i = 0; i < value.length; i++) { |
| 1188 | this.writeSignedVarint32_(field, value[i]); |
| 1189 | } |
| 1190 | }; |
| 1191 | |
| 1192 | |
| 1193 | /** |
| 1194 | * Writes an array of numbers to the buffer as a repeated varint field. |
| 1195 | * @param {number} field The field number. |
| 1196 | * @param {?Array.<number>} value The array of ints to write. |
| 1197 | * @private |
| 1198 | */ |
| 1199 | jspb.BinaryWriter.prototype.writeRepeatedVarint_ = function(field, value) { |
| 1200 | if (value == null) return; |
| 1201 | for (var i = 0; i < value.length; i++) { |
| 1202 | this.writeVarint_(field, value[i]); |
| 1203 | } |
| 1204 | }; |
| 1205 | |
| 1206 | |
| 1207 | /** |
| 1208 | * Writes an array of numbers to the buffer as a repeated zigzag field. |
| 1209 | * @param {number} field The field number. |
| 1210 | * @param {?Array.<number>} value The array of ints to write. |
| 1211 | * @private |
| 1212 | */ |
| 1213 | jspb.BinaryWriter.prototype.writeRepeatedZigzag32_ = function(field, value) { |
| 1214 | if (value == null) return; |
| 1215 | for (var i = 0; i < value.length; i++) { |
| 1216 | this.writeZigzagVarint32_(field, value[i]); |
| 1217 | } |
| 1218 | }; |
| 1219 | |
| 1220 | |
| 1221 | /** |
| 1222 | * Writes an array of numbers to the buffer as a repeated zigzag field. |
| 1223 | * @param {number} field The field number. |
| 1224 | * @param {?Array.<number>} value The array of ints to write. |
| 1225 | * @private |
| 1226 | */ |
| 1227 | jspb.BinaryWriter.prototype.writeRepeatedZigzag_ = function(field, value) { |
| 1228 | if (value == null) return; |
| 1229 | for (var i = 0; i < value.length; i++) { |
| 1230 | this.writeZigzagVarint_(field, value[i]); |
| 1231 | } |
| 1232 | }; |
| 1233 | |
| 1234 | |
| 1235 | /** |
| 1236 | * Writes an array of numbers to the buffer as a repeated 32-bit int field. |
| 1237 | * @param {number} field The field number. |
| 1238 | * @param {?Array.<number>} value The array of ints to write. |
| 1239 | */ |
| 1240 | jspb.BinaryWriter.prototype.writeRepeatedInt32 = |
| 1241 | jspb.BinaryWriter.prototype.writeRepeatedSignedVarint32_; |
| 1242 | |
| 1243 | |
| 1244 | /** |
| 1245 | * Writes an array of numbers formatted as strings to the buffer as a repeated |
| 1246 | * 32-bit int field. |
| 1247 | * @param {number} field The field number. |
| 1248 | * @param {?Array.<string>} value The array of ints to write. |
| 1249 | */ |
| 1250 | jspb.BinaryWriter.prototype.writeRepeatedInt32String = |
| 1251 | function(field, value) { |
| 1252 | if (value == null) return; |
| 1253 | for (var i = 0; i < value.length; i++) { |
| 1254 | this.writeInt32String(field, value[i]); |
| 1255 | } |
| 1256 | }; |
| 1257 | |
| 1258 | |
| 1259 | /** |
| 1260 | * Writes an array of numbers to the buffer as a repeated 64-bit int field. |
| 1261 | * @param {number} field The field number. |
| 1262 | * @param {?Array.<number>} value The array of ints to write. |
| 1263 | */ |
| 1264 | jspb.BinaryWriter.prototype.writeRepeatedInt64 = |
| 1265 | jspb.BinaryWriter.prototype.writeRepeatedVarint_; |
| 1266 | |
| 1267 | |
| 1268 | /** |
| 1269 | * Writes an array of numbers formatted as strings to the buffer as a repeated |
| 1270 | * 64-bit int field. |
| 1271 | * @param {number} field The field number. |
| 1272 | * @param {?Array.<string>} value The array of ints to write. |
| 1273 | */ |
| 1274 | jspb.BinaryWriter.prototype.writeRepeatedInt64String = |
| 1275 | function(field, value) { |
| 1276 | if (value == null) return; |
| 1277 | for (var i = 0; i < value.length; i++) { |
| 1278 | this.writeInt64String(field, value[i]); |
| 1279 | } |
| 1280 | }; |
| 1281 | |
| 1282 | |
| 1283 | /** |
| 1284 | * Writes an array numbers to the buffer as a repeated unsigned 32-bit int |
| 1285 | * field. |
| 1286 | * @param {number} field The field number. |
| 1287 | * @param {?Array.<number>} value The array of ints to write. |
| 1288 | */ |
| 1289 | jspb.BinaryWriter.prototype.writeRepeatedUint32 = |
| 1290 | jspb.BinaryWriter.prototype.writeRepeatedUnsignedVarint32_; |
| 1291 | |
| 1292 | |
| 1293 | /** |
| 1294 | * Writes an array of numbers formatted as strings to the buffer as a repeated |
| 1295 | * unsigned 32-bit int field. |
| 1296 | * @param {number} field The field number. |
| 1297 | * @param {?Array.<string>} value The array of ints to write. |
| 1298 | */ |
| 1299 | jspb.BinaryWriter.prototype.writeRepeatedUint32String = |
| 1300 | function(field, value) { |
| 1301 | if (value == null) return; |
| 1302 | for (var i = 0; i < value.length; i++) { |
| 1303 | this.writeUint32String(field, value[i]); |
| 1304 | } |
| 1305 | }; |
| 1306 | |
| 1307 | |
| 1308 | /** |
| 1309 | * Writes an array numbers to the buffer as a repeated unsigned 64-bit int |
| 1310 | * field. |
| 1311 | * @param {number} field The field number. |
| 1312 | * @param {?Array.<number>} value The array of ints to write. |
| 1313 | */ |
| 1314 | jspb.BinaryWriter.prototype.writeRepeatedUint64 = |
| 1315 | jspb.BinaryWriter.prototype.writeRepeatedVarint_; |
| 1316 | |
| 1317 | |
| 1318 | /** |
| 1319 | * Writes an array of numbers formatted as strings to the buffer as a repeated |
| 1320 | * unsigned 64-bit int field. |
| 1321 | * @param {number} field The field number. |
| 1322 | * @param {?Array.<string>} value The array of ints to write. |
| 1323 | */ |
| 1324 | jspb.BinaryWriter.prototype.writeRepeatedUint64String = |
| 1325 | function(field, value) { |
| 1326 | if (value == null) return; |
| 1327 | for (var i = 0; i < value.length; i++) { |
| 1328 | this.writeUint64String(field, value[i]); |
| 1329 | } |
| 1330 | }; |
| 1331 | |
| 1332 | |
| 1333 | /** |
| 1334 | * Writes an array numbers to the buffer as a repeated signed 32-bit int field. |
| 1335 | * @param {number} field The field number. |
| 1336 | * @param {?Array.<number>} value The array of ints to write. |
| 1337 | */ |
| 1338 | jspb.BinaryWriter.prototype.writeRepeatedSint32 = |
| 1339 | jspb.BinaryWriter.prototype.writeRepeatedZigzag32_; |
| 1340 | |
| 1341 | |
| 1342 | /** |
| 1343 | * Writes an array numbers to the buffer as a repeated signed 64-bit int field. |
| 1344 | * @param {number} field The field number. |
| 1345 | * @param {?Array.<number>} value The array of ints to write. |
| 1346 | */ |
| 1347 | jspb.BinaryWriter.prototype.writeRepeatedSint64 = |
| 1348 | jspb.BinaryWriter.prototype.writeRepeatedZigzag_; |
| 1349 | |
| 1350 | |
| 1351 | /** |
| 1352 | * Writes an array of numbers to the buffer as a repeated fixed32 field. This |
| 1353 | * works for both signed and unsigned fixed32s. |
| 1354 | * @param {number} field The field number. |
| 1355 | * @param {?Array.<number>} value The array of ints to write. |
| 1356 | */ |
| 1357 | jspb.BinaryWriter.prototype.writeRepeatedFixed32 = function(field, value) { |
| 1358 | if (value == null) return; |
| 1359 | for (var i = 0; i < value.length; i++) { |
| 1360 | this.writeFixed32(field, value[i]); |
| 1361 | } |
| 1362 | }; |
| 1363 | |
| 1364 | |
| 1365 | /** |
| 1366 | * Writes an array of numbers to the buffer as a repeated fixed64 field. This |
| 1367 | * works for both signed and unsigned fixed64s. |
| 1368 | * @param {number} field The field number. |
| 1369 | * @param {?Array.<number>} value The array of ints to write. |
| 1370 | */ |
| 1371 | jspb.BinaryWriter.prototype.writeRepeatedFixed64 = function(field, value) { |
| 1372 | if (value == null) return; |
| 1373 | for (var i = 0; i < value.length; i++) { |
| 1374 | this.writeFixed64(field, value[i]); |
| 1375 | } |
| 1376 | }; |
| 1377 | |
| 1378 | |
| 1379 | /** |
| 1380 | * Writes an array of numbers to the buffer as a repeated sfixed32 field. |
| 1381 | * @param {number} field The field number. |
| 1382 | * @param {?Array.<number>} value The array of ints to write. |
| 1383 | */ |
| 1384 | jspb.BinaryWriter.prototype.writeRepeatedSfixed32 = function(field, value) { |
| 1385 | if (value == null) return; |
| 1386 | for (var i = 0; i < value.length; i++) { |
| 1387 | this.writeSfixed32(field, value[i]); |
| 1388 | } |
| 1389 | }; |
| 1390 | |
| 1391 | |
| 1392 | /** |
| 1393 | * Writes an array of numbers to the buffer as a repeated sfixed64 field. |
| 1394 | * @param {number} field The field number. |
| 1395 | * @param {?Array.<number>} value The array of ints to write. |
| 1396 | */ |
| 1397 | jspb.BinaryWriter.prototype.writeRepeatedSfixed64 = function(field, value) { |
| 1398 | if (value == null) return; |
| 1399 | for (var i = 0; i < value.length; i++) { |
| 1400 | this.writeSfixed64(field, value[i]); |
| 1401 | } |
| 1402 | }; |
| 1403 | |
| 1404 | |
| 1405 | /** |
| 1406 | * Writes an array of numbers to the buffer as a repeated float field. |
| 1407 | * @param {number} field The field number. |
| 1408 | * @param {?Array.<number>} value The array of ints to write. |
| 1409 | */ |
| 1410 | jspb.BinaryWriter.prototype.writeRepeatedFloat = function(field, value) { |
| 1411 | if (value == null) return; |
| 1412 | for (var i = 0; i < value.length; i++) { |
| 1413 | this.writeFloat(field, value[i]); |
| 1414 | } |
| 1415 | }; |
| 1416 | |
| 1417 | |
| 1418 | /** |
| 1419 | * Writes an array of numbers to the buffer as a repeated double field. |
| 1420 | * @param {number} field The field number. |
| 1421 | * @param {?Array.<number>} value The array of ints to write. |
| 1422 | */ |
| 1423 | jspb.BinaryWriter.prototype.writeRepeatedDouble = function(field, value) { |
| 1424 | if (value == null) return; |
| 1425 | for (var i = 0; i < value.length; i++) { |
| 1426 | this.writeDouble(field, value[i]); |
| 1427 | } |
| 1428 | }; |
| 1429 | |
| 1430 | |
| 1431 | /** |
| 1432 | * Writes an array of booleans to the buffer as a repeated bool field. |
| 1433 | * @param {number} field The field number. |
| 1434 | * @param {?Array.<boolean>} value The array of ints to write. |
| 1435 | */ |
| 1436 | jspb.BinaryWriter.prototype.writeRepeatedBool = function(field, value) { |
| 1437 | if (value == null) return; |
| 1438 | for (var i = 0; i < value.length; i++) { |
| 1439 | this.writeBool(field, value[i]); |
| 1440 | } |
| 1441 | }; |
| 1442 | |
| 1443 | |
| 1444 | /** |
| 1445 | * Writes an array of enums to the buffer as a repeated enum field. |
| 1446 | * @param {number} field The field number. |
| 1447 | * @param {?Array.<number>} value The array of ints to write. |
| 1448 | */ |
| 1449 | jspb.BinaryWriter.prototype.writeRepeatedEnum = function(field, value) { |
| 1450 | if (value == null) return; |
| 1451 | for (var i = 0; i < value.length; i++) { |
| 1452 | this.writeEnum(field, value[i]); |
| 1453 | } |
| 1454 | }; |
| 1455 | |
| 1456 | |
| 1457 | /** |
| 1458 | * Writes an array of strings to the buffer as a repeated string field. |
| 1459 | * @param {number} field The field number. |
| 1460 | * @param {?Array.<string>} value The array of strings to write. |
| 1461 | */ |
| 1462 | jspb.BinaryWriter.prototype.writeRepeatedString = function(field, value) { |
| 1463 | if (value == null) return; |
| 1464 | for (var i = 0; i < value.length; i++) { |
| 1465 | this.writeString(field, value[i]); |
| 1466 | } |
| 1467 | }; |
| 1468 | |
| 1469 | |
| 1470 | /** |
| 1471 | * 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. |
| 1477 | * @param {?Array.<!Uint8Array|string>} value |
| 1478 | * The arrays of arrays of bytes to 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 | */ |
| 1485 | jspb.BinaryWriter.prototype.writeRepeatedBytes = |
| 1486 | function(field, value, opt_buffer, opt_start, opt_end, |
| 1487 | opt_stringIsRawBytes) { |
| 1488 | if (value != null) { |
| 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 | } |
| 1495 | }; |
| 1496 | |
| 1497 | |
| 1498 | /** |
| 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. |
| 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 |
| 1517 | * @param {number} field The field number. |
| 1518 | * @param {?Array.<!MessageType>} value The array of messages to |
| 1519 | * write. |
| 1520 | * @param {!jspb.WriterFunction} writerCallback Will be invoked with the value |
| 1521 | * 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 | */ |
| 1526 | jspb.BinaryWriter.prototype.writeRepeatedMessage = |
| 1527 | function(field, value, writerCallback, opt_buffer, opt_start, opt_end) { |
| 1528 | if (value) { |
| 1529 | for (var i = 0; i < value.length; i++) { |
| 1530 | var bookmark = this.beginDelimited_(field); |
| 1531 | |
| 1532 | writerCallback(value[i], this); |
| 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 | } |
| 1539 | }; |
| 1540 | |
| 1541 | |
| 1542 | /** |
| 1543 | * Writes an array of group messages to the buffer. |
| 1544 | * |
| 1545 | * @template MessageType |
| 1546 | * @param {number} field The field number. |
| 1547 | * @param {?Array.<!MessageType>} value The array of messages to |
| 1548 | * write. |
| 1549 | * @param {!jspb.WriterFunction} writerCallback Will be invoked with the value |
| 1550 | * to write and the writer to write it with. |
| 1551 | */ |
| 1552 | jspb.BinaryWriter.prototype.writeRepeatedGroup = |
| 1553 | function(field, value, writerCallback) { |
| 1554 | if (value) { |
| 1555 | for (var i = 0; i < value.length; i++) { |
| 1556 | this.rawWriteFieldHeader_( |
| 1557 | field, jspb.BinaryConstants.WireType.START_GROUP); |
| 1558 | writerCallback(value[i], this); |
| 1559 | this.rawWriteFieldHeader_( |
| 1560 | field, jspb.BinaryConstants.WireType.END_GROUP); |
| 1561 | } |
| 1562 | } |
| 1563 | }; |
| 1564 | |
| 1565 | |
| 1566 | /** |
| 1567 | * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to |
| 1568 | * the buffer. |
| 1569 | * @param {number} field The field number. |
| 1570 | * @param {?Array.<string>} value The array of hashes to write. |
| 1571 | */ |
| 1572 | jspb.BinaryWriter.prototype.writeRepeatedFixedHash64 = |
| 1573 | function(field, value) { |
| 1574 | if (value == null) return; |
| 1575 | for (var i = 0; i < value.length; i++) { |
| 1576 | this.writeFixedHash64(field, value[i]); |
| 1577 | } |
| 1578 | }; |
| 1579 | |
| 1580 | |
| 1581 | /** |
| 1582 | * Writes a repeated 64-bit hash string field (8 characters @ 8 bits of data |
| 1583 | * each) to the buffer. |
| 1584 | * @param {number} field The field number. |
| 1585 | * @param {?Array.<string>} value The array of hashes to write. |
| 1586 | */ |
| 1587 | jspb.BinaryWriter.prototype.writeRepeatedVarintHash64 = |
| 1588 | function(field, value) { |
| 1589 | if (value == null) return; |
| 1590 | for (var i = 0; i < value.length; i++) { |
| 1591 | this.writeVarintHash64(field, value[i]); |
| 1592 | } |
| 1593 | }; |
| 1594 | |
| 1595 | |
| 1596 | /** |
| 1597 | * 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. |
| 1603 | * @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 |
| 1608 | */ |
| 1609 | jspb.BinaryWriter.prototype.writePackedUnsignedVarint32_ = |
| 1610 | function(field, value, opt_buffer, opt_start, opt_end) { |
| 1611 | if (value != null && value.length) { |
| 1612 | var bookmark = this.beginDelimited_(field); |
| 1613 | for (var i = 0; i < value.length; i++) { |
| 1614 | this.rawWriteUnsignedVarint32(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 | } |
| 1620 | }; |
| 1621 | |
| 1622 | |
| 1623 | /** |
| 1624 | * 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. |
| 1630 | * @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 |
| 1635 | */ |
| 1636 | jspb.BinaryWriter.prototype.writePackedSignedVarint32_ = |
| 1637 | function(field, value, opt_buffer, opt_start, opt_end) { |
| 1638 | if (value != null && value.length) { |
| 1639 | var bookmark = this.beginDelimited_(field); |
| 1640 | for (var i = 0; i < value.length; i++) { |
| 1641 | this.rawWriteSignedVarint32(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 | } |
| 1647 | }; |
| 1648 | |
| 1649 | |
| 1650 | /** |
| 1651 | * 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. |
| 1657 | * @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 |
| 1662 | */ |
| 1663 | jspb.BinaryWriter.prototype.writePackedVarint_ = |
| 1664 | function(field, value, opt_buffer, opt_start, opt_end) { |
| 1665 | if (value != null && value.length) { |
| 1666 | var bookmark = this.beginDelimited_(field); |
| 1667 | for (var i = 0; i < value.length; i++) { |
| 1668 | this.rawWriteVarint(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 | } |
| 1674 | }; |
| 1675 | |
| 1676 | |
| 1677 | /** |
| 1678 | * 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. |
| 1684 | * @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 |
| 1689 | */ |
| 1690 | jspb.BinaryWriter.prototype.writePackedZigzag_ = |
| 1691 | function(field, value, opt_buffer, opt_start, opt_end) { |
| 1692 | if (value != null && value.length) { |
| 1693 | var bookmark = this.beginDelimited_(field); |
| 1694 | for (var i = 0; i < value.length; 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 | } |
| 1701 | }; |
| 1702 | |
| 1703 | |
| 1704 | /** |
| 1705 | * 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. |
| 1711 | * @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 | */ |
| 1716 | jspb.BinaryWriter.prototype.writePackedInt32 = |
| 1717 | jspb.BinaryWriter.prototype.writePackedSignedVarint32_; |
| 1718 | |
| 1719 | |
| 1720 | /** |
| 1721 | * Writes an array of numbers represented as strings to the buffer as a packed |
| 1722 | * 32-bit int field. |
| 1723 | * @param {number} field |
| 1724 | * @param {?Array.<string>} value |
| 1725 | */ |
| 1726 | jspb.BinaryWriter.prototype.writePackedInt32String = function(field, value) { |
| 1727 | if (value == null || !value.length) return; |
| 1728 | var bookmark = this.beginDelimited_(field); |
| 1729 | for (var i = 0; i < value.length; i++) { |
| 1730 | this.rawWriteSignedVarint32(parseInt(value[i], 10)); |
| 1731 | } |
| 1732 | this.endDelimited_(bookmark); |
| 1733 | }; |
| 1734 | |
| 1735 | |
| 1736 | /** |
| 1737 | * Writes an array of numbers to the buffer as a packed 64-bit int field. |
| 1738 | * @param {number} field The field number. |
| 1739 | * @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 | */ |
| 1744 | jspb.BinaryWriter.prototype.writePackedInt64 = |
| 1745 | jspb.BinaryWriter.prototype.writePackedVarint_; |
| 1746 | |
| 1747 | |
| 1748 | /** |
| 1749 | * Writes an array of numbers represented as strings to the buffer as a packed |
| 1750 | * 64-bit int field. |
| 1751 | * @param {number} field |
| 1752 | * @param {?Array.<string>} value |
| 1753 | */ |
| 1754 | jspb.BinaryWriter.prototype.writePackedInt64String = |
| 1755 | function(field, value) { |
| 1756 | if (value == null || !value.length) return; |
| 1757 | var bookmark = this.beginDelimited_(field); |
| 1758 | for (var i = 0; i < value.length; i++) { |
| 1759 | var num = jspb.arith.Int64.fromString(value[i]); |
| 1760 | this.rawWriteVarintFromNum(num); |
| 1761 | } |
| 1762 | this.endDelimited_(bookmark); |
| 1763 | }; |
| 1764 | |
| 1765 | |
| 1766 | /** |
| 1767 | * 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. |
| 1773 | * @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 | */ |
| 1778 | jspb.BinaryWriter.prototype.writePackedUint32 = |
| 1779 | jspb.BinaryWriter.prototype.writePackedUnsignedVarint32_; |
| 1780 | |
| 1781 | |
| 1782 | /** |
| 1783 | * Writes an array of numbers represented as strings to the buffer as a packed |
| 1784 | * unsigned 32-bit int field. |
| 1785 | * @param {number} field |
| 1786 | * @param {?Array.<string>} value |
| 1787 | */ |
| 1788 | jspb.BinaryWriter.prototype.writePackedUint32String = |
| 1789 | function(field, value) { |
| 1790 | if (value == null || !value.length) return; |
| 1791 | var bookmark = this.beginDelimited_(field); |
| 1792 | for (var i = 0; i < value.length; i++) { |
| 1793 | this.rawWriteUnsignedVarint32(parseInt(value[i], 10)); |
| 1794 | } |
| 1795 | this.endDelimited_(bookmark); |
| 1796 | }; |
| 1797 | |
| 1798 | |
| 1799 | /** |
| 1800 | * 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. |
| 1806 | * @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 | */ |
| 1811 | jspb.BinaryWriter.prototype.writePackedUint64 = |
| 1812 | jspb.BinaryWriter.prototype.writePackedVarint_; |
| 1813 | |
| 1814 | |
| 1815 | /** |
| 1816 | * Writes an array of numbers represented as strings to the buffer as a packed |
| 1817 | * unsigned 64-bit int field. |
| 1818 | * @param {number} field |
| 1819 | * @param {?Array.<string>} value |
| 1820 | */ |
| 1821 | jspb.BinaryWriter.prototype.writePackedUint64String = |
| 1822 | function(field, value) { |
| 1823 | if (value == null || !value.length) return; |
| 1824 | var bookmark = this.beginDelimited_(field); |
| 1825 | for (var i = 0; i < value.length; i++) { |
| 1826 | var num = jspb.arith.UInt64.fromString(value[i]); |
| 1827 | this.rawWriteVarintFromNum(num); |
| 1828 | } |
| 1829 | this.endDelimited_(bookmark); |
| 1830 | }; |
| 1831 | |
| 1832 | |
| 1833 | /** |
| 1834 | * 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. |
| 1840 | * @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 | */ |
| 1845 | jspb.BinaryWriter.prototype.writePackedSint32 = |
| 1846 | jspb.BinaryWriter.prototype.writePackedZigzag_; |
| 1847 | |
| 1848 | |
| 1849 | /** |
| 1850 | * 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. |
| 1856 | * @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 | */ |
| 1861 | jspb.BinaryWriter.prototype.writePackedSint64 = |
| 1862 | jspb.BinaryWriter.prototype.writePackedZigzag_; |
| 1863 | |
| 1864 | |
| 1865 | /** |
| 1866 | * 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. |
| 1872 | * @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 | */ |
| 1877 | jspb.BinaryWriter.prototype.writePackedFixed32 = |
| 1878 | function(field, value, opt_buffer, opt_start, opt_end) { |
| 1879 | if (value != null && value.length) { |
| 1880 | this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); |
| 1881 | this.rawWriteUnsignedVarint32(value.length * 4); |
| 1882 | for (var i = 0; i < value.length; 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 | } |
| 1888 | }; |
| 1889 | |
| 1890 | |
| 1891 | /** |
| 1892 | * 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. |
| 1898 | * @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 | */ |
| 1903 | jspb.BinaryWriter.prototype.writePackedFixed64 = |
| 1904 | function(field, value, opt_buffer, opt_start, opt_end) { |
| 1905 | if (value != null && value.length) { |
| 1906 | this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); |
| 1907 | this.rawWriteUnsignedVarint32(value.length * 8); |
| 1908 | for (var i = 0; i < value.length; 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 | } |
| 1914 | }; |
| 1915 | |
| 1916 | |
| 1917 | /** |
| 1918 | * 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. |
| 1924 | * @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 | */ |
| 1929 | jspb.BinaryWriter.prototype.writePackedSfixed32 = |
| 1930 | function(field, value, opt_buffer, opt_start, opt_end) { |
| 1931 | if (value != null && value.length) { |
| 1932 | this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); |
| 1933 | this.rawWriteUnsignedVarint32(value.length * 4); |
| 1934 | for (var i = 0; i < value.length; 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 | } |
| 1940 | }; |
| 1941 | |
| 1942 | |
| 1943 | /** |
| 1944 | * 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. |
| 1950 | * @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 | */ |
| 1955 | jspb.BinaryWriter.prototype.writePackedSfixed64 = |
| 1956 | function(field, value, opt_buffer, opt_start, opt_end) { |
| 1957 | if (value != null) { |
| 1958 | this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); |
| 1959 | this.rawWriteUnsignedVarint32(value.length * 8); |
| 1960 | for (var i = 0; i < value.length; 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 | } |
| 1966 | }; |
| 1967 | |
| 1968 | |
| 1969 | /** |
| 1970 | * 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. |
| 1976 | * @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 | */ |
| 1981 | jspb.BinaryWriter.prototype.writePackedFloat = |
| 1982 | function(field, value, opt_buffer, opt_start, opt_end) { |
| 1983 | if (value != null && value.length) { |
| 1984 | this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); |
| 1985 | this.rawWriteUnsignedVarint32(value.length * 4); |
| 1986 | for (var i = 0; i < value.length; 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 | } |
| 1992 | }; |
| 1993 | |
| 1994 | |
| 1995 | /** |
| 1996 | * 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. |
| 2002 | * @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 | */ |
| 2007 | jspb.BinaryWriter.prototype.writePackedDouble = |
| 2008 | function(field, value, opt_buffer, opt_start, opt_end) { |
| 2009 | if (value != null && value.length) { |
| 2010 | this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); |
| 2011 | this.rawWriteUnsignedVarint32(value.length * 8); |
| 2012 | for (var i = 0; i < value.length; 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 | } |
| 2018 | }; |
| 2019 | |
| 2020 | |
| 2021 | /** |
| 2022 | * 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. |
| 2028 | * @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 | */ |
| 2033 | jspb.BinaryWriter.prototype.writePackedBool = |
| 2034 | function(field, value, opt_buffer, opt_start, opt_end) { |
| 2035 | if (value != null && value.length) { |
| 2036 | this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); |
| 2037 | this.rawWriteUnsignedVarint32(value.length); |
| 2038 | for (var i = 0; i < value.length; 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 | } |
| 2044 | }; |
| 2045 | |
| 2046 | |
| 2047 | /** |
| 2048 | * 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. |
| 2054 | * @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 | */ |
| 2059 | jspb.BinaryWriter.prototype.writePackedEnum = |
| 2060 | function(field, value, opt_buffer, opt_start, opt_end) { |
| 2061 | if (value != null && value.length) { |
| 2062 | var bookmark = this.beginDelimited_(field); |
| 2063 | for (var i = 0; i < value.length; 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 | } |
| 2070 | }; |
| 2071 | |
| 2072 | |
| 2073 | /** |
| 2074 | * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to |
| 2075 | * 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. |
| 2081 | * @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 | */ |
| 2086 | jspb.BinaryWriter.prototype.writePackedFixedHash64 = |
| 2087 | function(field, value, opt_buffer, opt_start, opt_end) { |
| 2088 | if (value != null && value.length) { |
| 2089 | this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); |
| 2090 | this.rawWriteUnsignedVarint32(value.length * 8); |
| 2091 | for (var i = 0; i < value.length; 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 | } |
| 2097 | }; |
| 2098 | |
| 2099 | |
| 2100 | /** |
| 2101 | * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to |
| 2102 | * 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. |
| 2108 | * @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 | */ |
| 2113 | jspb.BinaryWriter.prototype.writePackedVarintHash64 = |
| 2114 | function(field, value, opt_buffer, opt_start, opt_end) { |
| 2115 | if (value != null && value.length) { |
| 2116 | var bookmark = this.beginDelimited_(field); |
| 2117 | for (var i = 0; i < value.length; 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 | } |
| 2124 | }; |