writer.js 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  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. * @author aappleby@google.com (Austin Appleby)
  55. */
  56. goog.provide('jspb.BinaryWriter');
  57. goog.require('goog.asserts');
  58. goog.require('goog.crypt.base64');
  59. goog.require('jspb.BinaryConstants');
  60. goog.require('jspb.BinaryEncoder');
  61. goog.require('jspb.arith.Int64');
  62. goog.require('jspb.arith.UInt64');
  63. goog.require('jspb.utils');
  64. /**
  65. * BinaryWriter implements encoders for all the wire types specified in
  66. * https://developers.google.com/protocol-buffers/docs/encoding.
  67. *
  68. * @constructor
  69. * @struct
  70. */
  71. jspb.BinaryWriter = function() {
  72. /**
  73. * Blocks of serialized data that will be concatenated once all messages have
  74. * been written.
  75. * @private {!Array<!Uint8Array|!Array<number>>}
  76. */
  77. this.blocks_ = [];
  78. /**
  79. * Total number of bytes in the blocks_ array. Does _not_ include bytes in
  80. * the encoder below.
  81. * @private {number}
  82. */
  83. this.totalLength_ = 0;
  84. /**
  85. * Binary encoder holding pieces of a message that we're still serializing.
  86. * When we get to a stopping point (either the start of a new submessage, or
  87. * when we need to append a raw Uint8Array), the encoder's buffer will be
  88. * added to the block array above and the encoder will be reset.
  89. * @private {!jspb.BinaryEncoder}
  90. */
  91. this.encoder_ = new jspb.BinaryEncoder();
  92. /**
  93. * A stack of bookmarks containing the parent blocks for each message started
  94. * via beginSubMessage(), needed as bookkeeping for endSubMessage().
  95. * TODO(aappleby): Deprecated, users should be calling writeMessage().
  96. * @private {!Array<!Array<number>>}
  97. */
  98. this.bookmarks_ = [];
  99. };
  100. /**
  101. * Append a typed array of bytes onto the buffer.
  102. *
  103. * @param {!Uint8Array} arr The byte array to append.
  104. * @private
  105. */
  106. jspb.BinaryWriter.prototype.appendUint8Array_ = function(arr) {
  107. var temp = this.encoder_.end();
  108. this.blocks_.push(temp);
  109. this.blocks_.push(arr);
  110. this.totalLength_ += temp.length + arr.length;
  111. };
  112. /**
  113. * Begins a new message by writing the field header and returning a bookmark
  114. * which we will use to patch in the message length to in endDelimited_ below.
  115. * @param {number} field
  116. * @return {!Array<number>}
  117. * @private
  118. */
  119. jspb.BinaryWriter.prototype.beginDelimited_ = function(field) {
  120. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  121. var bookmark = this.encoder_.end();
  122. this.blocks_.push(bookmark);
  123. this.totalLength_ += bookmark.length;
  124. bookmark.push(this.totalLength_);
  125. return bookmark;
  126. };
  127. /**
  128. * Ends a message by encoding the _change_ in length of the buffer to the
  129. * parent block and adds the number of bytes needed to encode that length to
  130. * the total byte length.
  131. * @param {!Array<number>} bookmark
  132. * @private
  133. */
  134. jspb.BinaryWriter.prototype.endDelimited_ = function(bookmark) {
  135. var oldLength = bookmark.pop();
  136. var messageLength = this.totalLength_ + this.encoder_.length() - oldLength;
  137. goog.asserts.assert(messageLength >= 0);
  138. while (messageLength > 127) {
  139. bookmark.push((messageLength & 0x7f) | 0x80);
  140. messageLength = messageLength >>> 7;
  141. this.totalLength_++;
  142. }
  143. bookmark.push(messageLength);
  144. this.totalLength_++;
  145. };
  146. /**
  147. * Writes a pre-serialized message to the buffer.
  148. * @param {!Uint8Array} bytes The array of bytes to write.
  149. * @param {number} start The start of the range to write.
  150. * @param {number} end The end of the range to write.
  151. */
  152. jspb.BinaryWriter.prototype.writeSerializedMessage = function(
  153. bytes, start, end) {
  154. this.appendUint8Array_(bytes.subarray(start, end));
  155. };
  156. /**
  157. * Writes a pre-serialized message to the buffer if the message and endpoints
  158. * are non-null.
  159. * @param {?Uint8Array} bytes The array of bytes to write.
  160. * @param {?number} start The start of the range to write.
  161. * @param {?number} end The end of the range to write.
  162. */
  163. jspb.BinaryWriter.prototype.maybeWriteSerializedMessage = function(
  164. bytes, start, end) {
  165. if (bytes != null && start != null && end != null) {
  166. this.writeSerializedMessage(bytes, start, end);
  167. }
  168. };
  169. /**
  170. * Resets the writer, throwing away any accumulated buffers.
  171. */
  172. jspb.BinaryWriter.prototype.reset = function() {
  173. this.blocks_ = [];
  174. this.encoder_.end();
  175. this.totalLength_ = 0;
  176. this.bookmarks_ = [];
  177. };
  178. /**
  179. * Converts the encoded data into a Uint8Array.
  180. * @return {!Uint8Array}
  181. */
  182. jspb.BinaryWriter.prototype.getResultBuffer = function() {
  183. goog.asserts.assert(this.bookmarks_.length == 0);
  184. var flat = new Uint8Array(this.totalLength_ + this.encoder_.length());
  185. var blocks = this.blocks_;
  186. var blockCount = blocks.length;
  187. var offset = 0;
  188. for (var i = 0; i < blockCount; i++) {
  189. var block = blocks[i];
  190. flat.set(block, offset);
  191. offset += block.length;
  192. }
  193. var tail = this.encoder_.end();
  194. flat.set(tail, offset);
  195. offset += tail.length;
  196. // Post condition: `flattened` must have had every byte written.
  197. goog.asserts.assert(offset == flat.length);
  198. // Replace our block list with the flattened block, which lets GC reclaim
  199. // the temp blocks sooner.
  200. this.blocks_ = [flat];
  201. return flat;
  202. };
  203. /**
  204. * Converts the encoded data into a base64-encoded string.
  205. * @param {boolean=} opt_webSafe True indicates we should use a websafe
  206. * alphabet, which does not require escaping for use in URLs.
  207. * @return {string}
  208. */
  209. jspb.BinaryWriter.prototype.getResultBase64String = function(opt_webSafe) {
  210. return goog.crypt.base64.encodeByteArray(this.getResultBuffer(), opt_webSafe);
  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 an int32 field to the buffer. Numbers outside the range [-2^31,2^31)
  397. * will be truncated.
  398. * @param {number} field The field number.
  399. * @param {number?} value The value to write.
  400. */
  401. jspb.BinaryWriter.prototype.writeInt32 = function(field, value) {
  402. if (value == null) return;
  403. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
  404. (value < jspb.BinaryConstants.TWO_TO_31));
  405. this.writeSignedVarint32_(field, value);
  406. };
  407. /**
  408. * Writes an int32 field represented as a string to the buffer. Numbers outside
  409. * the range [-2^31,2^31) will be truncated.
  410. * @param {number} field The field number.
  411. * @param {string?} value The value to write.
  412. */
  413. jspb.BinaryWriter.prototype.writeInt32String = function(field, value) {
  414. if (value == null) return;
  415. var intValue = /** {number} */ parseInt(value, 10);
  416. goog.asserts.assert((intValue >= -jspb.BinaryConstants.TWO_TO_31) &&
  417. (intValue < jspb.BinaryConstants.TWO_TO_31));
  418. this.writeSignedVarint32_(field, intValue);
  419. };
  420. /**
  421. * Writes an int64 field to the buffer. Numbers outside the range [-2^63,2^63)
  422. * will be truncated.
  423. * @param {number} field The field number.
  424. * @param {number?} value The value to write.
  425. */
  426. jspb.BinaryWriter.prototype.writeInt64 = function(field, value) {
  427. if (value == null) return;
  428. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
  429. (value < jspb.BinaryConstants.TWO_TO_63));
  430. this.writeSignedVarint64_(field, value);
  431. };
  432. /**
  433. * Writes a int64 field (with value as a string) to the buffer.
  434. * @param {number} field The field number.
  435. * @param {string?} value The value to write.
  436. */
  437. jspb.BinaryWriter.prototype.writeInt64String = function(field, value) {
  438. if (value == null) return;
  439. var num = jspb.arith.Int64.fromString(value);
  440. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  441. this.encoder_.writeSplitVarint64(num.lo, num.hi);
  442. };
  443. /**
  444. * Writes a uint32 field to the buffer. Numbers outside the range [0,2^32)
  445. * will be truncated.
  446. * @param {number} field The field number.
  447. * @param {number?} value The value to write.
  448. */
  449. jspb.BinaryWriter.prototype.writeUint32 = function(field, value) {
  450. if (value == null) return;
  451. goog.asserts.assert((value >= 0) &&
  452. (value < jspb.BinaryConstants.TWO_TO_32));
  453. this.writeUnsignedVarint32_(field, value);
  454. };
  455. /**
  456. * Writes a uint32 field represented as a string to the buffer. Numbers outside
  457. * the range [0,2^32) will be truncated.
  458. * @param {number} field The field number.
  459. * @param {string?} value The value to write.
  460. */
  461. jspb.BinaryWriter.prototype.writeUint32String = function(field, value) {
  462. if (value == null) return;
  463. var intValue = /** {number} */ parseInt(value, 10);
  464. goog.asserts.assert((intValue >= 0) &&
  465. (intValue < jspb.BinaryConstants.TWO_TO_32));
  466. this.writeUnsignedVarint32_(field, intValue);
  467. };
  468. /**
  469. * Writes a uint64 field to the buffer. Numbers outside the range [0,2^64)
  470. * will be truncated.
  471. * @param {number} field The field number.
  472. * @param {number?} value The value to write.
  473. */
  474. jspb.BinaryWriter.prototype.writeUint64 = function(field, value) {
  475. if (value == null) return;
  476. goog.asserts.assert((value >= 0) &&
  477. (value < jspb.BinaryConstants.TWO_TO_64));
  478. this.writeUnsignedVarint64_(field, value);
  479. };
  480. /**
  481. * Writes a uint64 field (with value as a string) to the buffer.
  482. * @param {number} field The field number.
  483. * @param {string?} value The value to write.
  484. */
  485. jspb.BinaryWriter.prototype.writeUint64String = function(field, value) {
  486. if (value == null) return;
  487. var num = jspb.arith.UInt64.fromString(value);
  488. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  489. this.encoder_.writeSplitVarint64(num.lo, num.hi);
  490. };
  491. /**
  492. * Writes a sint32 field to the buffer. Numbers outside the range [-2^31,2^31)
  493. * will be truncated.
  494. * @param {number} field The field number.
  495. * @param {number?} value The value to write.
  496. */
  497. jspb.BinaryWriter.prototype.writeSint32 = function(field, value) {
  498. if (value == null) return;
  499. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
  500. (value < jspb.BinaryConstants.TWO_TO_31));
  501. this.writeZigzagVarint32_(field, value);
  502. };
  503. /**
  504. * Writes a sint64 field to the buffer. Numbers outside the range [-2^63,2^63)
  505. * will be truncated.
  506. * @param {number} field The field number.
  507. * @param {number?} value The value to write.
  508. */
  509. jspb.BinaryWriter.prototype.writeSint64 = function(field, value) {
  510. if (value == null) return;
  511. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
  512. (value < jspb.BinaryConstants.TWO_TO_63));
  513. this.writeZigzagVarint64_(field, value);
  514. };
  515. /**
  516. * Writes a sint64 field to the buffer. Numbers outside the range [-2^63,2^63)
  517. * will be truncated.
  518. * @param {number} field The field number.
  519. * @param {string?} value The decimal string to write.
  520. */
  521. jspb.BinaryWriter.prototype.writeSint64String = function(field, value) {
  522. if (value == null) return;
  523. goog.asserts.assert((+value >= -jspb.BinaryConstants.TWO_TO_63) &&
  524. (+value < jspb.BinaryConstants.TWO_TO_63));
  525. this.writeZigzagVarint64String_(field, value);
  526. };
  527. /**
  528. * Writes a fixed32 field to the buffer. Numbers outside the range [0,2^32)
  529. * will be truncated.
  530. * @param {number} field The field number.
  531. * @param {number?} value The value to write.
  532. */
  533. jspb.BinaryWriter.prototype.writeFixed32 = function(field, value) {
  534. if (value == null) return;
  535. goog.asserts.assert((value >= 0) &&
  536. (value < jspb.BinaryConstants.TWO_TO_32));
  537. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32);
  538. this.encoder_.writeUint32(value);
  539. };
  540. /**
  541. * Writes a fixed64 field to the buffer. Numbers outside the range [0,2^64)
  542. * will be truncated.
  543. * @param {number} field The field number.
  544. * @param {number?} value The value to write.
  545. */
  546. jspb.BinaryWriter.prototype.writeFixed64 = function(field, value) {
  547. if (value == null) return;
  548. goog.asserts.assert((value >= 0) &&
  549. (value < jspb.BinaryConstants.TWO_TO_64));
  550. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  551. this.encoder_.writeUint64(value);
  552. };
  553. /**
  554. * Writes a fixed64 field (with value as a string) to the buffer.
  555. * @param {number} field The field number.
  556. * @param {string?} value The value to write.
  557. */
  558. jspb.BinaryWriter.prototype.writeFixed64String = function(field, value) {
  559. if (value == null) return;
  560. var num = jspb.arith.UInt64.fromString(value);
  561. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  562. this.encoder_.writeSplitFixed64(num.lo, num.hi);
  563. };
  564. /**
  565. * Writes a sfixed32 field to the buffer. Numbers outside the range
  566. * [-2^31,2^31) will be truncated.
  567. * @param {number} field The field number.
  568. * @param {number?} value The value to write.
  569. */
  570. jspb.BinaryWriter.prototype.writeSfixed32 = function(field, value) {
  571. if (value == null) return;
  572. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
  573. (value < jspb.BinaryConstants.TWO_TO_31));
  574. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32);
  575. this.encoder_.writeInt32(value);
  576. };
  577. /**
  578. * Writes a sfixed64 field to the buffer. Numbers outside the range
  579. * [-2^63,2^63) will be truncated.
  580. * @param {number} field The field number.
  581. * @param {number?} value The value to write.
  582. */
  583. jspb.BinaryWriter.prototype.writeSfixed64 = function(field, value) {
  584. if (value == null) return;
  585. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
  586. (value < jspb.BinaryConstants.TWO_TO_63));
  587. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  588. this.encoder_.writeInt64(value);
  589. };
  590. /**
  591. * Writes a sfixed64 string field to the buffer. Numbers outside the range
  592. * [-2^63,2^63) will be truncated.
  593. * @param {number} field The field number.
  594. * @param {string?} value The value to write.
  595. */
  596. jspb.BinaryWriter.prototype.writeSfixed64String = function(field, value) {
  597. if (value == null) return;
  598. var num = jspb.arith.Int64.fromString(value);
  599. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  600. this.encoder_.writeSplitFixed64(num.lo, num.hi);
  601. };
  602. /**
  603. * Writes a single-precision floating point field to the buffer. Numbers
  604. * requiring more than 32 bits of precision will be truncated.
  605. * @param {number} field The field number.
  606. * @param {number?} value The value to write.
  607. */
  608. jspb.BinaryWriter.prototype.writeFloat = function(field, value) {
  609. if (value == null) return;
  610. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32);
  611. this.encoder_.writeFloat(value);
  612. };
  613. /**
  614. * Writes a double-precision floating point field to the buffer. As this is the
  615. * native format used by JavaScript, no precision will be lost.
  616. * @param {number} field The field number.
  617. * @param {number?} value The value to write.
  618. */
  619. jspb.BinaryWriter.prototype.writeDouble = function(field, value) {
  620. if (value == null) return;
  621. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  622. this.encoder_.writeDouble(value);
  623. };
  624. /**
  625. * Writes a boolean field to the buffer. We allow numbers as input
  626. * because the JSPB code generator uses 0/1 instead of true/false to save space
  627. * in the string representation of the proto.
  628. * @param {number} field The field number.
  629. * @param {boolean?|number?} value The value to write.
  630. */
  631. jspb.BinaryWriter.prototype.writeBool = function(field, value) {
  632. if (value == null) return;
  633. goog.asserts.assert(goog.isBoolean(value) || goog.isNumber(value));
  634. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  635. this.encoder_.writeBool(value);
  636. };
  637. /**
  638. * Writes an enum field to the buffer.
  639. * @param {number} field The field number.
  640. * @param {number?} value The value to write.
  641. */
  642. jspb.BinaryWriter.prototype.writeEnum = function(field, value) {
  643. if (value == null) return;
  644. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
  645. (value < jspb.BinaryConstants.TWO_TO_31));
  646. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  647. this.encoder_.writeSignedVarint32(value);
  648. };
  649. /**
  650. * Writes a string field to the buffer.
  651. * @param {number} field The field number.
  652. * @param {string?} value The string to write.
  653. */
  654. jspb.BinaryWriter.prototype.writeString = function(field, value) {
  655. if (value == null) return;
  656. var bookmark = this.beginDelimited_(field);
  657. this.encoder_.writeString(value);
  658. this.endDelimited_(bookmark);
  659. };
  660. /**
  661. * Writes an arbitrary byte field to the buffer. Note - to match the behavior
  662. * of the C++ implementation, empty byte arrays _are_ serialized.
  663. * @param {number} field The field number.
  664. * @param {?jspb.ByteSource} value The array of bytes to write.
  665. */
  666. jspb.BinaryWriter.prototype.writeBytes = function(field, value) {
  667. if (value == null) return;
  668. var bytes = jspb.utils.byteSourceToUint8Array(value);
  669. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  670. this.encoder_.writeUnsignedVarint32(bytes.length);
  671. this.appendUint8Array_(bytes);
  672. };
  673. /**
  674. * Writes a message to the buffer.
  675. * @param {number} field The field number.
  676. * @param {?MessageType} value The message to write.
  677. * @param {function(MessageTypeNonNull, !jspb.BinaryWriter)} writerCallback
  678. * Will be invoked with the value to write and the writer to write it with.
  679. * @template MessageType
  680. * Use go/closure-ttl to declare a non-nullable version of MessageType. Replace
  681. * the null in blah|null with none. This is necessary because the compiler will
  682. * infer MessageType to be nullable if the value parameter is nullable.
  683. * @template MessageTypeNonNull :=
  684. * cond(isUnknown(MessageType), unknown(),
  685. * mapunion(MessageType, (X) =>
  686. * cond(eq(X, 'null'), none(), X)))
  687. * =:
  688. */
  689. jspb.BinaryWriter.prototype.writeMessage = function(
  690. field, value, writerCallback) {
  691. if (value == null) return;
  692. var bookmark = this.beginDelimited_(field);
  693. writerCallback(value, this);
  694. this.endDelimited_(bookmark);
  695. };
  696. /**
  697. * Writes a message set extension to the buffer.
  698. * @param {number} field The field number for the extension.
  699. * @param {?MessageType} value The extension message object to write. Note that
  700. * message set can only have extensions with type of optional message.
  701. * @param {function(!MessageTypeNonNull, !jspb.BinaryWriter)} writerCallback
  702. * Will be invoked with the value to write and the writer to write it with.
  703. * @template MessageType
  704. * Use go/closure-ttl to declare a non-nullable version of MessageType. Replace
  705. * the null in blah|null with none. This is necessary because the compiler will
  706. * infer MessageType to be nullable if the value parameter is nullable.
  707. * @template MessageTypeNonNull :=
  708. * cond(isUnknown(MessageType), unknown(),
  709. * mapunion(MessageType, (X) =>
  710. * cond(eq(X, 'null'), none(), X)))
  711. * =:
  712. */
  713. jspb.BinaryWriter.prototype.writeMessageSet = function(
  714. field, value, writerCallback) {
  715. if (value == null) return;
  716. // The wire format for a message set is defined by
  717. // google3/net/proto/message_set.proto
  718. this.writeFieldHeader_(1, jspb.BinaryConstants.WireType.START_GROUP);
  719. this.writeFieldHeader_(2, jspb.BinaryConstants.WireType.VARINT);
  720. this.encoder_.writeSignedVarint32(field);
  721. var bookmark = this.beginDelimited_(3);
  722. writerCallback(value, this);
  723. this.endDelimited_(bookmark);
  724. this.writeFieldHeader_(1, jspb.BinaryConstants.WireType.END_GROUP);
  725. };
  726. /**
  727. * Writes a group message to the buffer.
  728. *
  729. * @param {number} field The field number.
  730. * @param {?MessageType} value The message to write, wrapped with START_GROUP /
  731. * END_GROUP tags. Will be a no-op if 'value' is null.
  732. * @param {function(MessageTypeNonNull, !jspb.BinaryWriter)} writerCallback
  733. * Will be invoked with the value to write and the writer to write it with.
  734. * @template MessageType
  735. * Use go/closure-ttl to declare a non-nullable version of MessageType. Replace
  736. * the null in blah|null with none. This is necessary because the compiler will
  737. * infer MessageType to be nullable if the value parameter is nullable.
  738. * @template MessageTypeNonNull :=
  739. * cond(isUnknown(MessageType), unknown(),
  740. * mapunion(MessageType, (X) =>
  741. * cond(eq(X, 'null'), none(), X)))
  742. * =:
  743. */
  744. jspb.BinaryWriter.prototype.writeGroup = function(
  745. field, value, writerCallback) {
  746. if (value == null) return;
  747. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.START_GROUP);
  748. writerCallback(value, this);
  749. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.END_GROUP);
  750. };
  751. /**
  752. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  753. * the buffer.
  754. * @param {number} field The field number.
  755. * @param {string?} value The hash string.
  756. */
  757. jspb.BinaryWriter.prototype.writeFixedHash64 = function(field, value) {
  758. if (value == null) return;
  759. goog.asserts.assert(value.length == 8);
  760. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  761. this.encoder_.writeFixedHash64(value);
  762. };
  763. /**
  764. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  765. * the buffer.
  766. * @param {number} field The field number.
  767. * @param {string?} value The hash string.
  768. */
  769. jspb.BinaryWriter.prototype.writeVarintHash64 = function(field, value) {
  770. if (value == null) return;
  771. goog.asserts.assert(value.length == 8);
  772. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  773. this.encoder_.writeVarintHash64(value);
  774. };
  775. /**
  776. * Writes an array of numbers to the buffer as a repeated 32-bit int field.
  777. * @param {number} field The field number.
  778. * @param {?Array<number>} value The array of ints to write.
  779. */
  780. jspb.BinaryWriter.prototype.writeRepeatedInt32 = function(field, value) {
  781. if (value == null) return;
  782. for (var i = 0; i < value.length; i++) {
  783. this.writeSignedVarint32_(field, value[i]);
  784. }
  785. };
  786. /**
  787. * Writes an array of numbers formatted as strings to the buffer as a repeated
  788. * 32-bit int field.
  789. * @param {number} field The field number.
  790. * @param {?Array<string>} value The array of ints to write.
  791. */
  792. jspb.BinaryWriter.prototype.writeRepeatedInt32String = function(field, value) {
  793. if (value == null) return;
  794. for (var i = 0; i < value.length; i++) {
  795. this.writeInt32String(field, value[i]);
  796. }
  797. };
  798. /**
  799. * Writes an array of numbers to the buffer as a repeated 64-bit int field.
  800. * @param {number} field The field number.
  801. * @param {?Array<number>} value The array of ints to write.
  802. */
  803. jspb.BinaryWriter.prototype.writeRepeatedInt64 = function(field, value) {
  804. if (value == null) return;
  805. for (var i = 0; i < value.length; i++) {
  806. this.writeSignedVarint64_(field, value[i]);
  807. }
  808. };
  809. /**
  810. * Writes an array of numbers formatted as strings to the buffer as a repeated
  811. * 64-bit int field.
  812. * @param {number} field The field number.
  813. * @param {?Array<string>} value The array of ints to write.
  814. */
  815. jspb.BinaryWriter.prototype.writeRepeatedInt64String = function(field, value) {
  816. if (value == null) return;
  817. for (var i = 0; i < value.length; i++) {
  818. this.writeInt64String(field, value[i]);
  819. }
  820. };
  821. /**
  822. * Writes an array numbers to the buffer as a repeated unsigned 32-bit int
  823. * field.
  824. * @param {number} field The field number.
  825. * @param {?Array<number>} value The array of ints to write.
  826. */
  827. jspb.BinaryWriter.prototype.writeRepeatedUint32 = function(field, value) {
  828. if (value == null) return;
  829. for (var i = 0; i < value.length; i++) {
  830. this.writeUnsignedVarint32_(field, value[i]);
  831. }
  832. };
  833. /**
  834. * Writes an array of numbers formatted as strings to the buffer as a repeated
  835. * unsigned 32-bit int field.
  836. * @param {number} field The field number.
  837. * @param {?Array<string>} value The array of ints to write.
  838. */
  839. jspb.BinaryWriter.prototype.writeRepeatedUint32String = function(field, value) {
  840. if (value == null) return;
  841. for (var i = 0; i < value.length; i++) {
  842. this.writeUint32String(field, value[i]);
  843. }
  844. };
  845. /**
  846. * Writes an array numbers to the buffer as a repeated unsigned 64-bit int
  847. * field.
  848. * @param {number} field The field number.
  849. * @param {?Array<number>} value The array of ints to write.
  850. */
  851. jspb.BinaryWriter.prototype.writeRepeatedUint64 = function(field, value) {
  852. if (value == null) return;
  853. for (var i = 0; i < value.length; i++) {
  854. this.writeUnsignedVarint64_(field, value[i]);
  855. }
  856. };
  857. /**
  858. * Writes an array of numbers formatted as strings to the buffer as a repeated
  859. * unsigned 64-bit int field.
  860. * @param {number} field The field number.
  861. * @param {?Array<string>} value The array of ints to write.
  862. */
  863. jspb.BinaryWriter.prototype.writeRepeatedUint64String = function(field, value) {
  864. if (value == null) return;
  865. for (var i = 0; i < value.length; i++) {
  866. this.writeUint64String(field, value[i]);
  867. }
  868. };
  869. /**
  870. * Writes an array numbers to the buffer as a repeated signed 32-bit int field.
  871. * @param {number} field The field number.
  872. * @param {?Array<number>} value The array of ints to write.
  873. */
  874. jspb.BinaryWriter.prototype.writeRepeatedSint32 = function(field, value) {
  875. if (value == null) return;
  876. for (var i = 0; i < value.length; i++) {
  877. this.writeZigzagVarint32_(field, value[i]);
  878. }
  879. };
  880. /**
  881. * Writes an array numbers to the buffer as a repeated signed 64-bit int field.
  882. * @param {number} field The field number.
  883. * @param {?Array<number>} value The array of ints to write.
  884. */
  885. jspb.BinaryWriter.prototype.writeRepeatedSint64 = function(field, value) {
  886. if (value == null) return;
  887. for (var i = 0; i < value.length; i++) {
  888. this.writeZigzagVarint64_(field, value[i]);
  889. }
  890. };
  891. /**
  892. * Writes an array numbers to the buffer as a repeated signed 64-bit int field.
  893. * @param {number} field The field number.
  894. * @param {?Array<string>} value The array of ints to write.
  895. */
  896. jspb.BinaryWriter.prototype.writeRepeatedSint64String = function(field, value) {
  897. if (value == null) return;
  898. for (var i = 0; i < value.length; i++) {
  899. this.writeZigzagVarint64String_(field, value[i]);
  900. }
  901. };
  902. /**
  903. * Writes an array of numbers to the buffer as a repeated fixed32 field. This
  904. * works for both signed and unsigned fixed32s.
  905. * @param {number} field The field number.
  906. * @param {?Array<number>} value The array of ints to write.
  907. */
  908. jspb.BinaryWriter.prototype.writeRepeatedFixed32 = function(field, value) {
  909. if (value == null) return;
  910. for (var i = 0; i < value.length; i++) {
  911. this.writeFixed32(field, value[i]);
  912. }
  913. };
  914. /**
  915. * Writes an array of numbers to the buffer as a repeated fixed64 field. This
  916. * works for both signed and unsigned fixed64s.
  917. * @param {number} field The field number.
  918. * @param {?Array<number>} value The array of ints to write.
  919. */
  920. jspb.BinaryWriter.prototype.writeRepeatedFixed64 = function(field, value) {
  921. if (value == null) return;
  922. for (var i = 0; i < value.length; i++) {
  923. this.writeFixed64(field, value[i]);
  924. }
  925. };
  926. /**
  927. * Writes an array of numbers to the buffer as a repeated fixed64 field. This
  928. * works for both signed and unsigned fixed64s.
  929. * @param {number} field The field number.
  930. * @param {?Array<string>} value The array of decimal strings to write.
  931. */
  932. jspb.BinaryWriter.prototype.writeRepeatedFixed64String = function(
  933. field, value) {
  934. if (value == null) return;
  935. for (var i = 0; i < value.length; i++) {
  936. this.writeFixed64String(field, value[i]);
  937. }
  938. };
  939. /**
  940. * Writes an array of numbers to the buffer as a repeated sfixed32 field.
  941. * @param {number} field The field number.
  942. * @param {?Array<number>} value The array of ints to write.
  943. */
  944. jspb.BinaryWriter.prototype.writeRepeatedSfixed32 = function(field, value) {
  945. if (value == null) return;
  946. for (var i = 0; i < value.length; i++) {
  947. this.writeSfixed32(field, value[i]);
  948. }
  949. };
  950. /**
  951. * Writes an array of numbers to the buffer as a repeated sfixed64 field.
  952. * @param {number} field The field number.
  953. * @param {?Array<number>} value The array of ints to write.
  954. */
  955. jspb.BinaryWriter.prototype.writeRepeatedSfixed64 = function(field, value) {
  956. if (value == null) return;
  957. for (var i = 0; i < value.length; i++) {
  958. this.writeSfixed64(field, value[i]);
  959. }
  960. };
  961. /**
  962. * Writes an array of decimal strings to the buffer as a repeated sfixed64
  963. * field.
  964. * @param {number} field The field number.
  965. * @param {?Array<string>} value The array of decimal strings to write.
  966. */
  967. jspb.BinaryWriter.prototype.writeRepeatedSfixed64String = function(field, value) {
  968. if (value == null) return;
  969. for (var i = 0; i < value.length; i++) {
  970. this.writeSfixed64String(field, value[i]);
  971. }
  972. };
  973. /**
  974. * Writes an array of numbers to the buffer as a repeated float field.
  975. * @param {number} field The field number.
  976. * @param {?Array<number>} value The array of ints to write.
  977. */
  978. jspb.BinaryWriter.prototype.writeRepeatedFloat = function(field, value) {
  979. if (value == null) return;
  980. for (var i = 0; i < value.length; i++) {
  981. this.writeFloat(field, value[i]);
  982. }
  983. };
  984. /**
  985. * Writes an array of numbers to the buffer as a repeated double field.
  986. * @param {number} field The field number.
  987. * @param {?Array<number>} value The array of ints to write.
  988. */
  989. jspb.BinaryWriter.prototype.writeRepeatedDouble = function(field, value) {
  990. if (value == null) return;
  991. for (var i = 0; i < value.length; i++) {
  992. this.writeDouble(field, value[i]);
  993. }
  994. };
  995. /**
  996. * Writes an array of booleans to the buffer as a repeated bool field.
  997. * @param {number} field The field number.
  998. * @param {?Array<boolean>} value The array of ints to write.
  999. */
  1000. jspb.BinaryWriter.prototype.writeRepeatedBool = function(field, value) {
  1001. if (value == null) return;
  1002. for (var i = 0; i < value.length; i++) {
  1003. this.writeBool(field, value[i]);
  1004. }
  1005. };
  1006. /**
  1007. * Writes an array of enums to the buffer as a repeated enum field.
  1008. * @param {number} field The field number.
  1009. * @param {?Array<number>} value The array of ints to write.
  1010. */
  1011. jspb.BinaryWriter.prototype.writeRepeatedEnum = function(field, value) {
  1012. if (value == null) return;
  1013. for (var i = 0; i < value.length; i++) {
  1014. this.writeEnum(field, value[i]);
  1015. }
  1016. };
  1017. /**
  1018. * Writes an array of strings to the buffer as a repeated string field.
  1019. * @param {number} field The field number.
  1020. * @param {?Array<string>} value The array of strings to write.
  1021. */
  1022. jspb.BinaryWriter.prototype.writeRepeatedString = function(field, value) {
  1023. if (value == null) return;
  1024. for (var i = 0; i < value.length; i++) {
  1025. this.writeString(field, value[i]);
  1026. }
  1027. };
  1028. /**
  1029. * Writes an array of arbitrary byte fields to the buffer.
  1030. * @param {number} field The field number.
  1031. * @param {?Array<!jspb.ByteSource>} value The arrays of arrays of bytes to
  1032. * write.
  1033. */
  1034. jspb.BinaryWriter.prototype.writeRepeatedBytes = function(field, value) {
  1035. if (value == null) return;
  1036. for (var i = 0; i < value.length; i++) {
  1037. this.writeBytes(field, value[i]);
  1038. }
  1039. };
  1040. /**
  1041. * Writes an array of messages to the buffer.
  1042. * @template MessageType
  1043. * @param {number} field The field number.
  1044. * @param {?Array<MessageType>} value The array of messages to
  1045. * write.
  1046. * @param {function(MessageType, !jspb.BinaryWriter)} writerCallback
  1047. * Will be invoked with the value to write and the writer to write it with.
  1048. */
  1049. jspb.BinaryWriter.prototype.writeRepeatedMessage = function(
  1050. field, value, writerCallback) {
  1051. if (value == null) return;
  1052. for (var i = 0; i < value.length; i++) {
  1053. var bookmark = this.beginDelimited_(field);
  1054. writerCallback(value[i], this);
  1055. this.endDelimited_(bookmark);
  1056. }
  1057. };
  1058. /**
  1059. * Writes an array of group messages to the buffer.
  1060. * @template MessageType
  1061. * @param {number} field The field number.
  1062. * @param {?Array<MessageType>} value The array of messages to
  1063. * write.
  1064. * @param {function(MessageType, !jspb.BinaryWriter)} writerCallback
  1065. * Will be invoked with the value to write and the writer to write it with.
  1066. */
  1067. jspb.BinaryWriter.prototype.writeRepeatedGroup = function(
  1068. field, value, writerCallback) {
  1069. if (value == null) return;
  1070. for (var i = 0; i < value.length; i++) {
  1071. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.START_GROUP);
  1072. writerCallback(value[i], this);
  1073. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.END_GROUP);
  1074. }
  1075. };
  1076. /**
  1077. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  1078. * the buffer.
  1079. * @param {number} field The field number.
  1080. * @param {?Array<string>} value The array of hashes to write.
  1081. */
  1082. jspb.BinaryWriter.prototype.writeRepeatedFixedHash64 =
  1083. function(field, value) {
  1084. if (value == null) return;
  1085. for (var i = 0; i < value.length; i++) {
  1086. this.writeFixedHash64(field, value[i]);
  1087. }
  1088. };
  1089. /**
  1090. * Writes a repeated 64-bit hash string field (8 characters @ 8 bits of data
  1091. * each) to the buffer.
  1092. * @param {number} field The field number.
  1093. * @param {?Array<string>} value The array of hashes to write.
  1094. */
  1095. jspb.BinaryWriter.prototype.writeRepeatedVarintHash64 =
  1096. function(field, value) {
  1097. if (value == null) return;
  1098. for (var i = 0; i < value.length; i++) {
  1099. this.writeVarintHash64(field, value[i]);
  1100. }
  1101. };
  1102. /**
  1103. * Writes an array of numbers to the buffer as a packed 32-bit int field.
  1104. * @param {number} field The field number.
  1105. * @param {?Array<number>} value The array of ints to write.
  1106. */
  1107. jspb.BinaryWriter.prototype.writePackedInt32 = function(field, value) {
  1108. if (value == null || !value.length) return;
  1109. var bookmark = this.beginDelimited_(field);
  1110. for (var i = 0; i < value.length; i++) {
  1111. this.encoder_.writeSignedVarint32(value[i]);
  1112. }
  1113. this.endDelimited_(bookmark);
  1114. };
  1115. /**
  1116. * Writes an array of numbers represented as strings to the buffer as a packed
  1117. * 32-bit int field.
  1118. * @param {number} field
  1119. * @param {?Array<string>} value
  1120. */
  1121. jspb.BinaryWriter.prototype.writePackedInt32String = function(field, value) {
  1122. if (value == null || !value.length) return;
  1123. var bookmark = this.beginDelimited_(field);
  1124. for (var i = 0; i < value.length; i++) {
  1125. this.encoder_.writeSignedVarint32(parseInt(value[i], 10));
  1126. }
  1127. this.endDelimited_(bookmark);
  1128. };
  1129. /**
  1130. * Writes an array of numbers to the buffer as a packed 64-bit int field.
  1131. * @param {number} field The field number.
  1132. * @param {?Array<number>} value The array of ints to write.
  1133. */
  1134. jspb.BinaryWriter.prototype.writePackedInt64 = function(field, value) {
  1135. if (value == null || !value.length) return;
  1136. var bookmark = this.beginDelimited_(field);
  1137. for (var i = 0; i < value.length; i++) {
  1138. this.encoder_.writeSignedVarint64(value[i]);
  1139. }
  1140. this.endDelimited_(bookmark);
  1141. };
  1142. /**
  1143. * Writes an array of numbers represented as strings to the buffer as a packed
  1144. * 64-bit int field.
  1145. * @param {number} field
  1146. * @param {?Array<string>} value
  1147. */
  1148. jspb.BinaryWriter.prototype.writePackedInt64String = function(field, value) {
  1149. if (value == null || !value.length) return;
  1150. var bookmark = this.beginDelimited_(field);
  1151. for (var i = 0; i < value.length; i++) {
  1152. var num = jspb.arith.Int64.fromString(value[i]);
  1153. this.encoder_.writeSplitVarint64(num.lo, num.hi);
  1154. }
  1155. this.endDelimited_(bookmark);
  1156. };
  1157. /**
  1158. * Writes an array numbers to the buffer as a packed unsigned 32-bit int field.
  1159. * @param {number} field The field number.
  1160. * @param {?Array<number>} value The array of ints to write.
  1161. */
  1162. jspb.BinaryWriter.prototype.writePackedUint32 = function(field, value) {
  1163. if (value == null || !value.length) return;
  1164. var bookmark = this.beginDelimited_(field);
  1165. for (var i = 0; i < value.length; i++) {
  1166. this.encoder_.writeUnsignedVarint32(value[i]);
  1167. }
  1168. this.endDelimited_(bookmark);
  1169. };
  1170. /**
  1171. * Writes an array of numbers represented as strings to the buffer as a packed
  1172. * unsigned 32-bit int field.
  1173. * @param {number} field
  1174. * @param {?Array<string>} value
  1175. */
  1176. jspb.BinaryWriter.prototype.writePackedUint32String =
  1177. function(field, value) {
  1178. if (value == null || !value.length) return;
  1179. var bookmark = this.beginDelimited_(field);
  1180. for (var i = 0; i < value.length; i++) {
  1181. this.encoder_.writeUnsignedVarint32(parseInt(value[i], 10));
  1182. }
  1183. this.endDelimited_(bookmark);
  1184. };
  1185. /**
  1186. * Writes an array numbers to the buffer as a packed unsigned 64-bit int field.
  1187. * @param {number} field The field number.
  1188. * @param {?Array<number>} value The array of ints to write.
  1189. */
  1190. jspb.BinaryWriter.prototype.writePackedUint64 = function(field, value) {
  1191. if (value == null || !value.length) return;
  1192. var bookmark = this.beginDelimited_(field);
  1193. for (var i = 0; i < value.length; i++) {
  1194. this.encoder_.writeUnsignedVarint64(value[i]);
  1195. }
  1196. this.endDelimited_(bookmark);
  1197. };
  1198. /**
  1199. * Writes an array of numbers represented as strings to the buffer as a packed
  1200. * unsigned 64-bit int field.
  1201. * @param {number} field
  1202. * @param {?Array<string>} value
  1203. */
  1204. jspb.BinaryWriter.prototype.writePackedUint64String =
  1205. function(field, value) {
  1206. if (value == null || !value.length) return;
  1207. var bookmark = this.beginDelimited_(field);
  1208. for (var i = 0; i < value.length; i++) {
  1209. var num = jspb.arith.UInt64.fromString(value[i]);
  1210. this.encoder_.writeSplitVarint64(num.lo, num.hi);
  1211. }
  1212. this.endDelimited_(bookmark);
  1213. };
  1214. /**
  1215. * Writes an array numbers to the buffer as a packed signed 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.writePackedSint32 = 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_.writeZigzagVarint32(value[i]);
  1224. }
  1225. this.endDelimited_(bookmark);
  1226. };
  1227. /**
  1228. * Writes an array of numbers to the buffer as a packed signed 64-bit int field.
  1229. * @param {number} field The field number.
  1230. * @param {?Array<number>} value The array of ints to write.
  1231. */
  1232. jspb.BinaryWriter.prototype.writePackedSint64 = function(field, value) {
  1233. if (value == null || !value.length) return;
  1234. var bookmark = this.beginDelimited_(field);
  1235. for (var i = 0; i < value.length; i++) {
  1236. this.encoder_.writeZigzagVarint64(value[i]);
  1237. }
  1238. this.endDelimited_(bookmark);
  1239. };
  1240. /**
  1241. * Writes an array of decimal strings to the buffer as a packed signed 64-bit
  1242. * int field.
  1243. * @param {number} field The field number.
  1244. * @param {?Array<string>} value The array of decimal strings to write.
  1245. */
  1246. jspb.BinaryWriter.prototype.writePackedSint64String = 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. // TODO(haberman): make lossless
  1251. this.encoder_.writeZigzagVarint64(parseInt(value[i], 10));
  1252. }
  1253. this.endDelimited_(bookmark);
  1254. };
  1255. /**
  1256. * Writes an array of numbers to the buffer as a packed fixed32 field.
  1257. * @param {number} field The field number.
  1258. * @param {?Array<number>} value The array of ints to write.
  1259. */
  1260. jspb.BinaryWriter.prototype.writePackedFixed32 = function(field, value) {
  1261. if (value == null || !value.length) return;
  1262. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1263. this.encoder_.writeUnsignedVarint32(value.length * 4);
  1264. for (var i = 0; i < value.length; i++) {
  1265. this.encoder_.writeUint32(value[i]);
  1266. }
  1267. };
  1268. /**
  1269. * Writes an array of numbers to the buffer as a packed fixed64 field.
  1270. * @param {number} field The field number.
  1271. * @param {?Array<number>} value The array of ints to write.
  1272. */
  1273. jspb.BinaryWriter.prototype.writePackedFixed64 = function(field, value) {
  1274. if (value == null || !value.length) return;
  1275. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1276. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1277. for (var i = 0; i < value.length; i++) {
  1278. this.encoder_.writeUint64(value[i]);
  1279. }
  1280. };
  1281. /**
  1282. * Writes an array of numbers represented as strings to the buffer as a packed
  1283. * fixed64 field.
  1284. * @param {number} field The field number.
  1285. * @param {?Array<string>} value The array of strings to write.
  1286. */
  1287. jspb.BinaryWriter.prototype.writePackedFixed64String = function(field, value) {
  1288. if (value == null || !value.length) return;
  1289. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1290. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1291. for (var i = 0; i < value.length; i++) {
  1292. var num = jspb.arith.UInt64.fromString(value[i]);
  1293. this.encoder_.writeSplitFixed64(num.lo, num.hi);
  1294. }
  1295. };
  1296. /**
  1297. * Writes an array of numbers to the buffer as a packed sfixed32 field.
  1298. * @param {number} field The field number.
  1299. * @param {?Array<number>} value The array of ints to write.
  1300. */
  1301. jspb.BinaryWriter.prototype.writePackedSfixed32 = function(field, value) {
  1302. if (value == null || !value.length) return;
  1303. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1304. this.encoder_.writeUnsignedVarint32(value.length * 4);
  1305. for (var i = 0; i < value.length; i++) {
  1306. this.encoder_.writeInt32(value[i]);
  1307. }
  1308. };
  1309. /**
  1310. * Writes an array of numbers to the buffer as a packed sfixed64 field.
  1311. * @param {number} field The field number.
  1312. * @param {?Array<number>} value The array of ints to write.
  1313. */
  1314. jspb.BinaryWriter.prototype.writePackedSfixed64 = function(field, value) {
  1315. if (value == null || !value.length) return;
  1316. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1317. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1318. for (var i = 0; i < value.length; i++) {
  1319. this.encoder_.writeInt64(value[i]);
  1320. }
  1321. };
  1322. /**
  1323. * Writes an array of numbers to the buffer as a packed sfixed64 field.
  1324. * @param {number} field The field number.
  1325. * @param {?Array<string>} value The array of decimal strings to write.
  1326. */
  1327. jspb.BinaryWriter.prototype.writePackedSfixed64String = function(field, value) {
  1328. if (value == null || !value.length) return;
  1329. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1330. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1331. for (var i = 0; i < value.length; i++) {
  1332. this.encoder_.writeInt64String(value[i]);
  1333. }
  1334. };
  1335. /**
  1336. * Writes an array of numbers to the buffer as a packed float field.
  1337. * @param {number} field The field number.
  1338. * @param {?Array<number>} value The array of ints to write.
  1339. */
  1340. jspb.BinaryWriter.prototype.writePackedFloat = function(field, value) {
  1341. if (value == null || !value.length) return;
  1342. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1343. this.encoder_.writeUnsignedVarint32(value.length * 4);
  1344. for (var i = 0; i < value.length; i++) {
  1345. this.encoder_.writeFloat(value[i]);
  1346. }
  1347. };
  1348. /**
  1349. * Writes an array of numbers to the buffer as a packed double field.
  1350. * @param {number} field The field number.
  1351. * @param {?Array<number>} value The array of ints to write.
  1352. */
  1353. jspb.BinaryWriter.prototype.writePackedDouble = function(field, value) {
  1354. if (value == null || !value.length) return;
  1355. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1356. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1357. for (var i = 0; i < value.length; i++) {
  1358. this.encoder_.writeDouble(value[i]);
  1359. }
  1360. };
  1361. /**
  1362. * Writes an array of booleans to the buffer as a packed bool field.
  1363. * @param {number} field The field number.
  1364. * @param {?Array<boolean>} value The array of ints to write.
  1365. */
  1366. jspb.BinaryWriter.prototype.writePackedBool = function(field, value) {
  1367. if (value == null || !value.length) return;
  1368. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1369. this.encoder_.writeUnsignedVarint32(value.length);
  1370. for (var i = 0; i < value.length; i++) {
  1371. this.encoder_.writeBool(value[i]);
  1372. }
  1373. };
  1374. /**
  1375. * Writes an array of enums to the buffer as a packed enum field.
  1376. * @param {number} field The field number.
  1377. * @param {?Array<number>} value The array of ints to write.
  1378. */
  1379. jspb.BinaryWriter.prototype.writePackedEnum = function(field, value) {
  1380. if (value == null || !value.length) return;
  1381. var bookmark = this.beginDelimited_(field);
  1382. for (var i = 0; i < value.length; i++) {
  1383. this.encoder_.writeEnum(value[i]);
  1384. }
  1385. this.endDelimited_(bookmark);
  1386. };
  1387. /**
  1388. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  1389. * the buffer.
  1390. * @param {number} field The field number.
  1391. * @param {?Array<string>} value The array of hashes to write.
  1392. */
  1393. jspb.BinaryWriter.prototype.writePackedFixedHash64 = function(field, value) {
  1394. if (value == null || !value.length) return;
  1395. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1396. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1397. for (var i = 0; i < value.length; i++) {
  1398. this.encoder_.writeFixedHash64(value[i]);
  1399. }
  1400. };
  1401. /**
  1402. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  1403. * the buffer.
  1404. * @param {number} field The field number.
  1405. * @param {?Array<string>} value The array of hashes to write.
  1406. */
  1407. jspb.BinaryWriter.prototype.writePackedVarintHash64 = function(field, value) {
  1408. if (value == null || !value.length) return;
  1409. var bookmark = this.beginDelimited_(field);
  1410. for (var i = 0; i < value.length; i++) {
  1411. this.encoder_.writeVarintHash64(value[i]);
  1412. }
  1413. this.endDelimited_(bookmark);
  1414. };