writer.js 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  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 bas64-encoded string.
  205. * @return {string}
  206. */
  207. jspb.BinaryWriter.prototype.getResultBase64String = function() {
  208. return goog.crypt.base64.encodeByteArray(this.getResultBuffer());
  209. };
  210. /**
  211. * Begins a new sub-message. The client must call endSubMessage() when they're
  212. * done.
  213. * TODO(aappleby): Deprecated. Move callers to writeMessage().
  214. * @param {number} field The field number of the sub-message.
  215. */
  216. jspb.BinaryWriter.prototype.beginSubMessage = function(field) {
  217. this.bookmarks_.push(this.beginDelimited_(field));
  218. };
  219. /**
  220. * Finishes a sub-message and packs it into the parent messages' buffer.
  221. * TODO(aappleby): Deprecated. Move callers to writeMessage().
  222. */
  223. jspb.BinaryWriter.prototype.endSubMessage = function() {
  224. goog.asserts.assert(this.bookmarks_.length >= 0);
  225. this.endDelimited_(this.bookmarks_.pop());
  226. };
  227. /**
  228. * Encodes a (field number, wire type) tuple into a wire-format field header
  229. * and stores it in the buffer as a varint.
  230. * @param {number} field The field number.
  231. * @param {number} wireType The wire-type of the field, as specified in the
  232. * protocol buffer documentation.
  233. * @private
  234. */
  235. jspb.BinaryWriter.prototype.writeFieldHeader_ =
  236. function(field, wireType) {
  237. goog.asserts.assert(field >= 1 && field == Math.floor(field));
  238. var x = field * 8 + wireType;
  239. this.encoder_.writeUnsignedVarint32(x);
  240. };
  241. /**
  242. * Writes a field of any valid scalar type to the binary stream.
  243. * @param {jspb.BinaryConstants.FieldType} fieldType
  244. * @param {number} field
  245. * @param {jspb.AnyFieldType} value
  246. */
  247. jspb.BinaryWriter.prototype.writeAny = function(fieldType, field, value) {
  248. var fieldTypes = jspb.BinaryConstants.FieldType;
  249. switch (fieldType) {
  250. case fieldTypes.DOUBLE:
  251. this.writeDouble(field, /** @type {number} */(value));
  252. return;
  253. case fieldTypes.FLOAT:
  254. this.writeFloat(field, /** @type {number} */(value));
  255. return;
  256. case fieldTypes.INT64:
  257. this.writeInt64(field, /** @type {number} */(value));
  258. return;
  259. case fieldTypes.UINT64:
  260. this.writeUint64(field, /** @type {number} */(value));
  261. return;
  262. case fieldTypes.INT32:
  263. this.writeInt32(field, /** @type {number} */(value));
  264. return;
  265. case fieldTypes.FIXED64:
  266. this.writeFixed64(field, /** @type {number} */(value));
  267. return;
  268. case fieldTypes.FIXED32:
  269. this.writeFixed32(field, /** @type {number} */(value));
  270. return;
  271. case fieldTypes.BOOL:
  272. this.writeBool(field, /** @type {boolean} */(value));
  273. return;
  274. case fieldTypes.STRING:
  275. this.writeString(field, /** @type {string} */(value));
  276. return;
  277. case fieldTypes.GROUP:
  278. goog.asserts.fail('Group field type not supported in writeAny()');
  279. return;
  280. case fieldTypes.MESSAGE:
  281. goog.asserts.fail('Message field type not supported in writeAny()');
  282. return;
  283. case fieldTypes.BYTES:
  284. this.writeBytes(field, /** @type {?Uint8Array} */(value));
  285. return;
  286. case fieldTypes.UINT32:
  287. this.writeUint32(field, /** @type {number} */(value));
  288. return;
  289. case fieldTypes.ENUM:
  290. this.writeEnum(field, /** @type {number} */(value));
  291. return;
  292. case fieldTypes.SFIXED32:
  293. this.writeSfixed32(field, /** @type {number} */(value));
  294. return;
  295. case fieldTypes.SFIXED64:
  296. this.writeSfixed64(field, /** @type {number} */(value));
  297. return;
  298. case fieldTypes.SINT32:
  299. this.writeSint32(field, /** @type {number} */(value));
  300. return;
  301. case fieldTypes.SINT64:
  302. this.writeSint64(field, /** @type {number} */(value));
  303. return;
  304. case fieldTypes.FHASH64:
  305. this.writeFixedHash64(field, /** @type {string} */(value));
  306. return;
  307. case fieldTypes.VHASH64:
  308. this.writeVarintHash64(field, /** @type {string} */(value));
  309. return;
  310. default:
  311. goog.asserts.fail('Invalid field type in writeAny()');
  312. return;
  313. }
  314. };
  315. /**
  316. * Writes a varint field to the buffer without range checking.
  317. * @param {number} field The field number.
  318. * @param {number?} value The value to write.
  319. * @private
  320. */
  321. jspb.BinaryWriter.prototype.writeUnsignedVarint32_ = function(field, value) {
  322. if (value == null) return;
  323. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  324. this.encoder_.writeUnsignedVarint32(value);
  325. };
  326. /**
  327. * Writes a varint field to the buffer without range checking.
  328. * @param {number} field The field number.
  329. * @param {number?} value The value to write.
  330. * @private
  331. */
  332. jspb.BinaryWriter.prototype.writeSignedVarint32_ = function(field, value) {
  333. if (value == null) return;
  334. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  335. this.encoder_.writeSignedVarint32(value);
  336. };
  337. /**
  338. * Writes a varint field to the buffer without range checking.
  339. * @param {number} field The field number.
  340. * @param {number?} value The value to write.
  341. * @private
  342. */
  343. jspb.BinaryWriter.prototype.writeUnsignedVarint64_ = function(field, value) {
  344. if (value == null) return;
  345. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  346. this.encoder_.writeUnsignedVarint64(value);
  347. };
  348. /**
  349. * Writes a varint field to the buffer without range checking.
  350. * @param {number} field The field number.
  351. * @param {number?} value The value to write.
  352. * @private
  353. */
  354. jspb.BinaryWriter.prototype.writeSignedVarint64_ = function(field, value) {
  355. if (value == null) return;
  356. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  357. this.encoder_.writeSignedVarint64(value);
  358. };
  359. /**
  360. * Writes a zigzag varint field to the buffer without range checking.
  361. * @param {number} field The field number.
  362. * @param {number?} value The value to write.
  363. * @private
  364. */
  365. jspb.BinaryWriter.prototype.writeZigzagVarint32_ = function(field, value) {
  366. if (value == null) return;
  367. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  368. this.encoder_.writeZigzagVarint32(value);
  369. };
  370. /**
  371. * Writes a zigzag varint field to the buffer without range checking.
  372. * @param {number} field The field number.
  373. * @param {number?} value The value to write.
  374. * @private
  375. */
  376. jspb.BinaryWriter.prototype.writeZigzagVarint64_ = function(field, value) {
  377. if (value == null) return;
  378. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  379. this.encoder_.writeZigzagVarint64(value);
  380. };
  381. /**
  382. * Writes a zigzag varint field to the buffer without range checking.
  383. * @param {number} field The field number.
  384. * @param {string?} value The value to write.
  385. * @private
  386. */
  387. jspb.BinaryWriter.prototype.writeZigzagVarint64String_ = function(
  388. field, value) {
  389. if (value == null) return;
  390. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  391. this.encoder_.writeZigzagVarint64String(value);
  392. };
  393. /**
  394. * Writes an int32 field to the buffer. Numbers outside the range [-2^31,2^31)
  395. * will be truncated.
  396. * @param {number} field The field number.
  397. * @param {number?} value The value to write.
  398. */
  399. jspb.BinaryWriter.prototype.writeInt32 = function(field, value) {
  400. if (value == null) return;
  401. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
  402. (value < jspb.BinaryConstants.TWO_TO_31));
  403. this.writeSignedVarint32_(field, value);
  404. };
  405. /**
  406. * Writes an int32 field represented as a string to the buffer. Numbers outside
  407. * the range [-2^31,2^31) will be truncated.
  408. * @param {number} field The field number.
  409. * @param {string?} value The value to write.
  410. */
  411. jspb.BinaryWriter.prototype.writeInt32String = function(field, value) {
  412. if (value == null) return;
  413. var intValue = /** {number} */ parseInt(value, 10);
  414. goog.asserts.assert((intValue >= -jspb.BinaryConstants.TWO_TO_31) &&
  415. (intValue < jspb.BinaryConstants.TWO_TO_31));
  416. this.writeSignedVarint32_(field, intValue);
  417. };
  418. /**
  419. * Writes an int64 field to the buffer. Numbers outside the range [-2^63,2^63)
  420. * will be truncated.
  421. * @param {number} field The field number.
  422. * @param {number?} value The value to write.
  423. */
  424. jspb.BinaryWriter.prototype.writeInt64 = function(field, value) {
  425. if (value == null) return;
  426. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
  427. (value < jspb.BinaryConstants.TWO_TO_63));
  428. this.writeSignedVarint64_(field, value);
  429. };
  430. /**
  431. * Writes a int64 field (with value as a string) to the buffer.
  432. * @param {number} field The field number.
  433. * @param {string?} value The value to write.
  434. */
  435. jspb.BinaryWriter.prototype.writeInt64String = function(field, value) {
  436. if (value == null) return;
  437. var num = jspb.arith.Int64.fromString(value);
  438. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  439. this.encoder_.writeSplitVarint64(num.lo, num.hi);
  440. };
  441. /**
  442. * Writes a uint32 field to the buffer. Numbers outside the range [0,2^32)
  443. * will be truncated.
  444. * @param {number} field The field number.
  445. * @param {number?} value The value to write.
  446. */
  447. jspb.BinaryWriter.prototype.writeUint32 = function(field, value) {
  448. if (value == null) return;
  449. goog.asserts.assert((value >= 0) &&
  450. (value < jspb.BinaryConstants.TWO_TO_32));
  451. this.writeUnsignedVarint32_(field, value);
  452. };
  453. /**
  454. * Writes a uint32 field represented as a string to the buffer. Numbers outside
  455. * the range [0,2^32) will be truncated.
  456. * @param {number} field The field number.
  457. * @param {string?} value The value to write.
  458. */
  459. jspb.BinaryWriter.prototype.writeUint32String = function(field, value) {
  460. if (value == null) return;
  461. var intValue = /** {number} */ parseInt(value, 10);
  462. goog.asserts.assert((intValue >= 0) &&
  463. (intValue < jspb.BinaryConstants.TWO_TO_32));
  464. this.writeUnsignedVarint32_(field, intValue);
  465. };
  466. /**
  467. * Writes a uint64 field to the buffer. Numbers outside the range [0,2^64)
  468. * will be truncated.
  469. * @param {number} field The field number.
  470. * @param {number?} value The value to write.
  471. */
  472. jspb.BinaryWriter.prototype.writeUint64 = function(field, value) {
  473. if (value == null) return;
  474. goog.asserts.assert((value >= 0) &&
  475. (value < jspb.BinaryConstants.TWO_TO_64));
  476. this.writeUnsignedVarint64_(field, value);
  477. };
  478. /**
  479. * Writes a uint64 field (with value as a string) to the buffer.
  480. * @param {number} field The field number.
  481. * @param {string?} value The value to write.
  482. */
  483. jspb.BinaryWriter.prototype.writeUint64String = function(field, value) {
  484. if (value == null) return;
  485. var num = jspb.arith.UInt64.fromString(value);
  486. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  487. this.encoder_.writeSplitVarint64(num.lo, num.hi);
  488. };
  489. /**
  490. * Writes a sint32 field to the buffer. Numbers outside the range [-2^31,2^31)
  491. * will be truncated.
  492. * @param {number} field The field number.
  493. * @param {number?} value The value to write.
  494. */
  495. jspb.BinaryWriter.prototype.writeSint32 = function(field, value) {
  496. if (value == null) return;
  497. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
  498. (value < jspb.BinaryConstants.TWO_TO_31));
  499. this.writeZigzagVarint32_(field, value);
  500. };
  501. /**
  502. * Writes a sint64 field to the buffer. Numbers outside the range [-2^63,2^63)
  503. * will be truncated.
  504. * @param {number} field The field number.
  505. * @param {number?} value The value to write.
  506. */
  507. jspb.BinaryWriter.prototype.writeSint64 = function(field, value) {
  508. if (value == null) return;
  509. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
  510. (value < jspb.BinaryConstants.TWO_TO_63));
  511. this.writeZigzagVarint64_(field, value);
  512. };
  513. /**
  514. * Writes a sint64 field to the buffer. Numbers outside the range [-2^63,2^63)
  515. * will be truncated.
  516. * @param {number} field The field number.
  517. * @param {string?} value The decimal string to write.
  518. */
  519. jspb.BinaryWriter.prototype.writeSint64String = function(field, value) {
  520. if (value == null) return;
  521. goog.asserts.assert((+value >= -jspb.BinaryConstants.TWO_TO_63) &&
  522. (+value < jspb.BinaryConstants.TWO_TO_63));
  523. this.writeZigzagVarint64String_(field, value);
  524. };
  525. /**
  526. * Writes a fixed32 field to the buffer. Numbers outside the range [0,2^32)
  527. * will be truncated.
  528. * @param {number} field The field number.
  529. * @param {number?} value The value to write.
  530. */
  531. jspb.BinaryWriter.prototype.writeFixed32 = function(field, value) {
  532. if (value == null) return;
  533. goog.asserts.assert((value >= 0) &&
  534. (value < jspb.BinaryConstants.TWO_TO_32));
  535. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32);
  536. this.encoder_.writeUint32(value);
  537. };
  538. /**
  539. * Writes a fixed64 field to the buffer. Numbers outside the range [0,2^64)
  540. * will be truncated.
  541. * @param {number} field The field number.
  542. * @param {number?} value The value to write.
  543. */
  544. jspb.BinaryWriter.prototype.writeFixed64 = function(field, value) {
  545. if (value == null) return;
  546. goog.asserts.assert((value >= 0) &&
  547. (value < jspb.BinaryConstants.TWO_TO_64));
  548. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  549. this.encoder_.writeUint64(value);
  550. };
  551. /**
  552. * Writes a fixed64 field (with value as a string) to the buffer.
  553. * @param {number} field The field number.
  554. * @param {string?} value The value to write.
  555. */
  556. jspb.BinaryWriter.prototype.writeFixed64String = function(field, value) {
  557. if (value == null) return;
  558. var num = jspb.arith.UInt64.fromString(value);
  559. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  560. this.encoder_.writeSplitFixed64(num.lo, num.hi);
  561. };
  562. /**
  563. * Writes a sfixed32 field to the buffer. Numbers outside the range
  564. * [-2^31,2^31) will be truncated.
  565. * @param {number} field The field number.
  566. * @param {number?} value The value to write.
  567. */
  568. jspb.BinaryWriter.prototype.writeSfixed32 = function(field, value) {
  569. if (value == null) return;
  570. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
  571. (value < jspb.BinaryConstants.TWO_TO_31));
  572. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32);
  573. this.encoder_.writeInt32(value);
  574. };
  575. /**
  576. * Writes a sfixed64 field to the buffer. Numbers outside the range
  577. * [-2^63,2^63) will be truncated.
  578. * @param {number} field The field number.
  579. * @param {number?} value The value to write.
  580. */
  581. jspb.BinaryWriter.prototype.writeSfixed64 = function(field, value) {
  582. if (value == null) return;
  583. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
  584. (value < jspb.BinaryConstants.TWO_TO_63));
  585. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  586. this.encoder_.writeInt64(value);
  587. };
  588. /**
  589. * Writes a sfixed64 string field to the buffer. Numbers outside the range
  590. * [-2^63,2^63) will be truncated.
  591. * @param {number} field The field number.
  592. * @param {string?} value The value to write.
  593. */
  594. jspb.BinaryWriter.prototype.writeSfixed64String = function(field, value) {
  595. if (value == null) return;
  596. var num = jspb.arith.Int64.fromString(value);
  597. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  598. this.encoder_.writeSplitFixed64(num.lo, num.hi);
  599. };
  600. /**
  601. * Writes a single-precision floating point field to the buffer. Numbers
  602. * requiring more than 32 bits of precision will be truncated.
  603. * @param {number} field The field number.
  604. * @param {number?} value The value to write.
  605. */
  606. jspb.BinaryWriter.prototype.writeFloat = function(field, value) {
  607. if (value == null) return;
  608. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32);
  609. this.encoder_.writeFloat(value);
  610. };
  611. /**
  612. * Writes a double-precision floating point field to the buffer. As this is the
  613. * native format used by JavaScript, no precision will be lost.
  614. * @param {number} field The field number.
  615. * @param {number?} value The value to write.
  616. */
  617. jspb.BinaryWriter.prototype.writeDouble = function(field, value) {
  618. if (value == null) return;
  619. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  620. this.encoder_.writeDouble(value);
  621. };
  622. /**
  623. * Writes a boolean field to the buffer.
  624. * @param {number} field The field number.
  625. * @param {boolean?} value The value to write.
  626. */
  627. jspb.BinaryWriter.prototype.writeBool = function(field, value) {
  628. if (value == null) return;
  629. goog.asserts.assert(goog.isBoolean(value));
  630. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  631. this.encoder_.writeBool(value);
  632. };
  633. /**
  634. * Writes an enum field to the buffer.
  635. * @param {number} field The field number.
  636. * @param {number?} value The value to write.
  637. */
  638. jspb.BinaryWriter.prototype.writeEnum = function(field, value) {
  639. if (value == null) return;
  640. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
  641. (value < jspb.BinaryConstants.TWO_TO_31));
  642. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  643. this.encoder_.writeSignedVarint32(value);
  644. };
  645. /**
  646. * Writes a string field to the buffer.
  647. * @param {number} field The field number.
  648. * @param {string?} value The string to write.
  649. */
  650. jspb.BinaryWriter.prototype.writeString = function(field, value) {
  651. if (value == null) return;
  652. var bookmark = this.beginDelimited_(field);
  653. this.encoder_.writeString(value);
  654. this.endDelimited_(bookmark);
  655. };
  656. /**
  657. * Writes an arbitrary byte field to the buffer. Note - to match the behavior
  658. * of the C++ implementation, empty byte arrays _are_ serialized.
  659. * @param {number} field The field number.
  660. * @param {?jspb.ByteSource} value The array of bytes to write.
  661. */
  662. jspb.BinaryWriter.prototype.writeBytes = function(field, value) {
  663. if (value == null) return;
  664. var bytes = jspb.utils.byteSourceToUint8Array(value);
  665. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  666. this.encoder_.writeUnsignedVarint32(bytes.length);
  667. this.appendUint8Array_(bytes);
  668. };
  669. /**
  670. * Writes a message to the buffer.
  671. * @param {number} field The field number.
  672. * @param {?MessageType} value The message to write.
  673. * @param {function(MessageTypeNonNull, !jspb.BinaryWriter)} writerCallback
  674. * Will be invoked with the value to write and the writer to write it with.
  675. * @template MessageType
  676. * Use go/closure-ttl to declare a non-nullable version of MessageType. Replace
  677. * the null in blah|null with none. This is necessary because the compiler will
  678. * infer MessageType to be nullable if the value parameter is nullable.
  679. * @template MessageTypeNonNull :=
  680. * cond(isUnknown(MessageType), unknown(),
  681. * mapunion(MessageType, (X) =>
  682. * cond(eq(X, 'null'), none(), X)))
  683. * =:
  684. */
  685. jspb.BinaryWriter.prototype.writeMessage = function(
  686. field, value, writerCallback) {
  687. if (value == null) return;
  688. var bookmark = this.beginDelimited_(field);
  689. writerCallback(value, this);
  690. this.endDelimited_(bookmark);
  691. };
  692. /**
  693. * Writes a group message to the buffer.
  694. *
  695. * @param {number} field The field number.
  696. * @param {?MessageType} value The message to write, wrapped with START_GROUP /
  697. * END_GROUP tags. Will be a no-op if 'value' is null.
  698. * @param {function(MessageTypeNonNull, !jspb.BinaryWriter)} writerCallback
  699. * Will be invoked with the value to write and the writer to write it with.
  700. * @template MessageType
  701. * Use go/closure-ttl to declare a non-nullable version of MessageType. Replace
  702. * the null in blah|null with none. This is necessary because the compiler will
  703. * infer MessageType to be nullable if the value parameter is nullable.
  704. * @template MessageTypeNonNull :=
  705. * cond(isUnknown(MessageType), unknown(),
  706. * mapunion(MessageType, (X) =>
  707. * cond(eq(X, 'null'), none(), X)))
  708. * =:
  709. */
  710. jspb.BinaryWriter.prototype.writeGroup = function(
  711. field, value, writerCallback) {
  712. if (value == null) return;
  713. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.START_GROUP);
  714. writerCallback(value, this);
  715. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.END_GROUP);
  716. };
  717. /**
  718. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  719. * the buffer.
  720. * @param {number} field The field number.
  721. * @param {string?} value The hash string.
  722. */
  723. jspb.BinaryWriter.prototype.writeFixedHash64 = function(field, value) {
  724. if (value == null) return;
  725. goog.asserts.assert(value.length == 8);
  726. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  727. this.encoder_.writeFixedHash64(value);
  728. };
  729. /**
  730. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  731. * the buffer.
  732. * @param {number} field The field number.
  733. * @param {string?} value The hash string.
  734. */
  735. jspb.BinaryWriter.prototype.writeVarintHash64 = function(field, value) {
  736. if (value == null) return;
  737. goog.asserts.assert(value.length == 8);
  738. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  739. this.encoder_.writeVarintHash64(value);
  740. };
  741. /**
  742. * Writes an array of numbers to the buffer as a repeated 32-bit int field.
  743. * @param {number} field The field number.
  744. * @param {?Array.<number>} value The array of ints to write.
  745. */
  746. jspb.BinaryWriter.prototype.writeRepeatedInt32 = function(field, value) {
  747. if (value == null) return;
  748. for (var i = 0; i < value.length; i++) {
  749. this.writeSignedVarint32_(field, value[i]);
  750. }
  751. };
  752. /**
  753. * Writes an array of numbers formatted as strings to the buffer as a repeated
  754. * 32-bit int field.
  755. * @param {number} field The field number.
  756. * @param {?Array.<string>} value The array of ints to write.
  757. */
  758. jspb.BinaryWriter.prototype.writeRepeatedInt32String = function(field, value) {
  759. if (value == null) return;
  760. for (var i = 0; i < value.length; i++) {
  761. this.writeInt32String(field, value[i]);
  762. }
  763. };
  764. /**
  765. * Writes an array of numbers to the buffer as a repeated 64-bit int field.
  766. * @param {number} field The field number.
  767. * @param {?Array.<number>} value The array of ints to write.
  768. */
  769. jspb.BinaryWriter.prototype.writeRepeatedInt64 = function(field, value) {
  770. if (value == null) return;
  771. for (var i = 0; i < value.length; i++) {
  772. this.writeSignedVarint64_(field, value[i]);
  773. }
  774. };
  775. /**
  776. * Writes an array of numbers formatted as strings to the buffer as a repeated
  777. * 64-bit int field.
  778. * @param {number} field The field number.
  779. * @param {?Array.<string>} value The array of ints to write.
  780. */
  781. jspb.BinaryWriter.prototype.writeRepeatedInt64String = function(field, value) {
  782. if (value == null) return;
  783. for (var i = 0; i < value.length; i++) {
  784. this.writeInt64String(field, value[i]);
  785. }
  786. };
  787. /**
  788. * Writes an array numbers to the buffer as a repeated unsigned 32-bit int
  789. * field.
  790. * @param {number} field The field number.
  791. * @param {?Array.<number>} value The array of ints to write.
  792. */
  793. jspb.BinaryWriter.prototype.writeRepeatedUint32 = function(field, value) {
  794. if (value == null) return;
  795. for (var i = 0; i < value.length; i++) {
  796. this.writeUnsignedVarint32_(field, value[i]);
  797. }
  798. };
  799. /**
  800. * Writes an array of numbers formatted as strings to the buffer as a repeated
  801. * unsigned 32-bit int field.
  802. * @param {number} field The field number.
  803. * @param {?Array.<string>} value The array of ints to write.
  804. */
  805. jspb.BinaryWriter.prototype.writeRepeatedUint32String = function(field, value) {
  806. if (value == null) return;
  807. for (var i = 0; i < value.length; i++) {
  808. this.writeUint32String(field, value[i]);
  809. }
  810. };
  811. /**
  812. * Writes an array numbers to the buffer as a repeated unsigned 64-bit int
  813. * field.
  814. * @param {number} field The field number.
  815. * @param {?Array.<number>} value The array of ints to write.
  816. */
  817. jspb.BinaryWriter.prototype.writeRepeatedUint64 = function(field, value) {
  818. if (value == null) return;
  819. for (var i = 0; i < value.length; i++) {
  820. this.writeUnsignedVarint64_(field, value[i]);
  821. }
  822. };
  823. /**
  824. * Writes an array of numbers formatted as strings to the buffer as a repeated
  825. * unsigned 64-bit int field.
  826. * @param {number} field The field number.
  827. * @param {?Array.<string>} value The array of ints to write.
  828. */
  829. jspb.BinaryWriter.prototype.writeRepeatedUint64String = function(field, value) {
  830. if (value == null) return;
  831. for (var i = 0; i < value.length; i++) {
  832. this.writeUint64String(field, value[i]);
  833. }
  834. };
  835. /**
  836. * Writes an array numbers to the buffer as a repeated signed 32-bit int field.
  837. * @param {number} field The field number.
  838. * @param {?Array.<number>} value The array of ints to write.
  839. */
  840. jspb.BinaryWriter.prototype.writeRepeatedSint32 = function(field, value) {
  841. if (value == null) return;
  842. for (var i = 0; i < value.length; i++) {
  843. this.writeZigzagVarint32_(field, value[i]);
  844. }
  845. };
  846. /**
  847. * Writes an array numbers to the buffer as a repeated signed 64-bit int field.
  848. * @param {number} field The field number.
  849. * @param {?Array.<number>} value The array of ints to write.
  850. */
  851. jspb.BinaryWriter.prototype.writeRepeatedSint64 = function(field, value) {
  852. if (value == null) return;
  853. for (var i = 0; i < value.length; i++) {
  854. this.writeZigzagVarint64_(field, value[i]);
  855. }
  856. };
  857. /**
  858. * Writes an array numbers to the buffer as a repeated signed 64-bit int field.
  859. * @param {number} field The field number.
  860. * @param {?Array.<string>} value The array of ints to write.
  861. */
  862. jspb.BinaryWriter.prototype.writeRepeatedSint64String = function(field, value) {
  863. if (value == null) return;
  864. for (var i = 0; i < value.length; i++) {
  865. this.writeZigzagVarint64String_(field, value[i]);
  866. }
  867. };
  868. /**
  869. * Writes an array of numbers to the buffer as a repeated fixed32 field. This
  870. * works for both signed and unsigned fixed32s.
  871. * @param {number} field The field number.
  872. * @param {?Array.<number>} value The array of ints to write.
  873. */
  874. jspb.BinaryWriter.prototype.writeRepeatedFixed32 = function(field, value) {
  875. if (value == null) return;
  876. for (var i = 0; i < value.length; i++) {
  877. this.writeFixed32(field, value[i]);
  878. }
  879. };
  880. /**
  881. * Writes an array of numbers to the buffer as a repeated fixed64 field. This
  882. * works for both signed and unsigned fixed64s.
  883. * @param {number} field The field number.
  884. * @param {?Array.<number>} value The array of ints to write.
  885. */
  886. jspb.BinaryWriter.prototype.writeRepeatedFixed64 = function(field, value) {
  887. if (value == null) return;
  888. for (var i = 0; i < value.length; i++) {
  889. this.writeFixed64(field, value[i]);
  890. }
  891. };
  892. /**
  893. * Writes an array of numbers to the buffer as a repeated fixed64 field. This
  894. * works for both signed and unsigned fixed64s.
  895. * @param {number} field The field number.
  896. * @param {?Array.<string>} value The array of decimal strings to write.
  897. */
  898. jspb.BinaryWriter.prototype.writeRepeatedFixed64String = function(
  899. field, value) {
  900. if (value == null) return;
  901. for (var i = 0; i < value.length; i++) {
  902. this.writeFixed64String(field, value[i]);
  903. }
  904. };
  905. /**
  906. * Writes an array of numbers to the buffer as a repeated sfixed32 field.
  907. * @param {number} field The field number.
  908. * @param {?Array.<number>} value The array of ints to write.
  909. */
  910. jspb.BinaryWriter.prototype.writeRepeatedSfixed32 = function(field, value) {
  911. if (value == null) return;
  912. for (var i = 0; i < value.length; i++) {
  913. this.writeSfixed32(field, value[i]);
  914. }
  915. };
  916. /**
  917. * Writes an array of numbers to the buffer as a repeated sfixed64 field.
  918. * @param {number} field The field number.
  919. * @param {?Array.<number>} value The array of ints to write.
  920. */
  921. jspb.BinaryWriter.prototype.writeRepeatedSfixed64 = function(field, value) {
  922. if (value == null) return;
  923. for (var i = 0; i < value.length; i++) {
  924. this.writeSfixed64(field, value[i]);
  925. }
  926. };
  927. /**
  928. * Writes an array of decimal strings to the buffer as a repeated sfixed64
  929. * field.
  930. * @param {number} field The field number.
  931. * @param {?Array.<string>} value The array of decimal strings to write.
  932. */
  933. jspb.BinaryWriter.prototype.writeRepeatedSfixed64String = function(field, value) {
  934. if (value == null) return;
  935. for (var i = 0; i < value.length; i++) {
  936. this.writeSfixed64String(field, value[i]);
  937. }
  938. };
  939. /**
  940. * Writes an array of numbers to the buffer as a repeated float field.
  941. * @param {number} field The field number.
  942. * @param {?Array.<number>} value The array of ints to write.
  943. */
  944. jspb.BinaryWriter.prototype.writeRepeatedFloat = function(field, value) {
  945. if (value == null) return;
  946. for (var i = 0; i < value.length; i++) {
  947. this.writeFloat(field, value[i]);
  948. }
  949. };
  950. /**
  951. * Writes an array of numbers to the buffer as a repeated double field.
  952. * @param {number} field The field number.
  953. * @param {?Array.<number>} value The array of ints to write.
  954. */
  955. jspb.BinaryWriter.prototype.writeRepeatedDouble = function(field, value) {
  956. if (value == null) return;
  957. for (var i = 0; i < value.length; i++) {
  958. this.writeDouble(field, value[i]);
  959. }
  960. };
  961. /**
  962. * Writes an array of booleans to the buffer as a repeated bool field.
  963. * @param {number} field The field number.
  964. * @param {?Array.<boolean>} value The array of ints to write.
  965. */
  966. jspb.BinaryWriter.prototype.writeRepeatedBool = function(field, value) {
  967. if (value == null) return;
  968. for (var i = 0; i < value.length; i++) {
  969. this.writeBool(field, value[i]);
  970. }
  971. };
  972. /**
  973. * Writes an array of enums to the buffer as a repeated enum field.
  974. * @param {number} field The field number.
  975. * @param {?Array.<number>} value The array of ints to write.
  976. */
  977. jspb.BinaryWriter.prototype.writeRepeatedEnum = function(field, value) {
  978. if (value == null) return;
  979. for (var i = 0; i < value.length; i++) {
  980. this.writeEnum(field, value[i]);
  981. }
  982. };
  983. /**
  984. * Writes an array of strings to the buffer as a repeated string field.
  985. * @param {number} field The field number.
  986. * @param {?Array.<string>} value The array of strings to write.
  987. */
  988. jspb.BinaryWriter.prototype.writeRepeatedString = function(field, value) {
  989. if (value == null) return;
  990. for (var i = 0; i < value.length; i++) {
  991. this.writeString(field, value[i]);
  992. }
  993. };
  994. /**
  995. * Writes an array of arbitrary byte fields to the buffer.
  996. * @param {number} field The field number.
  997. * @param {?Array.<!jspb.ByteSource>} value The arrays of arrays of bytes to
  998. * write.
  999. */
  1000. jspb.BinaryWriter.prototype.writeRepeatedBytes = function(field, value) {
  1001. if (value == null) return;
  1002. for (var i = 0; i < value.length; i++) {
  1003. this.writeBytes(field, value[i]);
  1004. }
  1005. };
  1006. /**
  1007. * Writes an array of messages to the buffer.
  1008. * @template MessageType
  1009. * @param {number} field The field number.
  1010. * @param {?Array.<MessageType>} value The array of messages to
  1011. * write.
  1012. * @param {function(MessageType, !jspb.BinaryWriter)} writerCallback
  1013. * Will be invoked with the value to write and the writer to write it with.
  1014. */
  1015. jspb.BinaryWriter.prototype.writeRepeatedMessage = function(
  1016. field, value, writerCallback) {
  1017. if (value == null) return;
  1018. for (var i = 0; i < value.length; i++) {
  1019. var bookmark = this.beginDelimited_(field);
  1020. writerCallback(value[i], this);
  1021. this.endDelimited_(bookmark);
  1022. }
  1023. };
  1024. /**
  1025. * Writes an array of group messages to the buffer.
  1026. * @template MessageType
  1027. * @param {number} field The field number.
  1028. * @param {?Array.<MessageType>} value The array of messages to
  1029. * write.
  1030. * @param {function(MessageType, !jspb.BinaryWriter)} writerCallback
  1031. * Will be invoked with the value to write and the writer to write it with.
  1032. */
  1033. jspb.BinaryWriter.prototype.writeRepeatedGroup = function(
  1034. field, value, writerCallback) {
  1035. if (value == null) return;
  1036. for (var i = 0; i < value.length; i++) {
  1037. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.START_GROUP);
  1038. writerCallback(value[i], this);
  1039. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.END_GROUP);
  1040. }
  1041. };
  1042. /**
  1043. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  1044. * the buffer.
  1045. * @param {number} field The field number.
  1046. * @param {?Array.<string>} value The array of hashes to write.
  1047. */
  1048. jspb.BinaryWriter.prototype.writeRepeatedFixedHash64 =
  1049. function(field, value) {
  1050. if (value == null) return;
  1051. for (var i = 0; i < value.length; i++) {
  1052. this.writeFixedHash64(field, value[i]);
  1053. }
  1054. };
  1055. /**
  1056. * Writes a repeated 64-bit hash string field (8 characters @ 8 bits of data
  1057. * each) to the buffer.
  1058. * @param {number} field The field number.
  1059. * @param {?Array.<string>} value The array of hashes to write.
  1060. */
  1061. jspb.BinaryWriter.prototype.writeRepeatedVarintHash64 =
  1062. function(field, value) {
  1063. if (value == null) return;
  1064. for (var i = 0; i < value.length; i++) {
  1065. this.writeVarintHash64(field, value[i]);
  1066. }
  1067. };
  1068. /**
  1069. * Writes an array of numbers to the buffer as a packed 32-bit int field.
  1070. * @param {number} field The field number.
  1071. * @param {?Array.<number>} value The array of ints to write.
  1072. */
  1073. jspb.BinaryWriter.prototype.writePackedInt32 = function(field, value) {
  1074. if (value == null || !value.length) return;
  1075. var bookmark = this.beginDelimited_(field);
  1076. for (var i = 0; i < value.length; i++) {
  1077. this.encoder_.writeSignedVarint32(value[i]);
  1078. }
  1079. this.endDelimited_(bookmark);
  1080. };
  1081. /**
  1082. * Writes an array of numbers represented as strings to the buffer as a packed
  1083. * 32-bit int field.
  1084. * @param {number} field
  1085. * @param {?Array.<string>} value
  1086. */
  1087. jspb.BinaryWriter.prototype.writePackedInt32String = function(field, value) {
  1088. if (value == null || !value.length) return;
  1089. var bookmark = this.beginDelimited_(field);
  1090. for (var i = 0; i < value.length; i++) {
  1091. this.encoder_.writeSignedVarint32(parseInt(value[i], 10));
  1092. }
  1093. this.endDelimited_(bookmark);
  1094. };
  1095. /**
  1096. * Writes an array of numbers to the buffer as a packed 64-bit int field.
  1097. * @param {number} field The field number.
  1098. * @param {?Array.<number>} value The array of ints to write.
  1099. */
  1100. jspb.BinaryWriter.prototype.writePackedInt64 = function(field, value) {
  1101. if (value == null || !value.length) return;
  1102. var bookmark = this.beginDelimited_(field);
  1103. for (var i = 0; i < value.length; i++) {
  1104. this.encoder_.writeSignedVarint64(value[i]);
  1105. }
  1106. this.endDelimited_(bookmark);
  1107. };
  1108. /**
  1109. * Writes an array of numbers represented as strings to the buffer as a packed
  1110. * 64-bit int field.
  1111. * @param {number} field
  1112. * @param {?Array.<string>} value
  1113. */
  1114. jspb.BinaryWriter.prototype.writePackedInt64String = function(field, value) {
  1115. if (value == null || !value.length) return;
  1116. var bookmark = this.beginDelimited_(field);
  1117. for (var i = 0; i < value.length; i++) {
  1118. var num = jspb.arith.Int64.fromString(value[i]);
  1119. this.encoder_.writeSplitVarint64(num.lo, num.hi);
  1120. }
  1121. this.endDelimited_(bookmark);
  1122. };
  1123. /**
  1124. * Writes an array numbers to the buffer as a packed unsigned 32-bit int field.
  1125. * @param {number} field The field number.
  1126. * @param {?Array.<number>} value The array of ints to write.
  1127. */
  1128. jspb.BinaryWriter.prototype.writePackedUint32 = function(field, value) {
  1129. if (value == null || !value.length) return;
  1130. var bookmark = this.beginDelimited_(field);
  1131. for (var i = 0; i < value.length; i++) {
  1132. this.encoder_.writeUnsignedVarint32(value[i]);
  1133. }
  1134. this.endDelimited_(bookmark);
  1135. };
  1136. /**
  1137. * Writes an array of numbers represented as strings to the buffer as a packed
  1138. * unsigned 32-bit int field.
  1139. * @param {number} field
  1140. * @param {?Array.<string>} value
  1141. */
  1142. jspb.BinaryWriter.prototype.writePackedUint32String =
  1143. function(field, value) {
  1144. if (value == null || !value.length) return;
  1145. var bookmark = this.beginDelimited_(field);
  1146. for (var i = 0; i < value.length; i++) {
  1147. this.encoder_.writeUnsignedVarint32(parseInt(value[i], 10));
  1148. }
  1149. this.endDelimited_(bookmark);
  1150. };
  1151. /**
  1152. * Writes an array numbers to the buffer as a packed unsigned 64-bit int field.
  1153. * @param {number} field The field number.
  1154. * @param {?Array.<number>} value The array of ints to write.
  1155. */
  1156. jspb.BinaryWriter.prototype.writePackedUint64 = function(field, value) {
  1157. if (value == null || !value.length) return;
  1158. var bookmark = this.beginDelimited_(field);
  1159. for (var i = 0; i < value.length; i++) {
  1160. this.encoder_.writeUnsignedVarint64(value[i]);
  1161. }
  1162. this.endDelimited_(bookmark);
  1163. };
  1164. /**
  1165. * Writes an array of numbers represented as strings to the buffer as a packed
  1166. * unsigned 64-bit int field.
  1167. * @param {number} field
  1168. * @param {?Array.<string>} value
  1169. */
  1170. jspb.BinaryWriter.prototype.writePackedUint64String =
  1171. function(field, value) {
  1172. if (value == null || !value.length) return;
  1173. var bookmark = this.beginDelimited_(field);
  1174. for (var i = 0; i < value.length; i++) {
  1175. var num = jspb.arith.UInt64.fromString(value[i]);
  1176. this.encoder_.writeSplitVarint64(num.lo, num.hi);
  1177. }
  1178. this.endDelimited_(bookmark);
  1179. };
  1180. /**
  1181. * Writes an array numbers to the buffer as a packed signed 32-bit int field.
  1182. * @param {number} field The field number.
  1183. * @param {?Array.<number>} value The array of ints to write.
  1184. */
  1185. jspb.BinaryWriter.prototype.writePackedSint32 = function(field, value) {
  1186. if (value == null || !value.length) return;
  1187. var bookmark = this.beginDelimited_(field);
  1188. for (var i = 0; i < value.length; i++) {
  1189. this.encoder_.writeZigzagVarint32(value[i]);
  1190. }
  1191. this.endDelimited_(bookmark);
  1192. };
  1193. /**
  1194. * Writes an array of numbers to the buffer as a packed signed 64-bit int field.
  1195. * @param {number} field The field number.
  1196. * @param {?Array.<number>} value The array of ints to write.
  1197. */
  1198. jspb.BinaryWriter.prototype.writePackedSint64 = function(field, value) {
  1199. if (value == null || !value.length) return;
  1200. var bookmark = this.beginDelimited_(field);
  1201. for (var i = 0; i < value.length; i++) {
  1202. this.encoder_.writeZigzagVarint64(value[i]);
  1203. }
  1204. this.endDelimited_(bookmark);
  1205. };
  1206. /**
  1207. * Writes an array of decimal strings to the buffer as a packed signed 64-bit
  1208. * int field.
  1209. * @param {number} field The field number.
  1210. * @param {?Array.<string>} value The array of decimal strings to write.
  1211. */
  1212. jspb.BinaryWriter.prototype.writePackedSint64String = function(field, value) {
  1213. if (value == null || !value.length) return;
  1214. var bookmark = this.beginDelimited_(field);
  1215. for (var i = 0; i < value.length; i++) {
  1216. // TODO(haberman): make lossless
  1217. this.encoder_.writeZigzagVarint64(parseInt(value[i], 10));
  1218. }
  1219. this.endDelimited_(bookmark);
  1220. };
  1221. /**
  1222. * Writes an array of numbers to the buffer as a packed fixed32 field.
  1223. * @param {number} field The field number.
  1224. * @param {?Array.<number>} value The array of ints to write.
  1225. */
  1226. jspb.BinaryWriter.prototype.writePackedFixed32 = function(field, value) {
  1227. if (value == null || !value.length) return;
  1228. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1229. this.encoder_.writeUnsignedVarint32(value.length * 4);
  1230. for (var i = 0; i < value.length; i++) {
  1231. this.encoder_.writeUint32(value[i]);
  1232. }
  1233. };
  1234. /**
  1235. * Writes an array of numbers to the buffer as a packed fixed64 field.
  1236. * @param {number} field The field number.
  1237. * @param {?Array.<number>} value The array of ints to write.
  1238. */
  1239. jspb.BinaryWriter.prototype.writePackedFixed64 = function(field, value) {
  1240. if (value == null || !value.length) return;
  1241. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1242. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1243. for (var i = 0; i < value.length; i++) {
  1244. this.encoder_.writeUint64(value[i]);
  1245. }
  1246. };
  1247. /**
  1248. * Writes an array of numbers represented as strings to the buffer as a packed
  1249. * fixed64 field.
  1250. * @param {number} field The field number.
  1251. * @param {?Array.<string>} value The array of strings to write.
  1252. */
  1253. jspb.BinaryWriter.prototype.writePackedFixed64String = function(field, value) {
  1254. if (value == null || !value.length) return;
  1255. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1256. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1257. for (var i = 0; i < value.length; i++) {
  1258. var num = jspb.arith.UInt64.fromString(value[i]);
  1259. this.encoder_.writeSplitFixed64(num.lo, num.hi);
  1260. }
  1261. };
  1262. /**
  1263. * Writes an array of numbers to the buffer as a packed sfixed32 field.
  1264. * @param {number} field The field number.
  1265. * @param {?Array.<number>} value The array of ints to write.
  1266. */
  1267. jspb.BinaryWriter.prototype.writePackedSfixed32 = function(field, value) {
  1268. if (value == null || !value.length) return;
  1269. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1270. this.encoder_.writeUnsignedVarint32(value.length * 4);
  1271. for (var i = 0; i < value.length; i++) {
  1272. this.encoder_.writeInt32(value[i]);
  1273. }
  1274. };
  1275. /**
  1276. * Writes an array of numbers to the buffer as a packed sfixed64 field.
  1277. * @param {number} field The field number.
  1278. * @param {?Array.<number>} value The array of ints to write.
  1279. */
  1280. jspb.BinaryWriter.prototype.writePackedSfixed64 = function(field, value) {
  1281. if (value == null || !value.length) return;
  1282. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1283. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1284. for (var i = 0; i < value.length; i++) {
  1285. this.encoder_.writeInt64(value[i]);
  1286. }
  1287. };
  1288. /**
  1289. * Writes an array of numbers to the buffer as a packed sfixed64 field.
  1290. * @param {number} field The field number.
  1291. * @param {?Array.<string>} value The array of decimal strings to write.
  1292. */
  1293. jspb.BinaryWriter.prototype.writePackedSfixed64String = function(field, value) {
  1294. if (value == null || !value.length) return;
  1295. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1296. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1297. for (var i = 0; i < value.length; i++) {
  1298. this.encoder_.writeInt64String(value[i]);
  1299. }
  1300. };
  1301. /**
  1302. * Writes an array of numbers to the buffer as a packed float field.
  1303. * @param {number} field The field number.
  1304. * @param {?Array.<number>} value The array of ints to write.
  1305. */
  1306. jspb.BinaryWriter.prototype.writePackedFloat = function(field, value) {
  1307. if (value == null || !value.length) return;
  1308. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1309. this.encoder_.writeUnsignedVarint32(value.length * 4);
  1310. for (var i = 0; i < value.length; i++) {
  1311. this.encoder_.writeFloat(value[i]);
  1312. }
  1313. };
  1314. /**
  1315. * Writes an array of numbers to the buffer as a packed double field.
  1316. * @param {number} field The field number.
  1317. * @param {?Array.<number>} value The array of ints to write.
  1318. */
  1319. jspb.BinaryWriter.prototype.writePackedDouble = function(field, value) {
  1320. if (value == null || !value.length) return;
  1321. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1322. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1323. for (var i = 0; i < value.length; i++) {
  1324. this.encoder_.writeDouble(value[i]);
  1325. }
  1326. };
  1327. /**
  1328. * Writes an array of booleans to the buffer as a packed bool field.
  1329. * @param {number} field The field number.
  1330. * @param {?Array.<boolean>} value The array of ints to write.
  1331. */
  1332. jspb.BinaryWriter.prototype.writePackedBool = function(field, value) {
  1333. if (value == null || !value.length) return;
  1334. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1335. this.encoder_.writeUnsignedVarint32(value.length);
  1336. for (var i = 0; i < value.length; i++) {
  1337. this.encoder_.writeBool(value[i]);
  1338. }
  1339. };
  1340. /**
  1341. * Writes an array of enums to the buffer as a packed enum field.
  1342. * @param {number} field The field number.
  1343. * @param {?Array.<number>} value The array of ints to write.
  1344. */
  1345. jspb.BinaryWriter.prototype.writePackedEnum = function(field, value) {
  1346. if (value == null || !value.length) return;
  1347. var bookmark = this.beginDelimited_(field);
  1348. for (var i = 0; i < value.length; i++) {
  1349. this.encoder_.writeEnum(value[i]);
  1350. }
  1351. this.endDelimited_(bookmark);
  1352. };
  1353. /**
  1354. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  1355. * the buffer.
  1356. * @param {number} field The field number.
  1357. * @param {?Array.<string>} value The array of hashes to write.
  1358. */
  1359. jspb.BinaryWriter.prototype.writePackedFixedHash64 = function(field, value) {
  1360. if (value == null || !value.length) return;
  1361. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1362. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1363. for (var i = 0; i < value.length; i++) {
  1364. this.encoder_.writeFixedHash64(value[i]);
  1365. }
  1366. };
  1367. /**
  1368. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  1369. * the buffer.
  1370. * @param {number} field The field number.
  1371. * @param {?Array.<string>} value The array of hashes to write.
  1372. */
  1373. jspb.BinaryWriter.prototype.writePackedVarintHash64 = function(field, value) {
  1374. if (value == null || !value.length) return;
  1375. var bookmark = this.beginDelimited_(field);
  1376. for (var i = 0; i < value.length; i++) {
  1377. this.encoder_.writeVarintHash64(value[i]);
  1378. }
  1379. this.endDelimited_(bookmark);
  1380. };