writer.js 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833
  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. * @fileoverview This file contains utilities for encoding Javascript objects
  32. * into binary, wire-format protocol buffers (in the form of Uint8Arrays) that
  33. * a server can consume directly.
  34. *
  35. * jspb's BinaryWriter class defines methods for efficiently encoding
  36. * Javascript objects into binary, wire-format protocol buffers and supports
  37. * all the fundamental field types used in protocol buffers.
  38. *
  39. * Major caveat 1 - Users of this library _must_ keep their Javascript proto
  40. * parsing code in sync with the original .proto file - presumably you'll be
  41. * using the typed jspb code generator, but if you bypass that you'll need
  42. * to keep things in sync by hand.
  43. *
  44. * Major caveat 2 - Javascript is unable to accurately represent integers
  45. * larger than 2^53 due to its use of a double-precision floating point format
  46. * for all numbers. BinaryWriter does not make any special effort to preserve
  47. * precision for values above this limit - if you need to pass 64-bit integers
  48. * (hash codes, for example) between the client and server without precision
  49. * loss, do _not_ use this library.
  50. *
  51. * Major caveat 3 - This class uses typed arrays and must not be used on older
  52. * browsers that do not support them.
  53. *
  54. * @suppress {missingRequire} TODO(b/152540451): this shouldn't be needed
  55. * @author aappleby@google.com (Austin Appleby)
  56. */
  57. goog.provide('jspb.BinaryWriter');
  58. goog.require('goog.asserts');
  59. goog.require('goog.crypt.base64');
  60. goog.require('jspb.BinaryConstants');
  61. goog.require('jspb.BinaryEncoder');
  62. goog.require('jspb.arith.Int64');
  63. goog.require('jspb.arith.UInt64');
  64. goog.require('jspb.utils');
  65. /**
  66. * BinaryWriter implements encoders for all the wire types specified in
  67. * https://developers.google.com/protocol-buffers/docs/encoding.
  68. *
  69. * @constructor
  70. * @struct
  71. */
  72. jspb.BinaryWriter = function() {
  73. /**
  74. * Blocks of serialized data that will be concatenated once all messages have
  75. * been written.
  76. * @private {!Array<!Uint8Array|!Array<number>>}
  77. */
  78. this.blocks_ = [];
  79. /**
  80. * Total number of bytes in the blocks_ array. Does _not_ include bytes in
  81. * the encoder below.
  82. * @private {number}
  83. */
  84. this.totalLength_ = 0;
  85. /**
  86. * Binary encoder holding pieces of a message that we're still serializing.
  87. * When we get to a stopping point (either the start of a new submessage, or
  88. * when we need to append a raw Uint8Array), the encoder's buffer will be
  89. * added to the block array above and the encoder will be reset.
  90. * @private {!jspb.BinaryEncoder}
  91. */
  92. this.encoder_ = new jspb.BinaryEncoder();
  93. /**
  94. * A stack of bookmarks containing the parent blocks for each message started
  95. * via beginSubMessage(), needed as bookkeeping for endSubMessage().
  96. * TODO(aappleby): Deprecated, users should be calling writeMessage().
  97. * @private {!Array<!Array<number>>}
  98. */
  99. this.bookmarks_ = [];
  100. };
  101. /**
  102. * Append a typed array of bytes onto the buffer.
  103. *
  104. * @param {!Uint8Array} arr The byte array to append.
  105. * @private
  106. */
  107. jspb.BinaryWriter.prototype.appendUint8Array_ = function(arr) {
  108. var temp = this.encoder_.end();
  109. this.blocks_.push(temp);
  110. this.blocks_.push(arr);
  111. this.totalLength_ += temp.length + arr.length;
  112. };
  113. /**
  114. * Begins a new message by writing the field header and returning a bookmark
  115. * which we will use to patch in the message length to in endDelimited_ below.
  116. * @param {number} field
  117. * @return {!Array<number>}
  118. * @private
  119. */
  120. jspb.BinaryWriter.prototype.beginDelimited_ = function(field) {
  121. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  122. var bookmark = this.encoder_.end();
  123. this.blocks_.push(bookmark);
  124. this.totalLength_ += bookmark.length;
  125. bookmark.push(this.totalLength_);
  126. return bookmark;
  127. };
  128. /**
  129. * Ends a message by encoding the _change_ in length of the buffer to the
  130. * parent block and adds the number of bytes needed to encode that length to
  131. * the total byte length.
  132. * @param {!Array<number>} bookmark
  133. * @private
  134. */
  135. jspb.BinaryWriter.prototype.endDelimited_ = function(bookmark) {
  136. var oldLength = bookmark.pop();
  137. var messageLength = this.totalLength_ + this.encoder_.length() - oldLength;
  138. goog.asserts.assert(messageLength >= 0);
  139. while (messageLength > 127) {
  140. bookmark.push((messageLength & 0x7f) | 0x80);
  141. messageLength = messageLength >>> 7;
  142. this.totalLength_++;
  143. }
  144. bookmark.push(messageLength);
  145. this.totalLength_++;
  146. };
  147. /**
  148. * Writes a pre-serialized message to the buffer.
  149. * @param {!Uint8Array} bytes The array of bytes to write.
  150. * @param {number} start The start of the range to write.
  151. * @param {number} end The end of the range to write.
  152. */
  153. jspb.BinaryWriter.prototype.writeSerializedMessage = function(
  154. bytes, start, end) {
  155. this.appendUint8Array_(bytes.subarray(start, end));
  156. };
  157. /**
  158. * Writes a pre-serialized message to the buffer if the message and endpoints
  159. * are non-null.
  160. * @param {?Uint8Array} bytes The array of bytes to write.
  161. * @param {?number} start The start of the range to write.
  162. * @param {?number} end The end of the range to write.
  163. */
  164. jspb.BinaryWriter.prototype.maybeWriteSerializedMessage = function(
  165. bytes, start, end) {
  166. if (bytes != null && start != null && end != null) {
  167. this.writeSerializedMessage(bytes, start, end);
  168. }
  169. };
  170. /**
  171. * Resets the writer, throwing away any accumulated buffers.
  172. */
  173. jspb.BinaryWriter.prototype.reset = function() {
  174. this.blocks_ = [];
  175. this.encoder_.end();
  176. this.totalLength_ = 0;
  177. this.bookmarks_ = [];
  178. };
  179. /**
  180. * Converts the encoded data into a Uint8Array.
  181. * @return {!Uint8Array}
  182. */
  183. jspb.BinaryWriter.prototype.getResultBuffer = function() {
  184. goog.asserts.assert(this.bookmarks_.length == 0);
  185. var flat = new Uint8Array(this.totalLength_ + this.encoder_.length());
  186. var blocks = this.blocks_;
  187. var blockCount = blocks.length;
  188. var offset = 0;
  189. for (var i = 0; i < blockCount; i++) {
  190. var block = blocks[i];
  191. flat.set(block, offset);
  192. offset += block.length;
  193. }
  194. var tail = this.encoder_.end();
  195. flat.set(tail, offset);
  196. offset += tail.length;
  197. // Post condition: `flattened` must have had every byte written.
  198. goog.asserts.assert(offset == flat.length);
  199. // Replace our block list with the flattened block, which lets GC reclaim
  200. // the temp blocks sooner.
  201. this.blocks_ = [flat];
  202. return flat;
  203. };
  204. /**
  205. * Converts the encoded data into a base64-encoded string.
  206. * @param {!goog.crypt.base64.Alphabet=} alphabet Which flavor of base64 to use.
  207. * @return {string}
  208. */
  209. jspb.BinaryWriter.prototype.getResultBase64String = function(alphabet) {
  210. return goog.crypt.base64.encodeByteArray(this.getResultBuffer(), alphabet);
  211. };
  212. /**
  213. * Begins a new sub-message. The client must call endSubMessage() when they're
  214. * done.
  215. * TODO(aappleby): Deprecated. Move callers to writeMessage().
  216. * @param {number} field The field number of the sub-message.
  217. */
  218. jspb.BinaryWriter.prototype.beginSubMessage = function(field) {
  219. this.bookmarks_.push(this.beginDelimited_(field));
  220. };
  221. /**
  222. * Finishes a sub-message and packs it into the parent messages' buffer.
  223. * TODO(aappleby): Deprecated. Move callers to writeMessage().
  224. */
  225. jspb.BinaryWriter.prototype.endSubMessage = function() {
  226. goog.asserts.assert(this.bookmarks_.length >= 0);
  227. this.endDelimited_(this.bookmarks_.pop());
  228. };
  229. /**
  230. * Encodes a (field number, wire type) tuple into a wire-format field header
  231. * and stores it in the buffer as a varint.
  232. * @param {number} field The field number.
  233. * @param {number} wireType The wire-type of the field, as specified in the
  234. * protocol buffer documentation.
  235. * @private
  236. */
  237. jspb.BinaryWriter.prototype.writeFieldHeader_ =
  238. function(field, wireType) {
  239. goog.asserts.assert(field >= 1 && field == Math.floor(field));
  240. var x = field * 8 + wireType;
  241. this.encoder_.writeUnsignedVarint32(x);
  242. };
  243. /**
  244. * Writes a field of any valid scalar type to the binary stream.
  245. * @param {jspb.BinaryConstants.FieldType} fieldType
  246. * @param {number} field
  247. * @param {jspb.AnyFieldType} value
  248. */
  249. jspb.BinaryWriter.prototype.writeAny = function(fieldType, field, value) {
  250. var fieldTypes = jspb.BinaryConstants.FieldType;
  251. switch (fieldType) {
  252. case fieldTypes.DOUBLE:
  253. this.writeDouble(field, /** @type {number} */(value));
  254. return;
  255. case fieldTypes.FLOAT:
  256. this.writeFloat(field, /** @type {number} */(value));
  257. return;
  258. case fieldTypes.INT64:
  259. this.writeInt64(field, /** @type {number} */(value));
  260. return;
  261. case fieldTypes.UINT64:
  262. this.writeUint64(field, /** @type {number} */(value));
  263. return;
  264. case fieldTypes.INT32:
  265. this.writeInt32(field, /** @type {number} */(value));
  266. return;
  267. case fieldTypes.FIXED64:
  268. this.writeFixed64(field, /** @type {number} */(value));
  269. return;
  270. case fieldTypes.FIXED32:
  271. this.writeFixed32(field, /** @type {number} */(value));
  272. return;
  273. case fieldTypes.BOOL:
  274. this.writeBool(field, /** @type {boolean} */(value));
  275. return;
  276. case fieldTypes.STRING:
  277. this.writeString(field, /** @type {string} */(value));
  278. return;
  279. case fieldTypes.GROUP:
  280. goog.asserts.fail('Group field type not supported in writeAny()');
  281. return;
  282. case fieldTypes.MESSAGE:
  283. goog.asserts.fail('Message field type not supported in writeAny()');
  284. return;
  285. case fieldTypes.BYTES:
  286. this.writeBytes(field, /** @type {?Uint8Array} */(value));
  287. return;
  288. case fieldTypes.UINT32:
  289. this.writeUint32(field, /** @type {number} */(value));
  290. return;
  291. case fieldTypes.ENUM:
  292. this.writeEnum(field, /** @type {number} */(value));
  293. return;
  294. case fieldTypes.SFIXED32:
  295. this.writeSfixed32(field, /** @type {number} */(value));
  296. return;
  297. case fieldTypes.SFIXED64:
  298. this.writeSfixed64(field, /** @type {number} */(value));
  299. return;
  300. case fieldTypes.SINT32:
  301. this.writeSint32(field, /** @type {number} */(value));
  302. return;
  303. case fieldTypes.SINT64:
  304. this.writeSint64(field, /** @type {number} */(value));
  305. return;
  306. case fieldTypes.FHASH64:
  307. this.writeFixedHash64(field, /** @type {string} */(value));
  308. return;
  309. case fieldTypes.VHASH64:
  310. this.writeVarintHash64(field, /** @type {string} */(value));
  311. return;
  312. default:
  313. goog.asserts.fail('Invalid field type in writeAny()');
  314. return;
  315. }
  316. };
  317. /**
  318. * Writes a varint field to the buffer without range checking.
  319. * @param {number} field The field number.
  320. * @param {number?} value The value to write.
  321. * @private
  322. */
  323. jspb.BinaryWriter.prototype.writeUnsignedVarint32_ = function(field, value) {
  324. if (value == null) return;
  325. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  326. this.encoder_.writeUnsignedVarint32(value);
  327. };
  328. /**
  329. * Writes a varint field to the buffer without range checking.
  330. * @param {number} field The field number.
  331. * @param {number?} value The value to write.
  332. * @private
  333. */
  334. jspb.BinaryWriter.prototype.writeSignedVarint32_ = function(field, value) {
  335. if (value == null) return;
  336. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  337. this.encoder_.writeSignedVarint32(value);
  338. };
  339. /**
  340. * Writes a varint field to the buffer without range checking.
  341. * @param {number} field The field number.
  342. * @param {number?} value The value to write.
  343. * @private
  344. */
  345. jspb.BinaryWriter.prototype.writeUnsignedVarint64_ = function(field, value) {
  346. if (value == null) return;
  347. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  348. this.encoder_.writeUnsignedVarint64(value);
  349. };
  350. /**
  351. * Writes a varint field to the buffer without range checking.
  352. * @param {number} field The field number.
  353. * @param {number?} value The value to write.
  354. * @private
  355. */
  356. jspb.BinaryWriter.prototype.writeSignedVarint64_ = function(field, value) {
  357. if (value == null) return;
  358. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  359. this.encoder_.writeSignedVarint64(value);
  360. };
  361. /**
  362. * Writes a zigzag varint field to the buffer without range checking.
  363. * @param {number} field The field number.
  364. * @param {number?} value The value to write.
  365. * @private
  366. */
  367. jspb.BinaryWriter.prototype.writeZigzagVarint32_ = function(field, value) {
  368. if (value == null) return;
  369. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  370. this.encoder_.writeZigzagVarint32(value);
  371. };
  372. /**
  373. * Writes a zigzag varint field to the buffer without range checking.
  374. * @param {number} field The field number.
  375. * @param {number?} value The value to write.
  376. * @private
  377. */
  378. jspb.BinaryWriter.prototype.writeZigzagVarint64_ = function(field, value) {
  379. if (value == null) return;
  380. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  381. this.encoder_.writeZigzagVarint64(value);
  382. };
  383. /**
  384. * Writes a zigzag varint field to the buffer without range checking.
  385. * @param {number} field The field number.
  386. * @param {string?} value The value to write.
  387. * @private
  388. */
  389. jspb.BinaryWriter.prototype.writeZigzagVarint64String_ = function(
  390. field, value) {
  391. if (value == null) return;
  392. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  393. this.encoder_.writeZigzagVarint64String(value);
  394. };
  395. /**
  396. * Writes a zigzag varint field to the buffer without range checking.
  397. * @param {number} field The field number.
  398. * @param {string?} value The value to write.
  399. * @private
  400. */
  401. jspb.BinaryWriter.prototype.writeZigzagVarintHash64_ = function(field, value) {
  402. if (value == null) return;
  403. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  404. this.encoder_.writeZigzagVarintHash64(value);
  405. };
  406. /**
  407. * Writes an int32 field to the buffer. Numbers outside the range [-2^31,2^31)
  408. * will be truncated.
  409. * @param {number} field The field number.
  410. * @param {number?} value The value to write.
  411. */
  412. jspb.BinaryWriter.prototype.writeInt32 = function(field, value) {
  413. if (value == null) return;
  414. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
  415. (value < jspb.BinaryConstants.TWO_TO_31));
  416. this.writeSignedVarint32_(field, value);
  417. };
  418. /**
  419. * Writes an int32 field represented as a string to the buffer. Numbers outside
  420. * the range [-2^31,2^31) will be truncated.
  421. * @param {number} field The field number.
  422. * @param {string?} value The value to write.
  423. */
  424. jspb.BinaryWriter.prototype.writeInt32String = function(field, value) {
  425. if (value == null) return;
  426. var intValue = /** {number} */ parseInt(value, 10);
  427. goog.asserts.assert((intValue >= -jspb.BinaryConstants.TWO_TO_31) &&
  428. (intValue < jspb.BinaryConstants.TWO_TO_31));
  429. this.writeSignedVarint32_(field, intValue);
  430. };
  431. /**
  432. * Writes an int64 field to the buffer. Numbers outside the range [-2^63,2^63)
  433. * will be truncated.
  434. * @param {number} field The field number.
  435. * @param {number?} value The value to write.
  436. */
  437. jspb.BinaryWriter.prototype.writeInt64 = function(field, value) {
  438. if (value == null) return;
  439. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
  440. (value < jspb.BinaryConstants.TWO_TO_63));
  441. this.writeSignedVarint64_(field, value);
  442. };
  443. /**
  444. * Writes a int64 field (with value as a string) to the buffer.
  445. * @param {number} field The field number.
  446. * @param {string?} value The value to write.
  447. */
  448. jspb.BinaryWriter.prototype.writeInt64String = function(field, value) {
  449. if (value == null) return;
  450. var num = jspb.arith.Int64.fromString(value);
  451. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  452. this.encoder_.writeSplitVarint64(num.lo, num.hi);
  453. };
  454. /**
  455. * Writes a uint32 field to the buffer. Numbers outside the range [0,2^32)
  456. * will be truncated.
  457. * @param {number} field The field number.
  458. * @param {number?} value The value to write.
  459. */
  460. jspb.BinaryWriter.prototype.writeUint32 = function(field, value) {
  461. if (value == null) return;
  462. goog.asserts.assert((value >= 0) &&
  463. (value < jspb.BinaryConstants.TWO_TO_32));
  464. this.writeUnsignedVarint32_(field, value);
  465. };
  466. /**
  467. * Writes a uint32 field represented as a string to the buffer. Numbers outside
  468. * the range [0,2^32) will be truncated.
  469. * @param {number} field The field number.
  470. * @param {string?} value The value to write.
  471. */
  472. jspb.BinaryWriter.prototype.writeUint32String = function(field, value) {
  473. if (value == null) return;
  474. var intValue = /** {number} */ parseInt(value, 10);
  475. goog.asserts.assert((intValue >= 0) &&
  476. (intValue < jspb.BinaryConstants.TWO_TO_32));
  477. this.writeUnsignedVarint32_(field, intValue);
  478. };
  479. /**
  480. * Writes a uint64 field to the buffer. Numbers outside the range [0,2^64)
  481. * will be truncated.
  482. * @param {number} field The field number.
  483. * @param {number?} value The value to write.
  484. */
  485. jspb.BinaryWriter.prototype.writeUint64 = function(field, value) {
  486. if (value == null) return;
  487. goog.asserts.assert((value >= 0) &&
  488. (value < jspb.BinaryConstants.TWO_TO_64));
  489. this.writeUnsignedVarint64_(field, value);
  490. };
  491. /**
  492. * Writes a uint64 field (with value as a string) to the buffer.
  493. * @param {number} field The field number.
  494. * @param {string?} value The value to write.
  495. */
  496. jspb.BinaryWriter.prototype.writeUint64String = function(field, value) {
  497. if (value == null) return;
  498. var num = jspb.arith.UInt64.fromString(value);
  499. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  500. this.encoder_.writeSplitVarint64(num.lo, num.hi);
  501. };
  502. /**
  503. * Writes an sint32 field to the buffer. Numbers outside the range [-2^31,2^31)
  504. * will be truncated.
  505. * @param {number} field The field number.
  506. * @param {number?} value The value to write.
  507. */
  508. jspb.BinaryWriter.prototype.writeSint32 = function(field, value) {
  509. if (value == null) return;
  510. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
  511. (value < jspb.BinaryConstants.TWO_TO_31));
  512. this.writeZigzagVarint32_(field, value);
  513. };
  514. /**
  515. * Writes an sint64 field to the buffer. Numbers outside the range [-2^63,2^63)
  516. * will be truncated.
  517. * @param {number} field The field number.
  518. * @param {number?} value The value to write.
  519. */
  520. jspb.BinaryWriter.prototype.writeSint64 = function(field, value) {
  521. if (value == null) return;
  522. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
  523. (value < jspb.BinaryConstants.TWO_TO_63));
  524. this.writeZigzagVarint64_(field, value);
  525. };
  526. /**
  527. * Writes an sint64 field to the buffer from a hash64 encoded value. Numbers
  528. * outside the range [-2^63,2^63) will be truncated.
  529. * @param {number} field The field number.
  530. * @param {string?} value The hash64 string to write.
  531. */
  532. jspb.BinaryWriter.prototype.writeSintHash64 = function(field, value) {
  533. if (value == null) return;
  534. this.writeZigzagVarintHash64_(field, value);
  535. };
  536. /**
  537. * Writes an sint64 field to the buffer. Numbers outside the range [-2^63,2^63)
  538. * will be truncated.
  539. * @param {number} field The field number.
  540. * @param {string?} value The decimal string to write.
  541. */
  542. jspb.BinaryWriter.prototype.writeSint64String = function(field, value) {
  543. if (value == null) return;
  544. this.writeZigzagVarint64String_(field, value);
  545. };
  546. /**
  547. * Writes a fixed32 field to the buffer. Numbers outside the range [0,2^32)
  548. * will be truncated.
  549. * @param {number} field The field number.
  550. * @param {number?} value The value to write.
  551. */
  552. jspb.BinaryWriter.prototype.writeFixed32 = function(field, value) {
  553. if (value == null) return;
  554. goog.asserts.assert((value >= 0) &&
  555. (value < jspb.BinaryConstants.TWO_TO_32));
  556. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32);
  557. this.encoder_.writeUint32(value);
  558. };
  559. /**
  560. * Writes a fixed64 field to the buffer. Numbers outside the range [0,2^64)
  561. * will be truncated.
  562. * @param {number} field The field number.
  563. * @param {number?} value The value to write.
  564. */
  565. jspb.BinaryWriter.prototype.writeFixed64 = function(field, value) {
  566. if (value == null) return;
  567. goog.asserts.assert((value >= 0) &&
  568. (value < jspb.BinaryConstants.TWO_TO_64));
  569. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  570. this.encoder_.writeUint64(value);
  571. };
  572. /**
  573. * Writes a fixed64 field (with value as a string) to the buffer.
  574. * @param {number} field The field number.
  575. * @param {string?} value The value to write.
  576. */
  577. jspb.BinaryWriter.prototype.writeFixed64String = function(field, value) {
  578. if (value == null) return;
  579. var num = jspb.arith.UInt64.fromString(value);
  580. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  581. this.encoder_.writeSplitFixed64(num.lo, num.hi);
  582. };
  583. /**
  584. * Writes a sfixed32 field to the buffer. Numbers outside the range
  585. * [-2^31,2^31) will be truncated.
  586. * @param {number} field The field number.
  587. * @param {number?} value The value to write.
  588. */
  589. jspb.BinaryWriter.prototype.writeSfixed32 = function(field, value) {
  590. if (value == null) return;
  591. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
  592. (value < jspb.BinaryConstants.TWO_TO_31));
  593. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32);
  594. this.encoder_.writeInt32(value);
  595. };
  596. /**
  597. * Writes a sfixed64 field to the buffer. Numbers outside the range
  598. * [-2^63,2^63) will be truncated.
  599. * @param {number} field The field number.
  600. * @param {number?} value The value to write.
  601. */
  602. jspb.BinaryWriter.prototype.writeSfixed64 = function(field, value) {
  603. if (value == null) return;
  604. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
  605. (value < jspb.BinaryConstants.TWO_TO_63));
  606. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  607. this.encoder_.writeInt64(value);
  608. };
  609. /**
  610. * Writes a sfixed64 string field to the buffer. Numbers outside the range
  611. * [-2^63,2^63) will be truncated.
  612. * @param {number} field The field number.
  613. * @param {string?} value The value to write.
  614. */
  615. jspb.BinaryWriter.prototype.writeSfixed64String = function(field, value) {
  616. if (value == null) return;
  617. var num = jspb.arith.Int64.fromString(value);
  618. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  619. this.encoder_.writeSplitFixed64(num.lo, num.hi);
  620. };
  621. /**
  622. * Writes a single-precision floating point field to the buffer. Numbers
  623. * requiring more than 32 bits of precision will be truncated.
  624. * @param {number} field The field number.
  625. * @param {number?} value The value to write.
  626. */
  627. jspb.BinaryWriter.prototype.writeFloat = function(field, value) {
  628. if (value == null) return;
  629. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32);
  630. this.encoder_.writeFloat(value);
  631. };
  632. /**
  633. * Writes a double-precision floating point field to the buffer. As this is the
  634. * native format used by JavaScript, no precision will be lost.
  635. * @param {number} field The field number.
  636. * @param {number?} value The value to write.
  637. */
  638. jspb.BinaryWriter.prototype.writeDouble = function(field, value) {
  639. if (value == null) return;
  640. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  641. this.encoder_.writeDouble(value);
  642. };
  643. /**
  644. * Writes a boolean field to the buffer. We allow numbers as input
  645. * because the JSPB code generator uses 0/1 instead of true/false to save space
  646. * in the string representation of the proto.
  647. * @param {number} field The field number.
  648. * @param {boolean?|number?} value The value to write.
  649. */
  650. jspb.BinaryWriter.prototype.writeBool = function(field, value) {
  651. if (value == null) return;
  652. goog.asserts.assert(typeof value === 'boolean' || typeof value === 'number');
  653. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  654. this.encoder_.writeBool(value);
  655. };
  656. /**
  657. * Writes an enum field to the buffer.
  658. * @param {number} field The field number.
  659. * @param {number?} value The value to write.
  660. */
  661. jspb.BinaryWriter.prototype.writeEnum = function(field, value) {
  662. if (value == null) return;
  663. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
  664. (value < jspb.BinaryConstants.TWO_TO_31));
  665. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  666. this.encoder_.writeSignedVarint32(value);
  667. };
  668. /**
  669. * Writes a string field to the buffer.
  670. * @param {number} field The field number.
  671. * @param {string?} value The string to write.
  672. */
  673. jspb.BinaryWriter.prototype.writeString = function(field, value) {
  674. if (value == null) return;
  675. var bookmark = this.beginDelimited_(field);
  676. this.encoder_.writeString(value);
  677. this.endDelimited_(bookmark);
  678. };
  679. /**
  680. * Writes an arbitrary byte field to the buffer. Note - to match the behavior
  681. * of the C++ implementation, empty byte arrays _are_ serialized.
  682. * @param {number} field The field number.
  683. * @param {?jspb.ByteSource} value The array of bytes to write.
  684. */
  685. jspb.BinaryWriter.prototype.writeBytes = function(field, value) {
  686. if (value == null) return;
  687. var bytes = jspb.utils.byteSourceToUint8Array(value);
  688. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  689. this.encoder_.writeUnsignedVarint32(bytes.length);
  690. this.appendUint8Array_(bytes);
  691. };
  692. /**
  693. * Writes a message to the buffer.
  694. * @param {number} field The field number.
  695. * @param {?MessageType} value The message to write.
  696. * @param {function(MessageTypeNonNull, !jspb.BinaryWriter)} writerCallback
  697. * Will be invoked with the value to write and the writer to write it with.
  698. * @template MessageType
  699. * Use go/closure-ttl to declare a non-nullable version of MessageType. Replace
  700. * the null in blah|null with none. This is necessary because the compiler will
  701. * infer MessageType to be nullable if the value parameter is nullable.
  702. * @template MessageTypeNonNull :=
  703. * cond(isUnknown(MessageType), unknown(),
  704. * mapunion(MessageType, (X) =>
  705. * cond(eq(X, 'null'), none(), X)))
  706. * =:
  707. */
  708. jspb.BinaryWriter.prototype.writeMessage = function(
  709. field, value, writerCallback) {
  710. if (value == null) return;
  711. var bookmark = this.beginDelimited_(field);
  712. writerCallback(value, this);
  713. this.endDelimited_(bookmark);
  714. };
  715. /**
  716. * Writes a message set extension to the buffer.
  717. * @param {number} field The field number for the extension.
  718. * @param {?MessageType} value The extension message object to write. Note that
  719. * message set can only have extensions with type of optional message.
  720. * @param {function(!MessageTypeNonNull, !jspb.BinaryWriter)} writerCallback
  721. * Will be invoked with the value to write and the writer to write it with.
  722. * @template MessageType
  723. * Use go/closure-ttl to declare a non-nullable version of MessageType. Replace
  724. * the null in blah|null with none. This is necessary because the compiler will
  725. * infer MessageType to be nullable if the value parameter is nullable.
  726. * @template MessageTypeNonNull :=
  727. * cond(isUnknown(MessageType), unknown(),
  728. * mapunion(MessageType, (X) =>
  729. * cond(eq(X, 'null'), none(), X)))
  730. * =:
  731. */
  732. jspb.BinaryWriter.prototype.writeMessageSet = function(
  733. field, value, writerCallback) {
  734. if (value == null) return;
  735. // The wire format for a message set is defined by
  736. // google3/net/proto/message_set.proto
  737. this.writeFieldHeader_(1, jspb.BinaryConstants.WireType.START_GROUP);
  738. this.writeFieldHeader_(2, jspb.BinaryConstants.WireType.VARINT);
  739. this.encoder_.writeSignedVarint32(field);
  740. var bookmark = this.beginDelimited_(3);
  741. writerCallback(value, this);
  742. this.endDelimited_(bookmark);
  743. this.writeFieldHeader_(1, jspb.BinaryConstants.WireType.END_GROUP);
  744. };
  745. /**
  746. * Writes a group message to the buffer.
  747. *
  748. * @param {number} field The field number.
  749. * @param {?MessageType} value The message to write, wrapped with START_GROUP /
  750. * END_GROUP tags. Will be a no-op if 'value' is null.
  751. * @param {function(MessageTypeNonNull, !jspb.BinaryWriter)} writerCallback
  752. * Will be invoked with the value to write and the writer to write it with.
  753. * @template MessageType
  754. * Use go/closure-ttl to declare a non-nullable version of MessageType. Replace
  755. * the null in blah|null with none. This is necessary because the compiler will
  756. * infer MessageType to be nullable if the value parameter is nullable.
  757. * @template MessageTypeNonNull :=
  758. * cond(isUnknown(MessageType), unknown(),
  759. * mapunion(MessageType, (X) =>
  760. * cond(eq(X, 'null'), none(), X)))
  761. * =:
  762. */
  763. jspb.BinaryWriter.prototype.writeGroup = function(
  764. field, value, writerCallback) {
  765. if (value == null) return;
  766. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.START_GROUP);
  767. writerCallback(value, this);
  768. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.END_GROUP);
  769. };
  770. /**
  771. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  772. * the buffer.
  773. * @param {number} field The field number.
  774. * @param {string?} value The hash string.
  775. */
  776. jspb.BinaryWriter.prototype.writeFixedHash64 = function(field, value) {
  777. if (value == null) return;
  778. goog.asserts.assert(value.length == 8);
  779. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  780. this.encoder_.writeFixedHash64(value);
  781. };
  782. /**
  783. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  784. * the buffer.
  785. * @param {number} field The field number.
  786. * @param {string?} value The hash string.
  787. */
  788. jspb.BinaryWriter.prototype.writeVarintHash64 = function(field, value) {
  789. if (value == null) return;
  790. goog.asserts.assert(value.length == 8);
  791. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  792. this.encoder_.writeVarintHash64(value);
  793. };
  794. /**
  795. * Writes a 64-bit field to the buffer as a fixed64.
  796. * @param {number} field The field number.
  797. * @param {number} lowBits The low 32 bits.
  798. * @param {number} highBits The high 32 bits.
  799. */
  800. jspb.BinaryWriter.prototype.writeSplitFixed64 = function(
  801. field, lowBits, highBits) {
  802. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  803. this.encoder_.writeSplitFixed64(lowBits, highBits);
  804. };
  805. /**
  806. * Writes a 64-bit field to the buffer as a varint.
  807. * @param {number} field The field number.
  808. * @param {number} lowBits The low 32 bits.
  809. * @param {number} highBits The high 32 bits.
  810. */
  811. jspb.BinaryWriter.prototype.writeSplitVarint64 = function(
  812. field, lowBits, highBits) {
  813. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  814. this.encoder_.writeSplitVarint64(lowBits, highBits);
  815. };
  816. /**
  817. * Writes a 64-bit field to the buffer as a zigzag encoded varint.
  818. * @param {number} field The field number.
  819. * @param {number} lowBits The low 32 bits.
  820. * @param {number} highBits The high 32 bits.
  821. */
  822. jspb.BinaryWriter.prototype.writeSplitZigzagVarint64 = function(
  823. field, lowBits, highBits) {
  824. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  825. var encoder = this.encoder_;
  826. jspb.utils.toZigzag64(lowBits, highBits, function(lowBits, highBits) {
  827. encoder.writeSplitVarint64(lowBits >>> 0, highBits >>> 0);
  828. });
  829. };
  830. /**
  831. * Writes an array of numbers to the buffer as a repeated 32-bit int field.
  832. * @param {number} field The field number.
  833. * @param {?Array<number>} value The array of ints to write.
  834. */
  835. jspb.BinaryWriter.prototype.writeRepeatedInt32 = function(field, value) {
  836. if (value == null) return;
  837. for (var i = 0; i < value.length; i++) {
  838. this.writeSignedVarint32_(field, value[i]);
  839. }
  840. };
  841. /**
  842. * Writes an array of numbers formatted as strings to the buffer as a repeated
  843. * 32-bit int field.
  844. * @param {number} field The field number.
  845. * @param {?Array<string>} value The array of ints to write.
  846. */
  847. jspb.BinaryWriter.prototype.writeRepeatedInt32String = function(field, value) {
  848. if (value == null) return;
  849. for (var i = 0; i < value.length; i++) {
  850. this.writeInt32String(field, value[i]);
  851. }
  852. };
  853. /**
  854. * Writes an array of numbers to the buffer as a repeated 64-bit int field.
  855. * @param {number} field The field number.
  856. * @param {?Array<number>} value The array of ints to write.
  857. */
  858. jspb.BinaryWriter.prototype.writeRepeatedInt64 = function(field, value) {
  859. if (value == null) return;
  860. for (var i = 0; i < value.length; i++) {
  861. this.writeSignedVarint64_(field, value[i]);
  862. }
  863. };
  864. /**
  865. * Writes an array of 64-bit values to the buffer as a fixed64.
  866. * @param {number} field The field number.
  867. * @param {?Array<T>} value The value.
  868. * @param {function(T): number} lo Function to get low bits.
  869. * @param {function(T): number} hi Function to get high bits.
  870. * @template T
  871. */
  872. jspb.BinaryWriter.prototype.writeRepeatedSplitFixed64 = function(
  873. field, value, lo, hi) {
  874. if (value == null) return;
  875. for (var i = 0; i < value.length; i++) {
  876. this.writeSplitFixed64(field, lo(value[i]), hi(value[i]));
  877. }
  878. };
  879. /**
  880. * Writes an array of 64-bit values to the buffer as a varint.
  881. * @param {number} field The field number.
  882. * @param {?Array<T>} value The value.
  883. * @param {function(T): number} lo Function to get low bits.
  884. * @param {function(T): number} hi Function to get high bits.
  885. * @template T
  886. */
  887. jspb.BinaryWriter.prototype.writeRepeatedSplitVarint64 = function(
  888. field, value, lo, hi) {
  889. if (value == null) return;
  890. for (var i = 0; i < value.length; i++) {
  891. this.writeSplitVarint64(field, lo(value[i]), hi(value[i]));
  892. }
  893. };
  894. /**
  895. * Writes an array of 64-bit values to the buffer as a zigzag varint.
  896. * @param {number} field The field number.
  897. * @param {?Array<T>} value The value.
  898. * @param {function(T): number} lo Function to get low bits.
  899. * @param {function(T): number} hi Function to get high bits.
  900. * @template T
  901. */
  902. jspb.BinaryWriter.prototype.writeRepeatedSplitZigzagVarint64 = function(
  903. field, value, lo, hi) {
  904. if (value == null) return;
  905. for (var i = 0; i < value.length; i++) {
  906. this.writeSplitZigzagVarint64(field, lo(value[i]), hi(value[i]));
  907. }
  908. };
  909. /**
  910. * Writes an array of numbers formatted as strings to the buffer as a repeated
  911. * 64-bit int field.
  912. * @param {number} field The field number.
  913. * @param {?Array<string>} value The array of ints to write.
  914. */
  915. jspb.BinaryWriter.prototype.writeRepeatedInt64String = function(field, value) {
  916. if (value == null) return;
  917. for (var i = 0; i < value.length; i++) {
  918. this.writeInt64String(field, value[i]);
  919. }
  920. };
  921. /**
  922. * Writes an array numbers to the buffer as a repeated unsigned 32-bit int
  923. * field.
  924. * @param {number} field The field number.
  925. * @param {?Array<number>} value The array of ints to write.
  926. */
  927. jspb.BinaryWriter.prototype.writeRepeatedUint32 = function(field, value) {
  928. if (value == null) return;
  929. for (var i = 0; i < value.length; i++) {
  930. this.writeUnsignedVarint32_(field, value[i]);
  931. }
  932. };
  933. /**
  934. * Writes an array of numbers formatted as strings to the buffer as a repeated
  935. * unsigned 32-bit int field.
  936. * @param {number} field The field number.
  937. * @param {?Array<string>} value The array of ints to write.
  938. */
  939. jspb.BinaryWriter.prototype.writeRepeatedUint32String = function(field, value) {
  940. if (value == null) return;
  941. for (var i = 0; i < value.length; i++) {
  942. this.writeUint32String(field, value[i]);
  943. }
  944. };
  945. /**
  946. * Writes an array numbers to the buffer as a repeated unsigned 64-bit int
  947. * field.
  948. * @param {number} field The field number.
  949. * @param {?Array<number>} value The array of ints to write.
  950. */
  951. jspb.BinaryWriter.prototype.writeRepeatedUint64 = function(field, value) {
  952. if (value == null) return;
  953. for (var i = 0; i < value.length; i++) {
  954. this.writeUnsignedVarint64_(field, value[i]);
  955. }
  956. };
  957. /**
  958. * Writes an array of numbers formatted as strings to the buffer as a repeated
  959. * unsigned 64-bit int field.
  960. * @param {number} field The field number.
  961. * @param {?Array<string>} value The array of ints to write.
  962. */
  963. jspb.BinaryWriter.prototype.writeRepeatedUint64String = function(field, value) {
  964. if (value == null) return;
  965. for (var i = 0; i < value.length; i++) {
  966. this.writeUint64String(field, value[i]);
  967. }
  968. };
  969. /**
  970. * Writes an array numbers to the buffer as a repeated signed 32-bit int field.
  971. * @param {number} field The field number.
  972. * @param {?Array<number>} value The array of ints to write.
  973. */
  974. jspb.BinaryWriter.prototype.writeRepeatedSint32 = function(field, value) {
  975. if (value == null) return;
  976. for (var i = 0; i < value.length; i++) {
  977. this.writeZigzagVarint32_(field, value[i]);
  978. }
  979. };
  980. /**
  981. * Writes an array numbers to the buffer as a repeated signed 64-bit int field.
  982. * @param {number} field The field number.
  983. * @param {?Array<number>} value The array of ints to write.
  984. */
  985. jspb.BinaryWriter.prototype.writeRepeatedSint64 = function(field, value) {
  986. if (value == null) return;
  987. for (var i = 0; i < value.length; i++) {
  988. this.writeZigzagVarint64_(field, value[i]);
  989. }
  990. };
  991. /**
  992. * Writes an array numbers to the buffer as a repeated signed 64-bit int field.
  993. * @param {number} field The field number.
  994. * @param {?Array<string>} value The array of ints to write.
  995. */
  996. jspb.BinaryWriter.prototype.writeRepeatedSint64String = function(field, value) {
  997. if (value == null) return;
  998. for (var i = 0; i < value.length; i++) {
  999. this.writeZigzagVarint64String_(field, value[i]);
  1000. }
  1001. };
  1002. /**
  1003. * Writes an array of hash64 strings to the buffer as a repeated signed 64-bit
  1004. * int field.
  1005. * @param {number} field The field number.
  1006. * @param {?Array<string>} value The array of ints to write.
  1007. */
  1008. jspb.BinaryWriter.prototype.writeRepeatedSintHash64 = function(field, value) {
  1009. if (value == null) return;
  1010. for (var i = 0; i < value.length; i++) {
  1011. this.writeZigzagVarintHash64_(field, value[i]);
  1012. }
  1013. };
  1014. /**
  1015. * Writes an array of numbers to the buffer as a repeated fixed32 field. This
  1016. * works for both signed and unsigned fixed32s.
  1017. * @param {number} field The field number.
  1018. * @param {?Array<number>} value The array of ints to write.
  1019. */
  1020. jspb.BinaryWriter.prototype.writeRepeatedFixed32 = function(field, value) {
  1021. if (value == null) return;
  1022. for (var i = 0; i < value.length; i++) {
  1023. this.writeFixed32(field, value[i]);
  1024. }
  1025. };
  1026. /**
  1027. * Writes an array of numbers to the buffer as a repeated fixed64 field. This
  1028. * works for both signed and unsigned fixed64s.
  1029. * @param {number} field The field number.
  1030. * @param {?Array<number>} value The array of ints to write.
  1031. */
  1032. jspb.BinaryWriter.prototype.writeRepeatedFixed64 = function(field, value) {
  1033. if (value == null) return;
  1034. for (var i = 0; i < value.length; i++) {
  1035. this.writeFixed64(field, value[i]);
  1036. }
  1037. };
  1038. /**
  1039. * Writes an array of numbers to the buffer as a repeated fixed64 field. This
  1040. * works for both signed and unsigned fixed64s.
  1041. * @param {number} field The field number.
  1042. * @param {?Array<string>} value The array of decimal strings to write.
  1043. */
  1044. jspb.BinaryWriter.prototype.writeRepeatedFixed64String = function(
  1045. field, value) {
  1046. if (value == null) return;
  1047. for (var i = 0; i < value.length; i++) {
  1048. this.writeFixed64String(field, value[i]);
  1049. }
  1050. };
  1051. /**
  1052. * Writes an array of numbers to the buffer as a repeated sfixed32 field.
  1053. * @param {number} field The field number.
  1054. * @param {?Array<number>} value The array of ints to write.
  1055. */
  1056. jspb.BinaryWriter.prototype.writeRepeatedSfixed32 = function(field, value) {
  1057. if (value == null) return;
  1058. for (var i = 0; i < value.length; i++) {
  1059. this.writeSfixed32(field, value[i]);
  1060. }
  1061. };
  1062. /**
  1063. * Writes an array of numbers to the buffer as a repeated sfixed64 field.
  1064. * @param {number} field The field number.
  1065. * @param {?Array<number>} value The array of ints to write.
  1066. */
  1067. jspb.BinaryWriter.prototype.writeRepeatedSfixed64 = function(field, value) {
  1068. if (value == null) return;
  1069. for (var i = 0; i < value.length; i++) {
  1070. this.writeSfixed64(field, value[i]);
  1071. }
  1072. };
  1073. /**
  1074. * Writes an array of decimal strings to the buffer as a repeated sfixed64
  1075. * field.
  1076. * @param {number} field The field number.
  1077. * @param {?Array<string>} value The array of decimal strings to write.
  1078. */
  1079. jspb.BinaryWriter.prototype.writeRepeatedSfixed64String = function(field, value) {
  1080. if (value == null) return;
  1081. for (var i = 0; i < value.length; i++) {
  1082. this.writeSfixed64String(field, value[i]);
  1083. }
  1084. };
  1085. /**
  1086. * Writes an array of numbers to the buffer as a repeated float field.
  1087. * @param {number} field The field number.
  1088. * @param {?Array<number>} value The array of ints to write.
  1089. */
  1090. jspb.BinaryWriter.prototype.writeRepeatedFloat = function(field, value) {
  1091. if (value == null) return;
  1092. for (var i = 0; i < value.length; i++) {
  1093. this.writeFloat(field, value[i]);
  1094. }
  1095. };
  1096. /**
  1097. * Writes an array of numbers to the buffer as a repeated double field.
  1098. * @param {number} field The field number.
  1099. * @param {?Array<number>} value The array of ints to write.
  1100. */
  1101. jspb.BinaryWriter.prototype.writeRepeatedDouble = function(field, value) {
  1102. if (value == null) return;
  1103. for (var i = 0; i < value.length; i++) {
  1104. this.writeDouble(field, value[i]);
  1105. }
  1106. };
  1107. /**
  1108. * Writes an array of booleans to the buffer as a repeated bool field.
  1109. * @param {number} field The field number.
  1110. * @param {?Array<boolean>} value The array of ints to write.
  1111. */
  1112. jspb.BinaryWriter.prototype.writeRepeatedBool = function(field, value) {
  1113. if (value == null) return;
  1114. for (var i = 0; i < value.length; i++) {
  1115. this.writeBool(field, value[i]);
  1116. }
  1117. };
  1118. /**
  1119. * Writes an array of enums to the buffer as a repeated enum field.
  1120. * @param {number} field The field number.
  1121. * @param {?Array<number>} value The array of ints to write.
  1122. */
  1123. jspb.BinaryWriter.prototype.writeRepeatedEnum = function(field, value) {
  1124. if (value == null) return;
  1125. for (var i = 0; i < value.length; i++) {
  1126. this.writeEnum(field, value[i]);
  1127. }
  1128. };
  1129. /**
  1130. * Writes an array of strings to the buffer as a repeated string field.
  1131. * @param {number} field The field number.
  1132. * @param {?Array<string>} value The array of strings to write.
  1133. */
  1134. jspb.BinaryWriter.prototype.writeRepeatedString = function(field, value) {
  1135. if (value == null) return;
  1136. for (var i = 0; i < value.length; i++) {
  1137. this.writeString(field, value[i]);
  1138. }
  1139. };
  1140. /**
  1141. * Writes an array of arbitrary byte fields to the buffer.
  1142. * @param {number} field The field number.
  1143. * @param {?Array<!jspb.ByteSource>} value The arrays of arrays of bytes to
  1144. * write.
  1145. */
  1146. jspb.BinaryWriter.prototype.writeRepeatedBytes = function(field, value) {
  1147. if (value == null) return;
  1148. for (var i = 0; i < value.length; i++) {
  1149. this.writeBytes(field, value[i]);
  1150. }
  1151. };
  1152. /**
  1153. * Writes an array of messages to the buffer.
  1154. * @template MessageType
  1155. * @param {number} field The field number.
  1156. * @param {?Array<MessageType>} value The array of messages to
  1157. * write.
  1158. * @param {function(MessageType, !jspb.BinaryWriter)} writerCallback
  1159. * Will be invoked with the value to write and the writer to write it with.
  1160. */
  1161. jspb.BinaryWriter.prototype.writeRepeatedMessage = function(
  1162. field, value, writerCallback) {
  1163. if (value == null) return;
  1164. for (var i = 0; i < value.length; i++) {
  1165. var bookmark = this.beginDelimited_(field);
  1166. writerCallback(value[i], this);
  1167. this.endDelimited_(bookmark);
  1168. }
  1169. };
  1170. /**
  1171. * Writes an array of group messages to the buffer.
  1172. * @template MessageType
  1173. * @param {number} field The field number.
  1174. * @param {?Array<MessageType>} value The array of messages to
  1175. * write.
  1176. * @param {function(MessageType, !jspb.BinaryWriter)} writerCallback
  1177. * Will be invoked with the value to write and the writer to write it with.
  1178. */
  1179. jspb.BinaryWriter.prototype.writeRepeatedGroup = function(
  1180. field, value, writerCallback) {
  1181. if (value == null) return;
  1182. for (var i = 0; i < value.length; i++) {
  1183. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.START_GROUP);
  1184. writerCallback(value[i], this);
  1185. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.END_GROUP);
  1186. }
  1187. };
  1188. /**
  1189. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  1190. * the buffer.
  1191. * @param {number} field The field number.
  1192. * @param {?Array<string>} value The array of hashes to write.
  1193. */
  1194. jspb.BinaryWriter.prototype.writeRepeatedFixedHash64 =
  1195. function(field, value) {
  1196. if (value == null) return;
  1197. for (var i = 0; i < value.length; i++) {
  1198. this.writeFixedHash64(field, value[i]);
  1199. }
  1200. };
  1201. /**
  1202. * Writes a repeated 64-bit hash string field (8 characters @ 8 bits of data
  1203. * each) to the buffer.
  1204. * @param {number} field The field number.
  1205. * @param {?Array<string>} value The array of hashes to write.
  1206. */
  1207. jspb.BinaryWriter.prototype.writeRepeatedVarintHash64 =
  1208. function(field, value) {
  1209. if (value == null) return;
  1210. for (var i = 0; i < value.length; i++) {
  1211. this.writeVarintHash64(field, value[i]);
  1212. }
  1213. };
  1214. /**
  1215. * Writes an array of numbers to the buffer as a packed 32-bit int field.
  1216. * @param {number} field The field number.
  1217. * @param {?Array<number>} value The array of ints to write.
  1218. */
  1219. jspb.BinaryWriter.prototype.writePackedInt32 = function(field, value) {
  1220. if (value == null || !value.length) return;
  1221. var bookmark = this.beginDelimited_(field);
  1222. for (var i = 0; i < value.length; i++) {
  1223. this.encoder_.writeSignedVarint32(value[i]);
  1224. }
  1225. this.endDelimited_(bookmark);
  1226. };
  1227. /**
  1228. * Writes an array of numbers represented as strings to the buffer as a packed
  1229. * 32-bit int field.
  1230. * @param {number} field
  1231. * @param {?Array<string>} value
  1232. */
  1233. jspb.BinaryWriter.prototype.writePackedInt32String = function(field, value) {
  1234. if (value == null || !value.length) return;
  1235. var bookmark = this.beginDelimited_(field);
  1236. for (var i = 0; i < value.length; i++) {
  1237. this.encoder_.writeSignedVarint32(parseInt(value[i], 10));
  1238. }
  1239. this.endDelimited_(bookmark);
  1240. };
  1241. /**
  1242. * Writes an array of numbers to the buffer as a packed 64-bit int field.
  1243. * @param {number} field The field number.
  1244. * @param {?Array<number>} value The array of ints to write.
  1245. */
  1246. jspb.BinaryWriter.prototype.writePackedInt64 = function(field, value) {
  1247. if (value == null || !value.length) return;
  1248. var bookmark = this.beginDelimited_(field);
  1249. for (var i = 0; i < value.length; i++) {
  1250. this.encoder_.writeSignedVarint64(value[i]);
  1251. }
  1252. this.endDelimited_(bookmark);
  1253. };
  1254. /**
  1255. * Writes an array of 64-bit values to the buffer as a fixed64.
  1256. * @param {number} field The field number.
  1257. * @param {?Array<T>} value The value.
  1258. * @param {function(T): number} lo Function to get low bits.
  1259. * @param {function(T): number} hi Function to get high bits.
  1260. * @template T
  1261. */
  1262. jspb.BinaryWriter.prototype.writePackedSplitFixed64 = function(
  1263. field, value, lo, hi) {
  1264. if (value == null) return;
  1265. var bookmark = this.beginDelimited_(field);
  1266. for (var i = 0; i < value.length; i++) {
  1267. this.encoder_.writeSplitFixed64(lo(value[i]), hi(value[i]));
  1268. }
  1269. this.endDelimited_(bookmark);
  1270. };
  1271. /**
  1272. * Writes an array of 64-bit values to the buffer as a varint.
  1273. * @param {number} field The field number.
  1274. * @param {?Array<T>} value The value.
  1275. * @param {function(T): number} lo Function to get low bits.
  1276. * @param {function(T): number} hi Function to get high bits.
  1277. * @template T
  1278. */
  1279. jspb.BinaryWriter.prototype.writePackedSplitVarint64 = function(
  1280. field, value, lo, hi) {
  1281. if (value == null) return;
  1282. var bookmark = this.beginDelimited_(field);
  1283. for (var i = 0; i < value.length; i++) {
  1284. this.encoder_.writeSplitVarint64(lo(value[i]), hi(value[i]));
  1285. }
  1286. this.endDelimited_(bookmark);
  1287. };
  1288. /**
  1289. * Writes an array of 64-bit values to the buffer as a zigzag varint.
  1290. * @param {number} field The field number.
  1291. * @param {?Array<T>} value The value.
  1292. * @param {function(T): number} lo Function to get low bits.
  1293. * @param {function(T): number} hi Function to get high bits.
  1294. * @template T
  1295. */
  1296. jspb.BinaryWriter.prototype.writePackedSplitZigzagVarint64 = function(
  1297. field, value, lo, hi) {
  1298. if (value == null) return;
  1299. var bookmark = this.beginDelimited_(field);
  1300. var encoder = this.encoder_;
  1301. for (var i = 0; i < value.length; i++) {
  1302. jspb.utils.toZigzag64(
  1303. lo(value[i]), hi(value[i]), function(bitsLow, bitsHigh) {
  1304. encoder.writeSplitVarint64(bitsLow >>> 0, bitsHigh >>> 0);
  1305. });
  1306. }
  1307. this.endDelimited_(bookmark);
  1308. };
  1309. /**
  1310. * Writes an array of numbers represented as strings to the buffer as a packed
  1311. * 64-bit int field.
  1312. * @param {number} field
  1313. * @param {?Array<string>} value
  1314. */
  1315. jspb.BinaryWriter.prototype.writePackedInt64String = function(field, value) {
  1316. if (value == null || !value.length) return;
  1317. var bookmark = this.beginDelimited_(field);
  1318. for (var i = 0; i < value.length; i++) {
  1319. var num = jspb.arith.Int64.fromString(value[i]);
  1320. this.encoder_.writeSplitVarint64(num.lo, num.hi);
  1321. }
  1322. this.endDelimited_(bookmark);
  1323. };
  1324. /**
  1325. * Writes an array numbers to the buffer as a packed unsigned 32-bit int field.
  1326. * @param {number} field The field number.
  1327. * @param {?Array<number>} value The array of ints to write.
  1328. */
  1329. jspb.BinaryWriter.prototype.writePackedUint32 = function(field, value) {
  1330. if (value == null || !value.length) return;
  1331. var bookmark = this.beginDelimited_(field);
  1332. for (var i = 0; i < value.length; i++) {
  1333. this.encoder_.writeUnsignedVarint32(value[i]);
  1334. }
  1335. this.endDelimited_(bookmark);
  1336. };
  1337. /**
  1338. * Writes an array of numbers represented as strings to the buffer as a packed
  1339. * unsigned 32-bit int field.
  1340. * @param {number} field
  1341. * @param {?Array<string>} value
  1342. */
  1343. jspb.BinaryWriter.prototype.writePackedUint32String =
  1344. function(field, value) {
  1345. if (value == null || !value.length) return;
  1346. var bookmark = this.beginDelimited_(field);
  1347. for (var i = 0; i < value.length; i++) {
  1348. this.encoder_.writeUnsignedVarint32(parseInt(value[i], 10));
  1349. }
  1350. this.endDelimited_(bookmark);
  1351. };
  1352. /**
  1353. * Writes an array numbers to the buffer as a packed unsigned 64-bit int field.
  1354. * @param {number} field The field number.
  1355. * @param {?Array<number>} value The array of ints to write.
  1356. */
  1357. jspb.BinaryWriter.prototype.writePackedUint64 = function(field, value) {
  1358. if (value == null || !value.length) return;
  1359. var bookmark = this.beginDelimited_(field);
  1360. for (var i = 0; i < value.length; i++) {
  1361. this.encoder_.writeUnsignedVarint64(value[i]);
  1362. }
  1363. this.endDelimited_(bookmark);
  1364. };
  1365. /**
  1366. * Writes an array of numbers represented as strings to the buffer as a packed
  1367. * unsigned 64-bit int field.
  1368. * @param {number} field
  1369. * @param {?Array<string>} value
  1370. */
  1371. jspb.BinaryWriter.prototype.writePackedUint64String =
  1372. function(field, value) {
  1373. if (value == null || !value.length) return;
  1374. var bookmark = this.beginDelimited_(field);
  1375. for (var i = 0; i < value.length; i++) {
  1376. var num = jspb.arith.UInt64.fromString(value[i]);
  1377. this.encoder_.writeSplitVarint64(num.lo, num.hi);
  1378. }
  1379. this.endDelimited_(bookmark);
  1380. };
  1381. /**
  1382. * Writes an array numbers to the buffer as a packed signed 32-bit int field.
  1383. * @param {number} field The field number.
  1384. * @param {?Array<number>} value The array of ints to write.
  1385. */
  1386. jspb.BinaryWriter.prototype.writePackedSint32 = function(field, value) {
  1387. if (value == null || !value.length) return;
  1388. var bookmark = this.beginDelimited_(field);
  1389. for (var i = 0; i < value.length; i++) {
  1390. this.encoder_.writeZigzagVarint32(value[i]);
  1391. }
  1392. this.endDelimited_(bookmark);
  1393. };
  1394. /**
  1395. * Writes an array of numbers to the buffer as a packed signed 64-bit int field.
  1396. * @param {number} field The field number.
  1397. * @param {?Array<number>} value The array of ints to write.
  1398. */
  1399. jspb.BinaryWriter.prototype.writePackedSint64 = function(field, value) {
  1400. if (value == null || !value.length) return;
  1401. var bookmark = this.beginDelimited_(field);
  1402. for (var i = 0; i < value.length; i++) {
  1403. this.encoder_.writeZigzagVarint64(value[i]);
  1404. }
  1405. this.endDelimited_(bookmark);
  1406. };
  1407. /**
  1408. * Writes an array of decimal strings to the buffer as a packed signed 64-bit
  1409. * int field.
  1410. * @param {number} field The field number.
  1411. * @param {?Array<string>} value The array of decimal strings to write.
  1412. */
  1413. jspb.BinaryWriter.prototype.writePackedSint64String = function(field, value) {
  1414. if (value == null || !value.length) return;
  1415. var bookmark = this.beginDelimited_(field);
  1416. for (var i = 0; i < value.length; i++) {
  1417. this.encoder_.writeZigzagVarintHash64(
  1418. jspb.utils.decimalStringToHash64(value[i]));
  1419. }
  1420. this.endDelimited_(bookmark);
  1421. };
  1422. /**
  1423. * Writes an array of hash 64 strings to the buffer as a packed signed 64-bit
  1424. * int field.
  1425. * @param {number} field The field number.
  1426. * @param {?Array<string>} value The array of decimal strings to write.
  1427. */
  1428. jspb.BinaryWriter.prototype.writePackedSintHash64 = function(field, value) {
  1429. if (value == null || !value.length) return;
  1430. var bookmark = this.beginDelimited_(field);
  1431. for (var i = 0; i < value.length; i++) {
  1432. this.encoder_.writeZigzagVarintHash64(value[i]);
  1433. }
  1434. this.endDelimited_(bookmark);
  1435. };
  1436. /**
  1437. * Writes an array of numbers to the buffer as a packed fixed32 field.
  1438. * @param {number} field The field number.
  1439. * @param {?Array<number>} value The array of ints to write.
  1440. */
  1441. jspb.BinaryWriter.prototype.writePackedFixed32 = function(field, value) {
  1442. if (value == null || !value.length) return;
  1443. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1444. this.encoder_.writeUnsignedVarint32(value.length * 4);
  1445. for (var i = 0; i < value.length; i++) {
  1446. this.encoder_.writeUint32(value[i]);
  1447. }
  1448. };
  1449. /**
  1450. * Writes an array of numbers to the buffer as a packed fixed64 field.
  1451. * @param {number} field The field number.
  1452. * @param {?Array<number>} value The array of ints to write.
  1453. */
  1454. jspb.BinaryWriter.prototype.writePackedFixed64 = function(field, value) {
  1455. if (value == null || !value.length) return;
  1456. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1457. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1458. for (var i = 0; i < value.length; i++) {
  1459. this.encoder_.writeUint64(value[i]);
  1460. }
  1461. };
  1462. /**
  1463. * Writes an array of numbers represented as strings to the buffer as a packed
  1464. * fixed64 field.
  1465. * @param {number} field The field number.
  1466. * @param {?Array<string>} value The array of strings to write.
  1467. */
  1468. jspb.BinaryWriter.prototype.writePackedFixed64String = function(field, value) {
  1469. if (value == null || !value.length) return;
  1470. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1471. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1472. for (var i = 0; i < value.length; i++) {
  1473. var num = jspb.arith.UInt64.fromString(value[i]);
  1474. this.encoder_.writeSplitFixed64(num.lo, num.hi);
  1475. }
  1476. };
  1477. /**
  1478. * Writes an array of numbers to the buffer as a packed sfixed32 field.
  1479. * @param {number} field The field number.
  1480. * @param {?Array<number>} value The array of ints to write.
  1481. */
  1482. jspb.BinaryWriter.prototype.writePackedSfixed32 = function(field, value) {
  1483. if (value == null || !value.length) return;
  1484. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1485. this.encoder_.writeUnsignedVarint32(value.length * 4);
  1486. for (var i = 0; i < value.length; i++) {
  1487. this.encoder_.writeInt32(value[i]);
  1488. }
  1489. };
  1490. /**
  1491. * Writes an array of numbers to the buffer as a packed sfixed64 field.
  1492. * @param {number} field The field number.
  1493. * @param {?Array<number>} value The array of ints to write.
  1494. */
  1495. jspb.BinaryWriter.prototype.writePackedSfixed64 = function(field, value) {
  1496. if (value == null || !value.length) return;
  1497. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1498. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1499. for (var i = 0; i < value.length; i++) {
  1500. this.encoder_.writeInt64(value[i]);
  1501. }
  1502. };
  1503. /**
  1504. * Writes an array of numbers to the buffer as a packed sfixed64 field.
  1505. * @param {number} field The field number.
  1506. * @param {?Array<string>} value The array of decimal strings to write.
  1507. */
  1508. jspb.BinaryWriter.prototype.writePackedSfixed64String = function(field, value) {
  1509. if (value == null || !value.length) return;
  1510. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1511. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1512. for (var i = 0; i < value.length; i++) {
  1513. this.encoder_.writeInt64String(value[i]);
  1514. }
  1515. };
  1516. /**
  1517. * Writes an array of numbers to the buffer as a packed float field.
  1518. * @param {number} field The field number.
  1519. * @param {?Array<number>} value The array of ints to write.
  1520. */
  1521. jspb.BinaryWriter.prototype.writePackedFloat = function(field, value) {
  1522. if (value == null || !value.length) return;
  1523. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1524. this.encoder_.writeUnsignedVarint32(value.length * 4);
  1525. for (var i = 0; i < value.length; i++) {
  1526. this.encoder_.writeFloat(value[i]);
  1527. }
  1528. };
  1529. /**
  1530. * Writes an array of numbers to the buffer as a packed double field.
  1531. * @param {number} field The field number.
  1532. * @param {?Array<number>} value The array of ints to write.
  1533. */
  1534. jspb.BinaryWriter.prototype.writePackedDouble = function(field, value) {
  1535. if (value == null || !value.length) return;
  1536. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1537. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1538. for (var i = 0; i < value.length; i++) {
  1539. this.encoder_.writeDouble(value[i]);
  1540. }
  1541. };
  1542. /**
  1543. * Writes an array of booleans to the buffer as a packed bool field.
  1544. * @param {number} field The field number.
  1545. * @param {?Array<boolean>} value The array of ints to write.
  1546. */
  1547. jspb.BinaryWriter.prototype.writePackedBool = function(field, value) {
  1548. if (value == null || !value.length) return;
  1549. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1550. this.encoder_.writeUnsignedVarint32(value.length);
  1551. for (var i = 0; i < value.length; i++) {
  1552. this.encoder_.writeBool(value[i]);
  1553. }
  1554. };
  1555. /**
  1556. * Writes an array of enums to the buffer as a packed enum field.
  1557. * @param {number} field The field number.
  1558. * @param {?Array<number>} value The array of ints to write.
  1559. */
  1560. jspb.BinaryWriter.prototype.writePackedEnum = function(field, value) {
  1561. if (value == null || !value.length) return;
  1562. var bookmark = this.beginDelimited_(field);
  1563. for (var i = 0; i < value.length; i++) {
  1564. this.encoder_.writeEnum(value[i]);
  1565. }
  1566. this.endDelimited_(bookmark);
  1567. };
  1568. /**
  1569. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  1570. * the buffer.
  1571. * @param {number} field The field number.
  1572. * @param {?Array<string>} value The array of hashes to write.
  1573. */
  1574. jspb.BinaryWriter.prototype.writePackedFixedHash64 = function(field, value) {
  1575. if (value == null || !value.length) return;
  1576. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1577. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1578. for (var i = 0; i < value.length; i++) {
  1579. this.encoder_.writeFixedHash64(value[i]);
  1580. }
  1581. };
  1582. /**
  1583. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  1584. * the buffer.
  1585. * @param {number} field The field number.
  1586. * @param {?Array<string>} value The array of hashes to write.
  1587. */
  1588. jspb.BinaryWriter.prototype.writePackedVarintHash64 = function(field, value) {
  1589. if (value == null || !value.length) return;
  1590. var bookmark = this.beginDelimited_(field);
  1591. for (var i = 0; i < value.length; i++) {
  1592. this.encoder_.writeVarintHash64(value[i]);
  1593. }
  1594. this.endDelimited_(bookmark);
  1595. };