123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124 |
- // Protocol Buffers - Google's data interchange format
- // Copyright 2008 Google Inc. All rights reserved.
- // https://developers.google.com/protocol-buffers/
- //
- // Redistribution and use in source and binary forms, with or without
- // modification, are permitted provided that the following conditions are
- // met:
- //
- // * Redistributions of source code must retain the above copyright
- // notice, this list of conditions and the following disclaimer.
- // * Redistributions in binary form must reproduce the above
- // copyright notice, this list of conditions and the following disclaimer
- // in the documentation and/or other materials provided with the
- // distribution.
- // * Neither the name of Google Inc. nor the names of its
- // contributors may be used to endorse or promote products derived from
- // this software without specific prior written permission.
- //
- // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- /**
- * @fileoverview This file contains utilities for encoding Javascript objects
- * into binary, wire-format protocol buffers (in the form of Uint8Arrays) that
- * a server can consume directly.
- *
- * jspb's BinaryWriter class defines methods for efficiently encoding
- * Javascript objects into binary, wire-format protocol buffers and supports
- * all the fundamental field types used in protocol buffers.
- *
- * Major caveat 1 - Users of this library _must_ keep their Javascript proto
- * parsing code in sync with the original .proto file - presumably you'll be
- * using the typed jspb code generator, but if you bypass that you'll need
- * to keep things in sync by hand.
- *
- * Major caveat 2 - Javascript is unable to accurately represent integers
- * larger than 2^53 due to its use of a double-precision floating point format
- * for all numbers. BinaryWriter does not make any special effort to preserve
- * precision for values above this limit - if you need to pass 64-bit integers
- * (hash codes, for example) between the client and server without precision
- * loss, do _not_ use this library.
- *
- * Major caveat 3 - This class uses typed arrays and must not be used on older
- * browsers that do not support them.
- *
- * @author aappleby@google.com (Austin Appleby)
- */
- goog.provide('jspb.BinaryWriter');
- goog.require('goog.asserts');
- goog.require('goog.crypt.base64');
- goog.require('jspb.BinaryConstants');
- goog.require('jspb.arith.Int64');
- goog.require('jspb.arith.UInt64');
- goog.require('jspb.utils');
- goog.forwardDeclare('jspb.Message');
- /**
- * BinaryWriter implements encoders for all the wire types specified in
- * https://developers.google.com/protocol-buffers/docs/encoding.
- *
- * @constructor
- * @struct
- */
- jspb.BinaryWriter = function() {
- /**
- * Blocks of serialized data that will be concatenated once all messages have
- * been written.
- * @private {!Array<!Uint8Array|!Array<number>>}
- */
- this.blocks_ = [];
- /**
- * Total number of bytes in the blocks_ array. Does _not_ include the temp
- * buffer.
- * @private {number}
- */
- this.totalLength_ = 0;
- /**
- * Temporary buffer holding a message that we're still serializing. When we
- * get to a stopping point (either the start of a new submessage, or when we
- * need to append a raw Uint8Array), the temp buffer will be added to the
- * block array above and a new temp buffer will be created.
- * @private {!Array.<number>}
- */
- this.temp_ = [];
- /**
- * A stack of bookmarks containing the parent blocks for each message started
- * via beginSubMessage(), needed as bookkeeping for endSubMessage().
- * TODO(aappleby): Deprecated, users should be calling writeMessage().
- * @private {!Array.<!jspb.BinaryWriter.Bookmark_>}
- */
- this.bookmarks_ = [];
- };
- /**
- * @typedef {{block: !Array.<number>, length: number}}
- * @private
- */
- jspb.BinaryWriter.Bookmark_;
- /**
- * Saves the current temp buffer in the blocks_ array and starts a new one.
- * @return {!Array.<number>} Returns a reference to the old temp buffer.
- * @private
- */
- jspb.BinaryWriter.prototype.saveTempBuffer_ = function() {
- var oldTemp = this.temp_;
- this.blocks_.push(this.temp_);
- this.totalLength_ += this.temp_.length;
- this.temp_ = [];
- return oldTemp;
- };
- /**
- * Append a typed array of bytes onto the buffer.
- *
- * @param {!Uint8Array} arr The byte array to append.
- * @private
- */
- jspb.BinaryWriter.prototype.appendUint8Array_ = function(arr) {
- if (this.temp_.length) {
- this.saveTempBuffer_();
- }
- this.blocks_.push(arr);
- this.totalLength_ += arr.length;
- };
- /**
- * Append an untyped array of bytes onto the buffer.
- *
- * @param {!Array.<number>} arr The byte array to append.
- * @private
- */
- jspb.BinaryWriter.prototype.appendArray_ = function(arr) {
- if (this.temp_.length) {
- this.saveTempBuffer_();
- }
- this.temp_ = arr;
- };
- /**
- * Begins a length-delimited section by writing the field header to the current
- * temp buffer and then saving it in the block array. Returns the saved block,
- * which we will append the length to in endDelimited_ below.
- * @param {number} field
- * @return {!jspb.BinaryWriter.Bookmark_}
- * @private
- */
- jspb.BinaryWriter.prototype.beginDelimited_ = function(field) {
- this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
- return {block: this.saveTempBuffer_(), length: this.totalLength_};
- };
- /**
- * Ends a length-delimited block by encoding the _change_ in length of the
- * buffer to the parent block and adds the number of bytes needed to encode
- * that length to the total byte length. Note that 'parentLength' _must_ be the
- * total length _after_ the field header was written in beginDelimited_ above.
- * @param {!jspb.BinaryWriter.Bookmark_} bookmark
- * @private
- */
- jspb.BinaryWriter.prototype.endDelimited_ = function(bookmark) {
- var messageLength = this.totalLength_ + this.temp_.length - bookmark.length;
- goog.asserts.assert(messageLength >= 0);
- var bytes = 1;
- while (messageLength > 127) {
- bookmark.block.push((messageLength & 0x7f) | 0x80);
- messageLength = messageLength >>> 7;
- bytes++;
- }
- bookmark.block.push(messageLength);
- this.totalLength_ += bytes;
- };
- /**
- * Resets the writer, throwing away any accumulated buffers.
- */
- jspb.BinaryWriter.prototype.reset = function() {
- this.blocks_ = [];
- this.temp_ = [];
- this.totalLength_ = 0;
- this.bookmarks_ = [];
- };
- /**
- * Converts the encoded data into a Uint8Array.
- * @return {!Uint8Array}
- */
- jspb.BinaryWriter.prototype.getResultBuffer = function() {
- goog.asserts.assert(this.bookmarks_.length == 0);
- var flat = new Uint8Array(this.totalLength_ + this.temp_.length);
- var blocks = this.blocks_;
- var blockCount = blocks.length;
- var offset = 0;
- for (var i = 0; i < blockCount; i++) {
- var block = blocks[i];
- flat.set(block, offset);
- offset += block.length;
- }
- flat.set(this.temp_, offset);
- offset += this.temp_.length;
- // Post condition: `flattened` must have had every byte written.
- goog.asserts.assert(offset == flat.length);
- // Replace our block list with the flattened block, which lets GC reclaim
- // the temp blocks sooner.
- this.blocks_ = [flat];
- this.temp_ = [];
- return flat;
- };
- /**
- * Converts the encoded data into a bas64-encoded string.
- * @return {string}
- */
- jspb.BinaryWriter.prototype.getResultBase64String = function() {
- return goog.crypt.base64.encodeByteArray(this.getResultBuffer());
- };
- /**
- * Begins a new sub-message. The client must call endSubMessage() when they're
- * done.
- * TODO(aappleby): Deprecated. Move callers to writeMessage().
- * @param {number} field The field number of the sub-message.
- */
- jspb.BinaryWriter.prototype.beginSubMessage = function(field) {
- this.bookmarks_.push(this.beginDelimited_(field));
- };
- /**
- * Finishes a sub-message and packs it into the parent messages' buffer.
- * TODO(aappleby): Deprecated. Move callers to writeMessage().
- */
- jspb.BinaryWriter.prototype.endSubMessage = function() {
- goog.asserts.assert(this.bookmarks_.length >= 0);
- this.endDelimited_(this.bookmarks_.pop());
- };
- /**
- * Encodes a 32-bit unsigned integer into its wire-format varint representation
- * and stores it in the buffer.
- * @param {number} value The integer to convert.
- */
- jspb.BinaryWriter.prototype.rawWriteUnsignedVarint32 = function(value) {
- goog.asserts.assert(value == Math.floor(value));
- while (value > 127) {
- this.temp_.push((value & 0x7f) | 0x80);
- value = value >>> 7;
- }
- this.temp_.push(value);
- };
- /**
- * Encodes a 32-bit signed integer into its wire-format varint representation
- * and stores it in the buffer.
- * @param {number} value The integer to convert.
- */
- jspb.BinaryWriter.prototype.rawWriteSignedVarint32 = function(value) {
- goog.asserts.assert(value == Math.floor(value));
- if (value >= 0) {
- this.rawWriteUnsignedVarint32(value);
- return;
- }
- // Write nine bytes with a _signed_ right shift so we preserve the sign bit.
- for (var i = 0; i < 9; i++) {
- this.temp_.push((value & 0x7f) | 0x80);
- value = value >> 7;
- }
- // The above loop writes out 63 bits, so the last byte is always the sign bit
- // which is always set for negative numbers.
- this.temp_.push(1);
- };
- /**
- * Encodes an unsigned 64-bit integer in 32:32 split representation into its
- * wire-format varint representation and stores it in the buffer.
- * @param {number} lowBits The low 32 bits of the int.
- * @param {number} highBits The high 32 bits of the int.
- */
- jspb.BinaryWriter.prototype.rawWriteSplitVarint =
- function(lowBits, highBits) {
- // Break the binary representation into chunks of 7 bits, set the 8th bit
- // in each chunk if it's not the final chunk, and append to the result.
- while (highBits > 0 || lowBits > 127) {
- this.temp_.push((lowBits & 0x7f) | 0x80);
- lowBits = ((lowBits >>> 7) | (highBits << 25)) >>> 0;
- highBits = highBits >>> 7;
- }
- this.temp_.push(lowBits);
- };
- /**
- * Encodes a JavaScript integer into its wire-format varint representation and
- * stores it in the buffer. Due to the way the varint encoding works this
- * behaves correctly for both signed and unsigned integers, though integers
- * that are not representable in 64 bits will still be truncated.
- * @param {number} value The integer to convert.
- */
- jspb.BinaryWriter.prototype.rawWriteVarint = function(value) {
- goog.asserts.assert(value == Math.floor(value));
- jspb.utils.splitInt64(value);
- this.rawWriteSplitVarint(jspb.utils.split64Low,
- jspb.utils.split64High);
- };
- /**
- * Encodes a jspb.arith.{Int64,UInt64} instance into its wire-format
- * varint representation and stores it in the buffer. Due to the way the varint
- * encoding works this behaves correctly for both signed and unsigned integers,
- * though integers that are not representable in 64 bits will still be
- * truncated.
- * @param {jspb.arith.Int64|jspb.arith.UInt64} value
- */
- jspb.BinaryWriter.prototype.rawWriteVarintFromNum = function(value) {
- this.rawWriteSplitVarint(value.lo, value.hi);
- };
- /**
- * Encodes a JavaScript integer into its wire-format, zigzag-encoded varint
- * representation and stores it in the buffer.
- * @param {number} value The integer to convert.
- */
- jspb.BinaryWriter.prototype.rawWriteZigzagVarint32 = function(value) {
- goog.asserts.assert(value == Math.floor(value));
- this.rawWriteUnsignedVarint32(((value << 1) ^ (value >> 31)) >>> 0);
- };
- /**
- * Encodes a JavaScript integer into its wire-format, zigzag-encoded varint
- * representation and stores it in the buffer. Integers not representable in 64
- * bits will be truncated.
- * @param {number} value The integer to convert.
- */
- jspb.BinaryWriter.prototype.rawWriteZigzagVarint = function(value) {
- goog.asserts.assert(value == Math.floor(value));
- jspb.utils.splitZigzag64(value);
- this.rawWriteSplitVarint(jspb.utils.split64Low,
- jspb.utils.split64High);
- };
- /**
- * Writes a raw 8-bit unsigned integer to the buffer. Numbers outside the range
- * [0,2^8) will be truncated.
- * @param {number} value The value to write.
- */
- jspb.BinaryWriter.prototype.rawWriteUint8 = function(value) {
- goog.asserts.assert(value == Math.floor(value));
- goog.asserts.assert((value >= 0) && (value < 256));
- this.temp_.push((value >>> 0) & 0xFF);
- };
- /**
- * Writes a raw 16-bit unsigned integer to the buffer. Numbers outside the
- * range [0,2^16) will be truncated.
- * @param {number} value The value to write.
- */
- jspb.BinaryWriter.prototype.rawWriteUint16 = function(value) {
- goog.asserts.assert(value == Math.floor(value));
- goog.asserts.assert((value >= 0) && (value < 65536));
- this.temp_.push((value >>> 0) & 0xFF);
- this.temp_.push((value >>> 8) & 0xFF);
- };
- /**
- * Writes a raw 32-bit unsigned integer to the buffer. Numbers outside the
- * range [0,2^32) will be truncated.
- * @param {number} value The value to write.
- */
- jspb.BinaryWriter.prototype.rawWriteUint32 = function(value) {
- goog.asserts.assert(value == Math.floor(value));
- goog.asserts.assert((value >= 0) &&
- (value < jspb.BinaryConstants.TWO_TO_32));
- this.temp_.push((value >>> 0) & 0xFF);
- this.temp_.push((value >>> 8) & 0xFF);
- this.temp_.push((value >>> 16) & 0xFF);
- this.temp_.push((value >>> 24) & 0xFF);
- };
- /**
- * Writes a raw 64-bit unsigned integer to the buffer. Numbers outside the
- * range [0,2^64) will be truncated.
- * @param {number} value The value to write.
- */
- jspb.BinaryWriter.prototype.rawWriteUint64 = function(value) {
- goog.asserts.assert(value == Math.floor(value));
- goog.asserts.assert((value >= 0) &&
- (value < jspb.BinaryConstants.TWO_TO_64));
- jspb.utils.splitUint64(value);
- this.rawWriteUint32(jspb.utils.split64Low);
- this.rawWriteUint32(jspb.utils.split64High);
- };
- /**
- * Writes a raw 8-bit integer to the buffer. Numbers outside the range
- * [-2^7,2^7) will be truncated.
- * @param {number} value The value to write.
- */
- jspb.BinaryWriter.prototype.rawWriteInt8 = function(value) {
- goog.asserts.assert(value == Math.floor(value));
- goog.asserts.assert((value >= -128) && (value < 128));
- this.temp_.push((value >>> 0) & 0xFF);
- };
- /**
- * Writes a raw 16-bit integer to the buffer. Numbers outside the range
- * [-2^15,2^15) will be truncated.
- * @param {number} value The value to write.
- */
- jspb.BinaryWriter.prototype.rawWriteInt16 = function(value) {
- goog.asserts.assert(value == Math.floor(value));
- goog.asserts.assert((value >= -32768) && (value < 32768));
- this.temp_.push((value >>> 0) & 0xFF);
- this.temp_.push((value >>> 8) & 0xFF);
- };
- /**
- * Writes a raw 32-bit integer to the buffer. Numbers outside the range
- * [-2^31,2^31) will be truncated.
- * @param {number} value The value to write.
- */
- jspb.BinaryWriter.prototype.rawWriteInt32 = function(value) {
- goog.asserts.assert(value == Math.floor(value));
- goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
- (value < jspb.BinaryConstants.TWO_TO_31));
- this.temp_.push((value >>> 0) & 0xFF);
- this.temp_.push((value >>> 8) & 0xFF);
- this.temp_.push((value >>> 16) & 0xFF);
- this.temp_.push((value >>> 24) & 0xFF);
- };
- /**
- * Writes a raw 64-bit integer to the buffer. Numbers outside the range
- * [-2^63,2^63) will be truncated.
- * @param {number} value The value to write.
- */
- jspb.BinaryWriter.prototype.rawWriteInt64 = function(value) {
- goog.asserts.assert(value == Math.floor(value));
- goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
- (value < jspb.BinaryConstants.TWO_TO_63));
- jspb.utils.splitInt64(value);
- this.rawWriteUint32(jspb.utils.split64Low);
- this.rawWriteUint32(jspb.utils.split64High);
- };
- /**
- * Writes a raw single-precision floating point value to the buffer. Numbers
- * requiring more than 32 bits of precision will be truncated.
- * @param {number} value The value to write.
- */
- jspb.BinaryWriter.prototype.rawWriteFloat = function(value) {
- jspb.utils.splitFloat32(value);
- this.rawWriteUint32(jspb.utils.split64Low);
- };
- /**
- * Writes a raw double-precision floating point value to the buffer. As this is
- * the native format used by JavaScript, no precision will be lost.
- * @param {number} value The value to write.
- */
- jspb.BinaryWriter.prototype.rawWriteDouble = function(value) {
- jspb.utils.splitFloat64(value);
- this.rawWriteUint32(jspb.utils.split64Low);
- this.rawWriteUint32(jspb.utils.split64High);
- };
- /**
- * Writes a raw boolean value to the buffer as a varint.
- * @param {boolean} value The value to write.
- */
- jspb.BinaryWriter.prototype.rawWriteBool = function(value) {
- goog.asserts.assert(goog.isBoolean(value));
- this.temp_.push(~~value);
- };
- /**
- * Writes an raw enum value to the buffer as a varint.
- * @param {number} value The value to write.
- */
- jspb.BinaryWriter.prototype.rawWriteEnum = function(value) {
- goog.asserts.assert(value == Math.floor(value));
- goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
- (value < jspb.BinaryConstants.TWO_TO_31));
- this.rawWriteSignedVarint32(value);
- };
- /**
- * Writes a raw string value to the buffer.
- * @param {string} string The string to write.
- */
- jspb.BinaryWriter.prototype.rawWriteUtf8String = function(string) {
- for (var i = 0; i < string.length; i++) {
- this.temp_.push(string.charCodeAt(i));
- }
- };
- /**
- * Writes an arbitrary raw byte array to the buffer.
- * @param {!Uint8Array} bytes The array of bytes to write.
- */
- jspb.BinaryWriter.prototype.rawWriteBytes = function(bytes) {
- this.appendUint8Array_(bytes);
- };
- /**
- * Writes an arbitrary raw byte array to the buffer.
- * @param {!Uint8Array} bytes The array of bytes to write.
- * @param {number} start The start of the range to write.
- * @param {number} end The end of the range to write.
- */
- jspb.BinaryWriter.prototype.rawWriteByteRange = function(bytes, start, end) {
- this.appendUint8Array_(bytes.subarray(start, end));
- };
- /**
- * Writes a 64-bit hash string (8 characters @ 8 bits of data each) to the
- * buffer as a varint.
- * @param {string} hash The hash to write.
- */
- jspb.BinaryWriter.prototype.rawWriteVarintHash64 = function(hash) {
- jspb.utils.splitHash64(hash);
- this.rawWriteSplitVarint(jspb.utils.split64Low,
- jspb.utils.split64High);
- };
- /**
- * Writes a 64-bit hash string (8 characters @ 8 bits of data each) to the
- * buffer as a fixed64.
- * @param {string} hash The hash to write.
- */
- jspb.BinaryWriter.prototype.rawWriteFixedHash64 = function(hash) {
- jspb.utils.splitHash64(hash);
- this.rawWriteUint32(jspb.utils.split64Low);
- this.rawWriteUint32(jspb.utils.split64High);
- };
- /**
- * Encodes a (field number, wire type) tuple into a wire-format field header
- * and stores it in the buffer as a varint.
- * @param {number} field The field number.
- * @param {number} wireType The wire-type of the field, as specified in the
- * protocol buffer documentation.
- * @private
- */
- jspb.BinaryWriter.prototype.rawWriteFieldHeader_ =
- function(field, wireType) {
- goog.asserts.assert(field >= 1 && field == Math.floor(field));
- var x = field * 8 + wireType;
- this.rawWriteUnsignedVarint32(x);
- };
- /**
- * Writes a field of any valid scalar type to the binary stream.
- * @param {jspb.BinaryConstants.FieldType} fieldType
- * @param {number} field
- * @param {jspb.AnyFieldType} value
- */
- jspb.BinaryWriter.prototype.writeAny = function(fieldType, field, value) {
- var fieldTypes = jspb.BinaryConstants.FieldType;
- switch (fieldType) {
- case fieldTypes.DOUBLE:
- this.writeDouble(field, /** @type {number} */(value));
- return;
- case fieldTypes.FLOAT:
- this.writeFloat(field, /** @type {number} */(value));
- return;
- case fieldTypes.INT64:
- this.writeInt64(field, /** @type {number} */(value));
- return;
- case fieldTypes.UINT64:
- this.writeUint64(field, /** @type {number} */(value));
- return;
- case fieldTypes.INT32:
- this.writeInt32(field, /** @type {number} */(value));
- return;
- case fieldTypes.FIXED64:
- this.writeFixed64(field, /** @type {number} */(value));
- return;
- case fieldTypes.FIXED32:
- this.writeFixed32(field, /** @type {number} */(value));
- return;
- case fieldTypes.BOOL:
- this.writeBool(field, /** @type {boolean} */(value));
- return;
- case fieldTypes.STRING:
- this.writeString(field, /** @type {string} */(value));
- return;
- case fieldTypes.GROUP:
- goog.asserts.fail('Group field type not supported in writeAny()');
- return;
- case fieldTypes.MESSAGE:
- goog.asserts.fail('Message field type not supported in writeAny()');
- return;
- case fieldTypes.BYTES:
- this.writeBytes(field, /** @type {?Uint8Array} */(value));
- return;
- case fieldTypes.UINT32:
- this.writeUint32(field, /** @type {number} */(value));
- return;
- case fieldTypes.ENUM:
- this.writeEnum(field, /** @type {number} */(value));
- return;
- case fieldTypes.SFIXED32:
- this.writeSfixed32(field, /** @type {number} */(value));
- return;
- case fieldTypes.SFIXED64:
- this.writeSfixed64(field, /** @type {number} */(value));
- return;
- case fieldTypes.SINT32:
- this.writeSint32(field, /** @type {number} */(value));
- return;
- case fieldTypes.SINT64:
- this.writeSint64(field, /** @type {number} */(value));
- return;
- case fieldTypes.FHASH64:
- this.writeFixedHash64(field, /** @type {string} */(value));
- return;
- case fieldTypes.VHASH64:
- this.writeVarintHash64(field, /** @type {string} */(value));
- return;
- default:
- goog.asserts.fail('Invalid field type in writeAny()');
- return;
- }
- };
- /**
- * Writes a varint field to the buffer without range checking.
- * @param {number} field The field number.
- * @param {number?} value The value to write.
- * @private
- */
- jspb.BinaryWriter.prototype.writeUnsignedVarint32_ = function(field, value) {
- if (value == null) return;
- this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
- this.rawWriteSignedVarint32(value);
- };
- /**
- * Writes a varint field to the buffer without range checking.
- * @param {number} field The field number.
- * @param {number?} value The value to write.
- * @private
- */
- jspb.BinaryWriter.prototype.writeSignedVarint32_ = function(field, value) {
- if (value == null) return;
- this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
- this.rawWriteSignedVarint32(value);
- };
- /**
- * Writes a varint field to the buffer without range checking.
- * @param {number} field The field number.
- * @param {number?} value The value to write.
- * @private
- */
- jspb.BinaryWriter.prototype.writeVarint_ = function(field, value) {
- if (value == null) return;
- this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
- this.rawWriteVarint(value);
- };
- /**
- * Writes a zigzag varint field to the buffer without range checking.
- * @param {number} field The field number.
- * @param {number?} value The value to write.
- * @private
- */
- jspb.BinaryWriter.prototype.writeZigzagVarint32_ = function(field, value) {
- if (value == null) return;
- this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
- this.rawWriteZigzagVarint32(value);
- };
- /**
- * Writes a zigzag varint field to the buffer without range checking.
- * @param {number} field The field number.
- * @param {number?} value The value to write.
- * @private
- */
- jspb.BinaryWriter.prototype.writeZigzagVarint_ = function(field, value) {
- if (value == null) return;
- this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
- this.rawWriteZigzagVarint(value);
- };
- /**
- * Writes an int32 field to the buffer. Numbers outside the range [-2^31,2^31)
- * will be truncated.
- * @param {number} field The field number.
- * @param {number?} value The value to write.
- */
- jspb.BinaryWriter.prototype.writeInt32 = function(field, value) {
- if (value == null) return;
- goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
- (value < jspb.BinaryConstants.TWO_TO_31));
- this.writeSignedVarint32_(field, value);
- };
- /**
- * Writes an int32 field represented as a string to the buffer. Numbers outside
- * the range [-2^31,2^31) will be truncated.
- * @param {number} field The field number.
- * @param {string?} value The value to write.
- */
- jspb.BinaryWriter.prototype.writeInt32String = function(field, value) {
- if (value == null) return;
- var intValue = /** {number} */ parseInt(value, 10);
- goog.asserts.assert((intValue >= -jspb.BinaryConstants.TWO_TO_31) &&
- (intValue < jspb.BinaryConstants.TWO_TO_31));
- this.writeSignedVarint32_(field, intValue);
- };
- /**
- * Writes an int64 field to the buffer. Numbers outside the range [-2^63,2^63)
- * will be truncated.
- * @param {number} field The field number.
- * @param {number?} value The value to write.
- */
- jspb.BinaryWriter.prototype.writeInt64 = function(field, value) {
- if (value == null) return;
- goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
- (value < jspb.BinaryConstants.TWO_TO_63));
- this.writeVarint_(field, value);
- };
- /**
- * Writes a int64 field (with value as a string) to the buffer.
- * @param {number} field The field number.
- * @param {string?} value The value to write.
- */
- jspb.BinaryWriter.prototype.writeInt64String = function(field, value) {
- if (value == null) return;
- var num = jspb.arith.Int64.fromString(value);
- this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
- this.rawWriteVarintFromNum(num);
- };
- /**
- * Writes a uint32 field to the buffer. Numbers outside the range [0,2^32)
- * will be truncated.
- * @param {number} field The field number.
- * @param {number?} value The value to write.
- */
- jspb.BinaryWriter.prototype.writeUint32 = function(field, value) {
- if (value == null) return;
- goog.asserts.assert((value >= 0) &&
- (value < jspb.BinaryConstants.TWO_TO_32));
- this.writeUnsignedVarint32_(field, value);
- };
- /**
- * Writes a uint32 field represented as a string to the buffer. Numbers outside
- * the range [0,2^32) will be truncated.
- * @param {number} field The field number.
- * @param {string?} value The value to write.
- */
- jspb.BinaryWriter.prototype.writeUint32String = function(field, value) {
- if (value == null) return;
- var intValue = /** {number} */ parseInt(value, 10);
- goog.asserts.assert((intValue >= 0) &&
- (intValue < jspb.BinaryConstants.TWO_TO_32));
- this.writeUnsignedVarint32_(field, intValue);
- };
- /**
- * Writes a uint64 field to the buffer. Numbers outside the range [0,2^64)
- * will be truncated.
- * @param {number} field The field number.
- * @param {number?} value The value to write.
- */
- jspb.BinaryWriter.prototype.writeUint64 = function(field, value) {
- if (value == null) return;
- goog.asserts.assert((value >= 0) &&
- (value < jspb.BinaryConstants.TWO_TO_64));
- this.writeVarint_(field, value);
- };
- /**
- * Writes a uint64 field (with value as a string) to the buffer.
- * @param {number} field The field number.
- * @param {string?} value The value to write.
- */
- jspb.BinaryWriter.prototype.writeUint64String = function(field, value) {
- if (value == null) return;
- var num = jspb.arith.UInt64.fromString(value);
- this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
- this.rawWriteVarintFromNum(num);
- };
- /**
- * Writes a sint32 field to the buffer. Numbers outside the range [-2^31,2^31)
- * will be truncated.
- * @param {number} field The field number.
- * @param {number?} value The value to write.
- */
- jspb.BinaryWriter.prototype.writeSint32 = function(field, value) {
- if (value == null) return;
- goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
- (value < jspb.BinaryConstants.TWO_TO_31));
- this.writeZigzagVarint32_(field, value);
- };
- /**
- * Writes a sint64 field to the buffer. Numbers outside the range [-2^63,2^63)
- * will be truncated.
- * @param {number} field The field number.
- * @param {number?} value The value to write.
- */
- jspb.BinaryWriter.prototype.writeSint64 = function(field, value) {
- if (value == null) return;
- goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
- (value < jspb.BinaryConstants.TWO_TO_63));
- this.writeZigzagVarint_(field, value);
- };
- /**
- * Writes a fixed32 field to the buffer. Numbers outside the range [0,2^32)
- * will be truncated.
- * @param {number} field The field number.
- * @param {number?} value The value to write.
- */
- jspb.BinaryWriter.prototype.writeFixed32 = function(field, value) {
- if (value == null) return;
- goog.asserts.assert((value >= 0) &&
- (value < jspb.BinaryConstants.TWO_TO_32));
- this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32);
- this.rawWriteUint32(value);
- };
- /**
- * Writes a fixed64 field to the buffer. Numbers outside the range [0,2^64)
- * will be truncated.
- * @param {number} field The field number.
- * @param {number?} value The value to write.
- */
- jspb.BinaryWriter.prototype.writeFixed64 = function(field, value) {
- if (value == null) return;
- goog.asserts.assert((value >= 0) &&
- (value < jspb.BinaryConstants.TWO_TO_64));
- this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
- this.rawWriteUint64(value);
- };
- /**
- * Writes a sfixed32 field to the buffer. Numbers outside the range
- * [-2^31,2^31) will be truncated.
- * @param {number} field The field number.
- * @param {number?} value The value to write.
- */
- jspb.BinaryWriter.prototype.writeSfixed32 = function(field, value) {
- if (value == null) return;
- goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
- (value < jspb.BinaryConstants.TWO_TO_31));
- this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32);
- this.rawWriteInt32(value);
- };
- /**
- * Writes a sfixed64 field to the buffer. Numbers outside the range
- * [-2^63,2^63) will be truncated.
- * @param {number} field The field number.
- * @param {number?} value The value to write.
- */
- jspb.BinaryWriter.prototype.writeSfixed64 = function(field, value) {
- if (value == null) return;
- goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
- (value < jspb.BinaryConstants.TWO_TO_63));
- this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
- this.rawWriteInt64(value);
- };
- /**
- * Writes a single-precision floating point field to the buffer. Numbers
- * requiring more than 32 bits of precision will be truncated.
- * @param {number} field The field number.
- * @param {number?} value The value to write.
- */
- jspb.BinaryWriter.prototype.writeFloat = function(field, value) {
- if (value == null) return;
- this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32);
- this.rawWriteFloat(value);
- };
- /**
- * Writes a double-precision floating point field to the buffer. As this is the
- * native format used by JavaScript, no precision will be lost.
- * @param {number} field The field number.
- * @param {number?} value The value to write.
- */
- jspb.BinaryWriter.prototype.writeDouble = function(field, value) {
- if (value == null) return;
- this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
- this.rawWriteDouble(value);
- };
- /**
- * Writes a boolean field to the buffer.
- * @param {number} field The field number.
- * @param {boolean?} value The value to write.
- */
- jspb.BinaryWriter.prototype.writeBool = function(field, value) {
- if (value == null) return;
- goog.asserts.assert(goog.isBoolean(value));
- this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
- this.temp_.push(~~value);
- };
- /**
- * Writes an enum field to the buffer.
- * @param {number} field The field number.
- * @param {number?} value The value to write.
- */
- jspb.BinaryWriter.prototype.writeEnum = function(field, value) {
- if (value == null) return;
- goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
- (value < jspb.BinaryConstants.TWO_TO_31));
- this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
- this.rawWriteSignedVarint32(value);
- };
- /**
- * Writes a string field to the buffer.
- * @param {number} field The field number.
- * @param {string?} value The string to write.
- */
- jspb.BinaryWriter.prototype.writeString = function(field, value) {
- if (value == null) return;
- this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
- // Conversion loop swiped from goog.crypt.stringToUtf8ByteArray. Note that
- // 'bytes' will be at least as long as 'value', but could be longer if we
- // need to unpack unicode characters.
- var bytes = [];
- for (var i = 0; i < value.length; i++) {
- var c = value.charCodeAt(i);
- if (c < 128) {
- bytes.push(c);
- } else if (c < 2048) {
- bytes.push((c >> 6) | 192);
- bytes.push((c & 63) | 128);
- } else {
- bytes.push((c >> 12) | 224);
- bytes.push(((c >> 6) & 63) | 128);
- bytes.push((c & 63) | 128);
- }
- }
- this.rawWriteUnsignedVarint32(bytes.length);
- this.appendArray_(bytes);
- };
- /**
- * Writes an arbitrary byte field to the buffer. Note - to match the behavior
- * of the C++ implementation, empty byte arrays _are_ serialized.
- *
- * If 'value' is null, this method will try and copy the pre-serialized value
- * in 'opt_buffer' if present.
- *
- * @param {number} field The field number.
- * @param {jspb.ByteSource} value The array of bytes to write.
- * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
- * @param {?number=} opt_start The starting point in the above buffer.
- * @param {?number=} opt_end The ending point in the above buffer.
- * @param {boolean=} opt_stringIsRawBytes If `value` is a string, interpret it
- * as a series of raw bytes (codepoints 0--255 inclusive) rather than base64
- * data.
- */
- jspb.BinaryWriter.prototype.writeBytes =
- function(field, value, opt_buffer, opt_start, opt_end,
- opt_stringIsRawBytes) {
- if (value != null) {
- this.rawWriteFieldHeader_(field,
- jspb.BinaryConstants.WireType.DELIMITED);
- this.rawWriteUnsignedVarint32(value.length);
- this.rawWriteBytes(
- jspb.utils.byteSourceToUint8Array(value, opt_stringIsRawBytes));
- } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
- this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
- }
- };
- /**
- * Writes an arbitrary byte field to the buffer, with `opt_stringIsRawBytes`
- * flag implicitly true.
- * @param {number} field
- * @param {jspb.ByteSource} value The array of bytes to write.
- */
- jspb.BinaryWriter.prototype.writeBytesRawString = function(field, value) {
- this.writeBytes(field, value, null, null, null, true);
- };
- /**
- * Writes a message to the buffer.
- *
- * If 'value' is null, this method will try and copy the pre-serialized value
- * in 'opt_buffer' if present.
- *
- * @template MessageType
- * @param {number} field The field number.
- * @param {?MessageType} value The message to write.
- * @param {!jspb.WriterFunction} writerCallback Will be invoked with the value
- * to write and the writer to write it with.
- * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
- * @param {?number=} opt_start The starting point in the above buffer.
- * @param {?number=} opt_end The ending point in the above buffer.
- */
- jspb.BinaryWriter.prototype.writeMessage =
- function(field, value, writerCallback, opt_buffer, opt_start, opt_end) {
- if (value !== null) {
- var bookmark = this.beginDelimited_(field);
- writerCallback(value, this);
- this.endDelimited_(bookmark);
- } else if (opt_buffer && (opt_start != null) && (opt_end != null)) {
- this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
- }
- };
- /**
- * Writes a group message to the buffer.
- *
- * @template MessageType
- * @param {number} field The field number.
- * @param {?MessageType} value The message to write, wrapped with START_GROUP /
- * END_GROUP tags. Will be a no-op if 'value' is null.
- * @param {!jspb.WriterFunction} writerCallback Will be invoked with the value
- * to write and the writer to write it with.
- */
- jspb.BinaryWriter.prototype.writeGroup =
- function(field, value, writerCallback) {
- if (value) {
- this.rawWriteFieldHeader_(
- field, jspb.BinaryConstants.WireType.START_GROUP);
- writerCallback(value, this);
- this.rawWriteFieldHeader_(
- field, jspb.BinaryConstants.WireType.END_GROUP);
- }
- };
- /**
- * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
- * the buffer.
- * @param {number} field The field number.
- * @param {string?} value The hash string.
- */
- jspb.BinaryWriter.prototype.writeFixedHash64 = function(field, value) {
- if (value == null) return;
- goog.asserts.assert(value.length == 8);
- this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
- this.rawWriteFixedHash64(value);
- };
- /**
- * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
- * the buffer.
- * @param {number} field The field number.
- * @param {string?} value The hash string.
- */
- jspb.BinaryWriter.prototype.writeVarintHash64 = function(field, value) {
- if (value == null) return;
- goog.asserts.assert(value.length == 8);
- this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
- this.rawWriteVarintHash64(value);
- };
- /**
- * Writes an array of numbers to the buffer as a repeated varint field.
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- * @private
- */
- jspb.BinaryWriter.prototype.writeRepeatedUnsignedVarint32_ =
- function(field, value) {
- if (value == null) return;
- for (var i = 0; i < value.length; i++) {
- this.writeUnsignedVarint32_(field, value[i]);
- }
- };
- /**
- * Writes an array of numbers to the buffer as a repeated varint field.
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- * @private
- */
- jspb.BinaryWriter.prototype.writeRepeatedSignedVarint32_ =
- function(field, value) {
- if (value == null) return;
- for (var i = 0; i < value.length; i++) {
- this.writeSignedVarint32_(field, value[i]);
- }
- };
- /**
- * Writes an array of numbers to the buffer as a repeated varint field.
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- * @private
- */
- jspb.BinaryWriter.prototype.writeRepeatedVarint_ = function(field, value) {
- if (value == null) return;
- for (var i = 0; i < value.length; i++) {
- this.writeVarint_(field, value[i]);
- }
- };
- /**
- * Writes an array of numbers to the buffer as a repeated zigzag field.
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- * @private
- */
- jspb.BinaryWriter.prototype.writeRepeatedZigzag32_ = function(field, value) {
- if (value == null) return;
- for (var i = 0; i < value.length; i++) {
- this.writeZigzagVarint32_(field, value[i]);
- }
- };
- /**
- * Writes an array of numbers to the buffer as a repeated zigzag field.
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- * @private
- */
- jspb.BinaryWriter.prototype.writeRepeatedZigzag_ = function(field, value) {
- if (value == null) return;
- for (var i = 0; i < value.length; i++) {
- this.writeZigzagVarint_(field, value[i]);
- }
- };
- /**
- * Writes an array of numbers to the buffer as a repeated 32-bit int field.
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- */
- jspb.BinaryWriter.prototype.writeRepeatedInt32 =
- jspb.BinaryWriter.prototype.writeRepeatedSignedVarint32_;
- /**
- * Writes an array of numbers formatted as strings to the buffer as a repeated
- * 32-bit int field.
- * @param {number} field The field number.
- * @param {?Array.<string>} value The array of ints to write.
- */
- jspb.BinaryWriter.prototype.writeRepeatedInt32String =
- function(field, value) {
- if (value == null) return;
- for (var i = 0; i < value.length; i++) {
- this.writeInt32String(field, value[i]);
- }
- };
- /**
- * Writes an array of numbers to the buffer as a repeated 64-bit int field.
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- */
- jspb.BinaryWriter.prototype.writeRepeatedInt64 =
- jspb.BinaryWriter.prototype.writeRepeatedVarint_;
- /**
- * Writes an array of numbers formatted as strings to the buffer as a repeated
- * 64-bit int field.
- * @param {number} field The field number.
- * @param {?Array.<string>} value The array of ints to write.
- */
- jspb.BinaryWriter.prototype.writeRepeatedInt64String =
- function(field, value) {
- if (value == null) return;
- for (var i = 0; i < value.length; i++) {
- this.writeInt64String(field, value[i]);
- }
- };
- /**
- * Writes an array numbers to the buffer as a repeated unsigned 32-bit int
- * field.
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- */
- jspb.BinaryWriter.prototype.writeRepeatedUint32 =
- jspb.BinaryWriter.prototype.writeRepeatedUnsignedVarint32_;
- /**
- * Writes an array of numbers formatted as strings to the buffer as a repeated
- * unsigned 32-bit int field.
- * @param {number} field The field number.
- * @param {?Array.<string>} value The array of ints to write.
- */
- jspb.BinaryWriter.prototype.writeRepeatedUint32String =
- function(field, value) {
- if (value == null) return;
- for (var i = 0; i < value.length; i++) {
- this.writeUint32String(field, value[i]);
- }
- };
- /**
- * Writes an array numbers to the buffer as a repeated unsigned 64-bit int
- * field.
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- */
- jspb.BinaryWriter.prototype.writeRepeatedUint64 =
- jspb.BinaryWriter.prototype.writeRepeatedVarint_;
- /**
- * Writes an array of numbers formatted as strings to the buffer as a repeated
- * unsigned 64-bit int field.
- * @param {number} field The field number.
- * @param {?Array.<string>} value The array of ints to write.
- */
- jspb.BinaryWriter.prototype.writeRepeatedUint64String =
- function(field, value) {
- if (value == null) return;
- for (var i = 0; i < value.length; i++) {
- this.writeUint64String(field, value[i]);
- }
- };
- /**
- * Writes an array numbers to the buffer as a repeated signed 32-bit int field.
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- */
- jspb.BinaryWriter.prototype.writeRepeatedSint32 =
- jspb.BinaryWriter.prototype.writeRepeatedZigzag32_;
- /**
- * Writes an array numbers to the buffer as a repeated signed 64-bit int field.
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- */
- jspb.BinaryWriter.prototype.writeRepeatedSint64 =
- jspb.BinaryWriter.prototype.writeRepeatedZigzag_;
- /**
- * Writes an array of numbers to the buffer as a repeated fixed32 field. This
- * works for both signed and unsigned fixed32s.
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- */
- jspb.BinaryWriter.prototype.writeRepeatedFixed32 = function(field, value) {
- if (value == null) return;
- for (var i = 0; i < value.length; i++) {
- this.writeFixed32(field, value[i]);
- }
- };
- /**
- * Writes an array of numbers to the buffer as a repeated fixed64 field. This
- * works for both signed and unsigned fixed64s.
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- */
- jspb.BinaryWriter.prototype.writeRepeatedFixed64 = function(field, value) {
- if (value == null) return;
- for (var i = 0; i < value.length; i++) {
- this.writeFixed64(field, value[i]);
- }
- };
- /**
- * Writes an array of numbers to the buffer as a repeated sfixed32 field.
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- */
- jspb.BinaryWriter.prototype.writeRepeatedSfixed32 = function(field, value) {
- if (value == null) return;
- for (var i = 0; i < value.length; i++) {
- this.writeSfixed32(field, value[i]);
- }
- };
- /**
- * Writes an array of numbers to the buffer as a repeated sfixed64 field.
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- */
- jspb.BinaryWriter.prototype.writeRepeatedSfixed64 = function(field, value) {
- if (value == null) return;
- for (var i = 0; i < value.length; i++) {
- this.writeSfixed64(field, value[i]);
- }
- };
- /**
- * Writes an array of numbers to the buffer as a repeated float field.
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- */
- jspb.BinaryWriter.prototype.writeRepeatedFloat = function(field, value) {
- if (value == null) return;
- for (var i = 0; i < value.length; i++) {
- this.writeFloat(field, value[i]);
- }
- };
- /**
- * Writes an array of numbers to the buffer as a repeated double field.
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- */
- jspb.BinaryWriter.prototype.writeRepeatedDouble = function(field, value) {
- if (value == null) return;
- for (var i = 0; i < value.length; i++) {
- this.writeDouble(field, value[i]);
- }
- };
- /**
- * Writes an array of booleans to the buffer as a repeated bool field.
- * @param {number} field The field number.
- * @param {?Array.<boolean>} value The array of ints to write.
- */
- jspb.BinaryWriter.prototype.writeRepeatedBool = function(field, value) {
- if (value == null) return;
- for (var i = 0; i < value.length; i++) {
- this.writeBool(field, value[i]);
- }
- };
- /**
- * Writes an array of enums to the buffer as a repeated enum field.
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- */
- jspb.BinaryWriter.prototype.writeRepeatedEnum = function(field, value) {
- if (value == null) return;
- for (var i = 0; i < value.length; i++) {
- this.writeEnum(field, value[i]);
- }
- };
- /**
- * Writes an array of strings to the buffer as a repeated string field.
- * @param {number} field The field number.
- * @param {?Array.<string>} value The array of strings to write.
- */
- jspb.BinaryWriter.prototype.writeRepeatedString = function(field, value) {
- if (value == null) return;
- for (var i = 0; i < value.length; i++) {
- this.writeString(field, value[i]);
- }
- };
- /**
- * Writes an array of arbitrary byte fields to the buffer.
- *
- * If 'value' is null, this method will try and copy the pre-serialized value
- * in 'opt_buffer' if present.
- *
- * @param {number} field The field number.
- * @param {?Array.<!Uint8Array|string>} value
- * The arrays of arrays of bytes to write.
- * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
- * @param {?number=} opt_start The starting point in the above buffer.
- * @param {?number=} opt_end The ending point in the above buffer.
- * @param {boolean=} opt_stringIsRawBytes Any values that are strings are
- * interpreted as raw bytes rather than base64 data.
- */
- jspb.BinaryWriter.prototype.writeRepeatedBytes =
- function(field, value, opt_buffer, opt_start, opt_end,
- opt_stringIsRawBytes) {
- if (value != null) {
- for (var i = 0; i < value.length; i++) {
- this.writeBytes(field, value[i], null, null, null, opt_stringIsRawBytes);
- }
- } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
- this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
- }
- };
- /**
- * Writes an array of arbitrary byte fields to the buffer, with
- * `opt_stringIsRawBytes` implicitly true.
- * @param {number} field
- * @param {?Array.<string>} value
- */
- jspb.BinaryWriter.prototype.writeRepeatedBytesRawString =
- function(field, value) {
- this.writeRepeatedBytes(field, value, null, null, null, true);
- };
- /**
- * Writes an array of messages to the buffer.
- *
- * If 'value' is null, this method will try and copy the pre-serialized value
- * in 'opt_buffer' if present.
- *
- * @template MessageType
- * @param {number} field The field number.
- * @param {?Array.<!MessageType>} value The array of messages to
- * write.
- * @param {!jspb.WriterFunction} writerCallback Will be invoked with the value
- * to write and the writer to write it with.
- * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
- * @param {?number=} opt_start The starting point in the above buffer.
- * @param {?number=} opt_end The ending point in the above buffer.
- */
- jspb.BinaryWriter.prototype.writeRepeatedMessage =
- function(field, value, writerCallback, opt_buffer, opt_start, opt_end) {
- if (value) {
- for (var i = 0; i < value.length; i++) {
- var bookmark = this.beginDelimited_(field);
- writerCallback(value[i], this);
- this.endDelimited_(bookmark);
- }
- } else if (opt_buffer && (opt_start != null) && (opt_end != null)) {
- this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
- }
- };
- /**
- * Writes an array of group messages to the buffer.
- *
- * @template MessageType
- * @param {number} field The field number.
- * @param {?Array.<!MessageType>} value The array of messages to
- * write.
- * @param {!jspb.WriterFunction} writerCallback Will be invoked with the value
- * to write and the writer to write it with.
- */
- jspb.BinaryWriter.prototype.writeRepeatedGroup =
- function(field, value, writerCallback) {
- if (value) {
- for (var i = 0; i < value.length; i++) {
- this.rawWriteFieldHeader_(
- field, jspb.BinaryConstants.WireType.START_GROUP);
- writerCallback(value[i], this);
- this.rawWriteFieldHeader_(
- field, jspb.BinaryConstants.WireType.END_GROUP);
- }
- }
- };
- /**
- * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
- * the buffer.
- * @param {number} field The field number.
- * @param {?Array.<string>} value The array of hashes to write.
- */
- jspb.BinaryWriter.prototype.writeRepeatedFixedHash64 =
- function(field, value) {
- if (value == null) return;
- for (var i = 0; i < value.length; i++) {
- this.writeFixedHash64(field, value[i]);
- }
- };
- /**
- * Writes a repeated 64-bit hash string field (8 characters @ 8 bits of data
- * each) to the buffer.
- * @param {number} field The field number.
- * @param {?Array.<string>} value The array of hashes to write.
- */
- jspb.BinaryWriter.prototype.writeRepeatedVarintHash64 =
- function(field, value) {
- if (value == null) return;
- for (var i = 0; i < value.length; i++) {
- this.writeVarintHash64(field, value[i]);
- }
- };
- /**
- * Writes an array of numbers to the buffer as a packed varint field.
- *
- * If 'value' is null, this method will try and copy the pre-serialized value
- * in 'opt_buffer' if present.
- *
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
- * @param {?number=} opt_start The starting point in the above buffer.
- * @param {?number=} opt_end The ending point in the above buffer.
- * @private
- */
- jspb.BinaryWriter.prototype.writePackedUnsignedVarint32_ =
- function(field, value, opt_buffer, opt_start, opt_end) {
- if (value != null && value.length) {
- var bookmark = this.beginDelimited_(field);
- for (var i = 0; i < value.length; i++) {
- this.rawWriteUnsignedVarint32(value[i]);
- }
- this.endDelimited_(bookmark);
- } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
- this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
- }
- };
- /**
- * Writes an array of numbers to the buffer as a packed varint field.
- *
- * If 'value' is null, this method will try and copy the pre-serialized value
- * in 'opt_buffer' if present.
- *
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
- * @param {?number=} opt_start The starting point in the above buffer.
- * @param {?number=} opt_end The ending point in the above buffer.
- * @private
- */
- jspb.BinaryWriter.prototype.writePackedSignedVarint32_ =
- function(field, value, opt_buffer, opt_start, opt_end) {
- if (value != null && value.length) {
- var bookmark = this.beginDelimited_(field);
- for (var i = 0; i < value.length; i++) {
- this.rawWriteSignedVarint32(value[i]);
- }
- this.endDelimited_(bookmark);
- } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
- this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
- }
- };
- /**
- * Writes an array of numbers to the buffer as a packed varint field.
- *
- * If 'value' is null, this method will try and copy the pre-serialized value
- * in 'opt_buffer' if present.
- *
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
- * @param {?number=} opt_start The starting point in the above buffer.
- * @param {?number=} opt_end The ending point in the above buffer.
- * @private
- */
- jspb.BinaryWriter.prototype.writePackedVarint_ =
- function(field, value, opt_buffer, opt_start, opt_end) {
- if (value != null && value.length) {
- var bookmark = this.beginDelimited_(field);
- for (var i = 0; i < value.length; i++) {
- this.rawWriteVarint(value[i]);
- }
- this.endDelimited_(bookmark);
- } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
- this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
- }
- };
- /**
- * Writes an array of numbers to the buffer as a packed zigzag field.
- *
- * If 'value' is null, this method will try and copy the pre-serialized value
- * in 'opt_buffer' if present.
- *
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
- * @param {?number=} opt_start The starting point in the above buffer.
- * @param {?number=} opt_end The ending point in the above buffer.
- * @private
- */
- jspb.BinaryWriter.prototype.writePackedZigzag_ =
- function(field, value, opt_buffer, opt_start, opt_end) {
- if (value != null && value.length) {
- var bookmark = this.beginDelimited_(field);
- for (var i = 0; i < value.length; i++) {
- this.rawWriteZigzagVarint(value[i]);
- }
- this.endDelimited_(bookmark);
- } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
- this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
- }
- };
- /**
- * Writes an array of numbers to the buffer as a packed 32-bit int field.
- *
- * If 'value' is null, this method will try and copy the pre-serialized value
- * in 'opt_buffer' if present.
- *
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
- * @param {?number=} opt_start The starting point in the above buffer.
- * @param {?number=} opt_end The ending point in the above buffer.
- */
- jspb.BinaryWriter.prototype.writePackedInt32 =
- jspb.BinaryWriter.prototype.writePackedSignedVarint32_;
- /**
- * Writes an array of numbers represented as strings to the buffer as a packed
- * 32-bit int field.
- * @param {number} field
- * @param {?Array.<string>} value
- */
- jspb.BinaryWriter.prototype.writePackedInt32String = function(field, value) {
- if (value == null || !value.length) return;
- var bookmark = this.beginDelimited_(field);
- for (var i = 0; i < value.length; i++) {
- this.rawWriteSignedVarint32(parseInt(value[i], 10));
- }
- this.endDelimited_(bookmark);
- };
- /**
- * Writes an array of numbers to the buffer as a packed 64-bit int field.
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
- * @param {?number=} opt_start The starting point in the above buffer.
- * @param {?number=} opt_end The ending point in the above buffer.
- */
- jspb.BinaryWriter.prototype.writePackedInt64 =
- jspb.BinaryWriter.prototype.writePackedVarint_;
- /**
- * Writes an array of numbers represented as strings to the buffer as a packed
- * 64-bit int field.
- * @param {number} field
- * @param {?Array.<string>} value
- */
- jspb.BinaryWriter.prototype.writePackedInt64String =
- function(field, value) {
- if (value == null || !value.length) return;
- var bookmark = this.beginDelimited_(field);
- for (var i = 0; i < value.length; i++) {
- var num = jspb.arith.Int64.fromString(value[i]);
- this.rawWriteVarintFromNum(num);
- }
- this.endDelimited_(bookmark);
- };
- /**
- * Writes an array numbers to the buffer as a packed unsigned 32-bit int field.
- *
- * If 'value' is null, this method will try and copy the pre-serialized value
- * in 'opt_buffer' if present.
- *
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
- * @param {?number=} opt_start The starting point in the above buffer.
- * @param {?number=} opt_end The ending point in the above buffer.
- */
- jspb.BinaryWriter.prototype.writePackedUint32 =
- jspb.BinaryWriter.prototype.writePackedUnsignedVarint32_;
- /**
- * Writes an array of numbers represented as strings to the buffer as a packed
- * unsigned 32-bit int field.
- * @param {number} field
- * @param {?Array.<string>} value
- */
- jspb.BinaryWriter.prototype.writePackedUint32String =
- function(field, value) {
- if (value == null || !value.length) return;
- var bookmark = this.beginDelimited_(field);
- for (var i = 0; i < value.length; i++) {
- this.rawWriteUnsignedVarint32(parseInt(value[i], 10));
- }
- this.endDelimited_(bookmark);
- };
- /**
- * Writes an array numbers to the buffer as a packed unsigned 64-bit int field.
- *
- * If 'value' is null, this method will try and copy the pre-serialized value
- * in 'opt_buffer' if present.
- *
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
- * @param {?number=} opt_start The starting point in the above buffer.
- * @param {?number=} opt_end The ending point in the above buffer.
- */
- jspb.BinaryWriter.prototype.writePackedUint64 =
- jspb.BinaryWriter.prototype.writePackedVarint_;
- /**
- * Writes an array of numbers represented as strings to the buffer as a packed
- * unsigned 64-bit int field.
- * @param {number} field
- * @param {?Array.<string>} value
- */
- jspb.BinaryWriter.prototype.writePackedUint64String =
- function(field, value) {
- if (value == null || !value.length) return;
- var bookmark = this.beginDelimited_(field);
- for (var i = 0; i < value.length; i++) {
- var num = jspb.arith.UInt64.fromString(value[i]);
- this.rawWriteVarintFromNum(num);
- }
- this.endDelimited_(bookmark);
- };
- /**
- * Writes an array numbers to the buffer as a packed signed 32-bit int field.
- *
- * If 'value' is null, this method will try and copy the pre-serialized value
- * in 'opt_buffer' if present.
- *
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
- * @param {?number=} opt_start The starting point in the above buffer.
- * @param {?number=} opt_end The ending point in the above buffer.
- */
- jspb.BinaryWriter.prototype.writePackedSint32 =
- jspb.BinaryWriter.prototype.writePackedZigzag_;
- /**
- * Writes an array numbers to the buffer as a packed signed 64-bit int field.
- *
- * If 'value' is null, this method will try and copy the pre-serialized value
- * in 'opt_buffer' if present.
- *
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
- * @param {?number=} opt_start The starting point in the above buffer.
- * @param {?number=} opt_end The ending point in the above buffer.
- */
- jspb.BinaryWriter.prototype.writePackedSint64 =
- jspb.BinaryWriter.prototype.writePackedZigzag_;
- /**
- * Writes an array of numbers to the buffer as a packed fixed32 field.
- *
- * If 'value' is null, this method will try and copy the pre-serialized value
- * in 'opt_buffer' if present.
- *
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
- * @param {?number=} opt_start The starting point in the above buffer.
- * @param {?number=} opt_end The ending point in the above buffer.
- */
- jspb.BinaryWriter.prototype.writePackedFixed32 =
- function(field, value, opt_buffer, opt_start, opt_end) {
- if (value != null && value.length) {
- this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
- this.rawWriteUnsignedVarint32(value.length * 4);
- for (var i = 0; i < value.length; i++) {
- this.rawWriteUint32(value[i]);
- }
- } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
- this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
- }
- };
- /**
- * Writes an array of numbers to the buffer as a packed fixed64 field.
- *
- * If 'value' is null, this method will try and copy the pre-serialized value
- * in 'opt_buffer' if present.
- *
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
- * @param {?number=} opt_start The starting point in the above buffer.
- * @param {?number=} opt_end The ending point in the above buffer.
- */
- jspb.BinaryWriter.prototype.writePackedFixed64 =
- function(field, value, opt_buffer, opt_start, opt_end) {
- if (value != null && value.length) {
- this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
- this.rawWriteUnsignedVarint32(value.length * 8);
- for (var i = 0; i < value.length; i++) {
- this.rawWriteUint64(value[i]);
- }
- } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
- this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
- }
- };
- /**
- * Writes an array of numbers to the buffer as a packed sfixed32 field.
- *
- * If 'value' is null, this method will try and copy the pre-serialized value
- * in 'opt_buffer' if present.
- *
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
- * @param {?number=} opt_start The starting point in the above buffer.
- * @param {?number=} opt_end The ending point in the above buffer.
- */
- jspb.BinaryWriter.prototype.writePackedSfixed32 =
- function(field, value, opt_buffer, opt_start, opt_end) {
- if (value != null && value.length) {
- this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
- this.rawWriteUnsignedVarint32(value.length * 4);
- for (var i = 0; i < value.length; i++) {
- this.rawWriteInt32(value[i]);
- }
- } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
- this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
- }
- };
- /**
- * Writes an array of numbers to the buffer as a packed sfixed64 field.
- *
- * If 'value' is null, this method will try and copy the pre-serialized value
- * in 'opt_buffer' if present.
- *
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
- * @param {?number=} opt_start The starting point in the above buffer.
- * @param {?number=} opt_end The ending point in the above buffer.
- */
- jspb.BinaryWriter.prototype.writePackedSfixed64 =
- function(field, value, opt_buffer, opt_start, opt_end) {
- if (value != null) {
- this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
- this.rawWriteUnsignedVarint32(value.length * 8);
- for (var i = 0; i < value.length; i++) {
- this.rawWriteInt64(value[i]);
- }
- } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
- this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
- }
- };
- /**
- * Writes an array of numbers to the buffer as a packed float field.
- *
- * If 'value' is null, this method will try and copy the pre-serialized value
- * in 'opt_buffer' if present.
- *
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
- * @param {?number=} opt_start The starting point in the above buffer.
- * @param {?number=} opt_end The ending point in the above buffer.
- */
- jspb.BinaryWriter.prototype.writePackedFloat =
- function(field, value, opt_buffer, opt_start, opt_end) {
- if (value != null && value.length) {
- this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
- this.rawWriteUnsignedVarint32(value.length * 4);
- for (var i = 0; i < value.length; i++) {
- this.rawWriteFloat(value[i]);
- }
- } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
- this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
- }
- };
- /**
- * Writes an array of numbers to the buffer as a packed double field.
- *
- * If 'value' is null, this method will try and copy the pre-serialized value
- * in 'opt_buffer' if present.
- *
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
- * @param {?number=} opt_start The starting point in the above buffer.
- * @param {?number=} opt_end The ending point in the above buffer.
- */
- jspb.BinaryWriter.prototype.writePackedDouble =
- function(field, value, opt_buffer, opt_start, opt_end) {
- if (value != null && value.length) {
- this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
- this.rawWriteUnsignedVarint32(value.length * 8);
- for (var i = 0; i < value.length; i++) {
- this.rawWriteDouble(value[i]);
- }
- } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
- this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
- }
- };
- /**
- * Writes an array of booleans to the buffer as a packed bool field.
- *
- * If 'value' is null, this method will try and copy the pre-serialized value
- * in 'opt_buffer' if present.
- *
- * @param {number} field The field number.
- * @param {?Array.<boolean>} value The array of ints to write.
- * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
- * @param {?number=} opt_start The starting point in the above buffer.
- * @param {?number=} opt_end The ending point in the above buffer.
- */
- jspb.BinaryWriter.prototype.writePackedBool =
- function(field, value, opt_buffer, opt_start, opt_end) {
- if (value != null && value.length) {
- this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
- this.rawWriteUnsignedVarint32(value.length);
- for (var i = 0; i < value.length; i++) {
- this.rawWriteBool(value[i]);
- }
- } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
- this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
- }
- };
- /**
- * Writes an array of enums to the buffer as a packed enum field.
- *
- * If 'value' is null, this method will try and copy the pre-serialized value
- * in 'opt_buffer' if present.
- *
- * @param {number} field The field number.
- * @param {?Array.<number>} value The array of ints to write.
- * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
- * @param {?number=} opt_start The starting point in the above buffer.
- * @param {?number=} opt_end The ending point in the above buffer.
- */
- jspb.BinaryWriter.prototype.writePackedEnum =
- function(field, value, opt_buffer, opt_start, opt_end) {
- if (value != null && value.length) {
- var bookmark = this.beginDelimited_(field);
- for (var i = 0; i < value.length; i++) {
- this.rawWriteEnum(value[i]);
- }
- this.endDelimited_(bookmark);
- } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
- this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
- }
- };
- /**
- * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
- * the buffer.
- *
- * If 'value' is null, this method will try and copy the pre-serialized value
- * in 'opt_buffer' if present.
- *
- * @param {number} field The field number.
- * @param {?Array.<string>} value The array of hashes to write.
- * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
- * @param {?number=} opt_start The starting point in the above buffer.
- * @param {?number=} opt_end The ending point in the above buffer.
- */
- jspb.BinaryWriter.prototype.writePackedFixedHash64 =
- function(field, value, opt_buffer, opt_start, opt_end) {
- if (value != null && value.length) {
- this.rawWriteFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
- this.rawWriteUnsignedVarint32(value.length * 8);
- for (var i = 0; i < value.length; i++) {
- this.rawWriteFixedHash64(value[i]);
- }
- } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
- this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
- }
- };
- /**
- * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
- * the buffer.
- *
- * If 'value' is null, this method will try and copy the pre-serialized value
- * in 'opt_buffer' if present.
- *
- * @param {number} field The field number.
- * @param {?Array.<string>} value The array of hashes to write.
- * @param {?Uint8Array=} opt_buffer A buffer containing pre-packed values.
- * @param {?number=} opt_start The starting point in the above buffer.
- * @param {?number=} opt_end The ending point in the above buffer.
- */
- jspb.BinaryWriter.prototype.writePackedVarintHash64 =
- function(field, value, opt_buffer, opt_start, opt_end) {
- if (value != null && value.length) {
- var bookmark = this.beginDelimited_(field);
- for (var i = 0; i < value.length; i++) {
- this.rawWriteVarintHash64(value[i]);
- }
- this.endDelimited_(bookmark);
- } else if ((opt_buffer != null) && (opt_start != null) && (opt_end != null)) {
- this.rawWriteByteRange(opt_buffer, opt_start, opt_end);
- }
- };
|