writer.js 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  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 group message to the buffer.
  698. *
  699. * @param {number} field The field number.
  700. * @param {?MessageType} value The message to write, wrapped with START_GROUP /
  701. * END_GROUP tags. Will be a no-op if 'value' is null.
  702. * @param {function(MessageTypeNonNull, !jspb.BinaryWriter)} writerCallback
  703. * Will be invoked with the value to write and the writer to write it with.
  704. * @template MessageType
  705. * Use go/closure-ttl to declare a non-nullable version of MessageType. Replace
  706. * the null in blah|null with none. This is necessary because the compiler will
  707. * infer MessageType to be nullable if the value parameter is nullable.
  708. * @template MessageTypeNonNull :=
  709. * cond(isUnknown(MessageType), unknown(),
  710. * mapunion(MessageType, (X) =>
  711. * cond(eq(X, 'null'), none(), X)))
  712. * =:
  713. */
  714. jspb.BinaryWriter.prototype.writeGroup = function(
  715. field, value, writerCallback) {
  716. if (value == null) return;
  717. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.START_GROUP);
  718. writerCallback(value, this);
  719. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.END_GROUP);
  720. };
  721. /**
  722. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  723. * the buffer.
  724. * @param {number} field The field number.
  725. * @param {string?} value The hash string.
  726. */
  727. jspb.BinaryWriter.prototype.writeFixedHash64 = function(field, value) {
  728. if (value == null) return;
  729. goog.asserts.assert(value.length == 8);
  730. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  731. this.encoder_.writeFixedHash64(value);
  732. };
  733. /**
  734. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  735. * the buffer.
  736. * @param {number} field The field number.
  737. * @param {string?} value The hash string.
  738. */
  739. jspb.BinaryWriter.prototype.writeVarintHash64 = function(field, value) {
  740. if (value == null) return;
  741. goog.asserts.assert(value.length == 8);
  742. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  743. this.encoder_.writeVarintHash64(value);
  744. };
  745. /**
  746. * Writes an array of numbers to the buffer as a repeated 32-bit int field.
  747. * @param {number} field The field number.
  748. * @param {?Array<number>} value The array of ints to write.
  749. */
  750. jspb.BinaryWriter.prototype.writeRepeatedInt32 = function(field, value) {
  751. if (value == null) return;
  752. for (var i = 0; i < value.length; i++) {
  753. this.writeSignedVarint32_(field, value[i]);
  754. }
  755. };
  756. /**
  757. * Writes an array of numbers formatted as strings to the buffer as a repeated
  758. * 32-bit int field.
  759. * @param {number} field The field number.
  760. * @param {?Array<string>} value The array of ints to write.
  761. */
  762. jspb.BinaryWriter.prototype.writeRepeatedInt32String = function(field, value) {
  763. if (value == null) return;
  764. for (var i = 0; i < value.length; i++) {
  765. this.writeInt32String(field, value[i]);
  766. }
  767. };
  768. /**
  769. * Writes an array of numbers to the buffer as a repeated 64-bit int field.
  770. * @param {number} field The field number.
  771. * @param {?Array<number>} value The array of ints to write.
  772. */
  773. jspb.BinaryWriter.prototype.writeRepeatedInt64 = function(field, value) {
  774. if (value == null) return;
  775. for (var i = 0; i < value.length; i++) {
  776. this.writeSignedVarint64_(field, value[i]);
  777. }
  778. };
  779. /**
  780. * Writes an array of numbers formatted as strings to the buffer as a repeated
  781. * 64-bit int field.
  782. * @param {number} field The field number.
  783. * @param {?Array<string>} value The array of ints to write.
  784. */
  785. jspb.BinaryWriter.prototype.writeRepeatedInt64String = function(field, value) {
  786. if (value == null) return;
  787. for (var i = 0; i < value.length; i++) {
  788. this.writeInt64String(field, value[i]);
  789. }
  790. };
  791. /**
  792. * Writes an array numbers to the buffer as a repeated unsigned 32-bit int
  793. * field.
  794. * @param {number} field The field number.
  795. * @param {?Array<number>} value The array of ints to write.
  796. */
  797. jspb.BinaryWriter.prototype.writeRepeatedUint32 = function(field, value) {
  798. if (value == null) return;
  799. for (var i = 0; i < value.length; i++) {
  800. this.writeUnsignedVarint32_(field, value[i]);
  801. }
  802. };
  803. /**
  804. * Writes an array of numbers formatted as strings to the buffer as a repeated
  805. * unsigned 32-bit int field.
  806. * @param {number} field The field number.
  807. * @param {?Array<string>} value The array of ints to write.
  808. */
  809. jspb.BinaryWriter.prototype.writeRepeatedUint32String = function(field, value) {
  810. if (value == null) return;
  811. for (var i = 0; i < value.length; i++) {
  812. this.writeUint32String(field, value[i]);
  813. }
  814. };
  815. /**
  816. * Writes an array numbers to the buffer as a repeated unsigned 64-bit int
  817. * field.
  818. * @param {number} field The field number.
  819. * @param {?Array<number>} value The array of ints to write.
  820. */
  821. jspb.BinaryWriter.prototype.writeRepeatedUint64 = function(field, value) {
  822. if (value == null) return;
  823. for (var i = 0; i < value.length; i++) {
  824. this.writeUnsignedVarint64_(field, value[i]);
  825. }
  826. };
  827. /**
  828. * Writes an array of numbers formatted as strings to the buffer as a repeated
  829. * unsigned 64-bit int field.
  830. * @param {number} field The field number.
  831. * @param {?Array<string>} value The array of ints to write.
  832. */
  833. jspb.BinaryWriter.prototype.writeRepeatedUint64String = function(field, value) {
  834. if (value == null) return;
  835. for (var i = 0; i < value.length; i++) {
  836. this.writeUint64String(field, value[i]);
  837. }
  838. };
  839. /**
  840. * Writes an array numbers to the buffer as a repeated signed 32-bit int field.
  841. * @param {number} field The field number.
  842. * @param {?Array<number>} value The array of ints to write.
  843. */
  844. jspb.BinaryWriter.prototype.writeRepeatedSint32 = function(field, value) {
  845. if (value == null) return;
  846. for (var i = 0; i < value.length; i++) {
  847. this.writeZigzagVarint32_(field, value[i]);
  848. }
  849. };
  850. /**
  851. * Writes an array numbers to the buffer as a repeated signed 64-bit int field.
  852. * @param {number} field The field number.
  853. * @param {?Array<number>} value The array of ints to write.
  854. */
  855. jspb.BinaryWriter.prototype.writeRepeatedSint64 = function(field, value) {
  856. if (value == null) return;
  857. for (var i = 0; i < value.length; i++) {
  858. this.writeZigzagVarint64_(field, value[i]);
  859. }
  860. };
  861. /**
  862. * Writes an array numbers to the buffer as a repeated signed 64-bit int field.
  863. * @param {number} field The field number.
  864. * @param {?Array<string>} value The array of ints to write.
  865. */
  866. jspb.BinaryWriter.prototype.writeRepeatedSint64String = function(field, value) {
  867. if (value == null) return;
  868. for (var i = 0; i < value.length; i++) {
  869. this.writeZigzagVarint64String_(field, value[i]);
  870. }
  871. };
  872. /**
  873. * Writes an array of numbers to the buffer as a repeated fixed32 field. This
  874. * works for both signed and unsigned fixed32s.
  875. * @param {number} field The field number.
  876. * @param {?Array<number>} value The array of ints to write.
  877. */
  878. jspb.BinaryWriter.prototype.writeRepeatedFixed32 = function(field, value) {
  879. if (value == null) return;
  880. for (var i = 0; i < value.length; i++) {
  881. this.writeFixed32(field, value[i]);
  882. }
  883. };
  884. /**
  885. * Writes an array of numbers to the buffer as a repeated fixed64 field. This
  886. * works for both signed and unsigned fixed64s.
  887. * @param {number} field The field number.
  888. * @param {?Array<number>} value The array of ints to write.
  889. */
  890. jspb.BinaryWriter.prototype.writeRepeatedFixed64 = function(field, value) {
  891. if (value == null) return;
  892. for (var i = 0; i < value.length; i++) {
  893. this.writeFixed64(field, value[i]);
  894. }
  895. };
  896. /**
  897. * Writes an array of numbers to the buffer as a repeated fixed64 field. This
  898. * works for both signed and unsigned fixed64s.
  899. * @param {number} field The field number.
  900. * @param {?Array<string>} value The array of decimal strings to write.
  901. */
  902. jspb.BinaryWriter.prototype.writeRepeatedFixed64String = function(
  903. field, value) {
  904. if (value == null) return;
  905. for (var i = 0; i < value.length; i++) {
  906. this.writeFixed64String(field, value[i]);
  907. }
  908. };
  909. /**
  910. * Writes an array of numbers to the buffer as a repeated sfixed32 field.
  911. * @param {number} field The field number.
  912. * @param {?Array<number>} value The array of ints to write.
  913. */
  914. jspb.BinaryWriter.prototype.writeRepeatedSfixed32 = function(field, value) {
  915. if (value == null) return;
  916. for (var i = 0; i < value.length; i++) {
  917. this.writeSfixed32(field, value[i]);
  918. }
  919. };
  920. /**
  921. * Writes an array of numbers to the buffer as a repeated sfixed64 field.
  922. * @param {number} field The field number.
  923. * @param {?Array<number>} value The array of ints to write.
  924. */
  925. jspb.BinaryWriter.prototype.writeRepeatedSfixed64 = function(field, value) {
  926. if (value == null) return;
  927. for (var i = 0; i < value.length; i++) {
  928. this.writeSfixed64(field, value[i]);
  929. }
  930. };
  931. /**
  932. * Writes an array of decimal strings to the buffer as a repeated sfixed64
  933. * field.
  934. * @param {number} field The field number.
  935. * @param {?Array<string>} value The array of decimal strings to write.
  936. */
  937. jspb.BinaryWriter.prototype.writeRepeatedSfixed64String = function(field, value) {
  938. if (value == null) return;
  939. for (var i = 0; i < value.length; i++) {
  940. this.writeSfixed64String(field, value[i]);
  941. }
  942. };
  943. /**
  944. * Writes an array of numbers to the buffer as a repeated float field.
  945. * @param {number} field The field number.
  946. * @param {?Array<number>} value The array of ints to write.
  947. */
  948. jspb.BinaryWriter.prototype.writeRepeatedFloat = function(field, value) {
  949. if (value == null) return;
  950. for (var i = 0; i < value.length; i++) {
  951. this.writeFloat(field, value[i]);
  952. }
  953. };
  954. /**
  955. * Writes an array of numbers to the buffer as a repeated double field.
  956. * @param {number} field The field number.
  957. * @param {?Array<number>} value The array of ints to write.
  958. */
  959. jspb.BinaryWriter.prototype.writeRepeatedDouble = function(field, value) {
  960. if (value == null) return;
  961. for (var i = 0; i < value.length; i++) {
  962. this.writeDouble(field, value[i]);
  963. }
  964. };
  965. /**
  966. * Writes an array of booleans to the buffer as a repeated bool field.
  967. * @param {number} field The field number.
  968. * @param {?Array<boolean>} value The array of ints to write.
  969. */
  970. jspb.BinaryWriter.prototype.writeRepeatedBool = function(field, value) {
  971. if (value == null) return;
  972. for (var i = 0; i < value.length; i++) {
  973. this.writeBool(field, value[i]);
  974. }
  975. };
  976. /**
  977. * Writes an array of enums to the buffer as a repeated enum field.
  978. * @param {number} field The field number.
  979. * @param {?Array<number>} value The array of ints to write.
  980. */
  981. jspb.BinaryWriter.prototype.writeRepeatedEnum = function(field, value) {
  982. if (value == null) return;
  983. for (var i = 0; i < value.length; i++) {
  984. this.writeEnum(field, value[i]);
  985. }
  986. };
  987. /**
  988. * Writes an array of strings to the buffer as a repeated string field.
  989. * @param {number} field The field number.
  990. * @param {?Array<string>} value The array of strings to write.
  991. */
  992. jspb.BinaryWriter.prototype.writeRepeatedString = function(field, value) {
  993. if (value == null) return;
  994. for (var i = 0; i < value.length; i++) {
  995. this.writeString(field, value[i]);
  996. }
  997. };
  998. /**
  999. * Writes an array of arbitrary byte fields to the buffer.
  1000. * @param {number} field The field number.
  1001. * @param {?Array<!jspb.ByteSource>} value The arrays of arrays of bytes to
  1002. * write.
  1003. */
  1004. jspb.BinaryWriter.prototype.writeRepeatedBytes = function(field, value) {
  1005. if (value == null) return;
  1006. for (var i = 0; i < value.length; i++) {
  1007. this.writeBytes(field, value[i]);
  1008. }
  1009. };
  1010. /**
  1011. * Writes an array of messages to the buffer.
  1012. * @template MessageType
  1013. * @param {number} field The field number.
  1014. * @param {?Array<MessageType>} value The array of messages to
  1015. * write.
  1016. * @param {function(MessageType, !jspb.BinaryWriter)} writerCallback
  1017. * Will be invoked with the value to write and the writer to write it with.
  1018. */
  1019. jspb.BinaryWriter.prototype.writeRepeatedMessage = function(
  1020. field, value, writerCallback) {
  1021. if (value == null) return;
  1022. for (var i = 0; i < value.length; i++) {
  1023. var bookmark = this.beginDelimited_(field);
  1024. writerCallback(value[i], this);
  1025. this.endDelimited_(bookmark);
  1026. }
  1027. };
  1028. /**
  1029. * Writes an array of group messages to the buffer.
  1030. * @template MessageType
  1031. * @param {number} field The field number.
  1032. * @param {?Array<MessageType>} value The array of messages to
  1033. * write.
  1034. * @param {function(MessageType, !jspb.BinaryWriter)} writerCallback
  1035. * Will be invoked with the value to write and the writer to write it with.
  1036. */
  1037. jspb.BinaryWriter.prototype.writeRepeatedGroup = function(
  1038. field, value, writerCallback) {
  1039. if (value == null) return;
  1040. for (var i = 0; i < value.length; i++) {
  1041. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.START_GROUP);
  1042. writerCallback(value[i], this);
  1043. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.END_GROUP);
  1044. }
  1045. };
  1046. /**
  1047. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  1048. * the buffer.
  1049. * @param {number} field The field number.
  1050. * @param {?Array<string>} value The array of hashes to write.
  1051. */
  1052. jspb.BinaryWriter.prototype.writeRepeatedFixedHash64 =
  1053. function(field, value) {
  1054. if (value == null) return;
  1055. for (var i = 0; i < value.length; i++) {
  1056. this.writeFixedHash64(field, value[i]);
  1057. }
  1058. };
  1059. /**
  1060. * Writes a repeated 64-bit hash string field (8 characters @ 8 bits of data
  1061. * each) to the buffer.
  1062. * @param {number} field The field number.
  1063. * @param {?Array<string>} value The array of hashes to write.
  1064. */
  1065. jspb.BinaryWriter.prototype.writeRepeatedVarintHash64 =
  1066. function(field, value) {
  1067. if (value == null) return;
  1068. for (var i = 0; i < value.length; i++) {
  1069. this.writeVarintHash64(field, value[i]);
  1070. }
  1071. };
  1072. /**
  1073. * Writes an array of numbers to the buffer as a packed 32-bit int field.
  1074. * @param {number} field The field number.
  1075. * @param {?Array<number>} value The array of ints to write.
  1076. */
  1077. jspb.BinaryWriter.prototype.writePackedInt32 = function(field, value) {
  1078. if (value == null || !value.length) return;
  1079. var bookmark = this.beginDelimited_(field);
  1080. for (var i = 0; i < value.length; i++) {
  1081. this.encoder_.writeSignedVarint32(value[i]);
  1082. }
  1083. this.endDelimited_(bookmark);
  1084. };
  1085. /**
  1086. * Writes an array of numbers represented as strings to the buffer as a packed
  1087. * 32-bit int field.
  1088. * @param {number} field
  1089. * @param {?Array<string>} value
  1090. */
  1091. jspb.BinaryWriter.prototype.writePackedInt32String = function(field, value) {
  1092. if (value == null || !value.length) return;
  1093. var bookmark = this.beginDelimited_(field);
  1094. for (var i = 0; i < value.length; i++) {
  1095. this.encoder_.writeSignedVarint32(parseInt(value[i], 10));
  1096. }
  1097. this.endDelimited_(bookmark);
  1098. };
  1099. /**
  1100. * Writes an array of numbers to the buffer as a packed 64-bit int field.
  1101. * @param {number} field The field number.
  1102. * @param {?Array<number>} value The array of ints to write.
  1103. */
  1104. jspb.BinaryWriter.prototype.writePackedInt64 = function(field, value) {
  1105. if (value == null || !value.length) return;
  1106. var bookmark = this.beginDelimited_(field);
  1107. for (var i = 0; i < value.length; i++) {
  1108. this.encoder_.writeSignedVarint64(value[i]);
  1109. }
  1110. this.endDelimited_(bookmark);
  1111. };
  1112. /**
  1113. * Writes an array of numbers represented as strings to the buffer as a packed
  1114. * 64-bit int field.
  1115. * @param {number} field
  1116. * @param {?Array<string>} value
  1117. */
  1118. jspb.BinaryWriter.prototype.writePackedInt64String = function(field, value) {
  1119. if (value == null || !value.length) return;
  1120. var bookmark = this.beginDelimited_(field);
  1121. for (var i = 0; i < value.length; i++) {
  1122. var num = jspb.arith.Int64.fromString(value[i]);
  1123. this.encoder_.writeSplitVarint64(num.lo, num.hi);
  1124. }
  1125. this.endDelimited_(bookmark);
  1126. };
  1127. /**
  1128. * Writes an array numbers to the buffer as a packed unsigned 32-bit int field.
  1129. * @param {number} field The field number.
  1130. * @param {?Array<number>} value The array of ints to write.
  1131. */
  1132. jspb.BinaryWriter.prototype.writePackedUint32 = function(field, value) {
  1133. if (value == null || !value.length) return;
  1134. var bookmark = this.beginDelimited_(field);
  1135. for (var i = 0; i < value.length; i++) {
  1136. this.encoder_.writeUnsignedVarint32(value[i]);
  1137. }
  1138. this.endDelimited_(bookmark);
  1139. };
  1140. /**
  1141. * Writes an array of numbers represented as strings to the buffer as a packed
  1142. * unsigned 32-bit int field.
  1143. * @param {number} field
  1144. * @param {?Array<string>} value
  1145. */
  1146. jspb.BinaryWriter.prototype.writePackedUint32String =
  1147. function(field, value) {
  1148. if (value == null || !value.length) return;
  1149. var bookmark = this.beginDelimited_(field);
  1150. for (var i = 0; i < value.length; i++) {
  1151. this.encoder_.writeUnsignedVarint32(parseInt(value[i], 10));
  1152. }
  1153. this.endDelimited_(bookmark);
  1154. };
  1155. /**
  1156. * Writes an array numbers to the buffer as a packed unsigned 64-bit int field.
  1157. * @param {number} field The field number.
  1158. * @param {?Array<number>} value The array of ints to write.
  1159. */
  1160. jspb.BinaryWriter.prototype.writePackedUint64 = function(field, value) {
  1161. if (value == null || !value.length) return;
  1162. var bookmark = this.beginDelimited_(field);
  1163. for (var i = 0; i < value.length; i++) {
  1164. this.encoder_.writeUnsignedVarint64(value[i]);
  1165. }
  1166. this.endDelimited_(bookmark);
  1167. };
  1168. /**
  1169. * Writes an array of numbers represented as strings to the buffer as a packed
  1170. * unsigned 64-bit int field.
  1171. * @param {number} field
  1172. * @param {?Array<string>} value
  1173. */
  1174. jspb.BinaryWriter.prototype.writePackedUint64String =
  1175. function(field, value) {
  1176. if (value == null || !value.length) return;
  1177. var bookmark = this.beginDelimited_(field);
  1178. for (var i = 0; i < value.length; i++) {
  1179. var num = jspb.arith.UInt64.fromString(value[i]);
  1180. this.encoder_.writeSplitVarint64(num.lo, num.hi);
  1181. }
  1182. this.endDelimited_(bookmark);
  1183. };
  1184. /**
  1185. * Writes an array numbers to the buffer as a packed signed 32-bit int field.
  1186. * @param {number} field The field number.
  1187. * @param {?Array<number>} value The array of ints to write.
  1188. */
  1189. jspb.BinaryWriter.prototype.writePackedSint32 = function(field, value) {
  1190. if (value == null || !value.length) return;
  1191. var bookmark = this.beginDelimited_(field);
  1192. for (var i = 0; i < value.length; i++) {
  1193. this.encoder_.writeZigzagVarint32(value[i]);
  1194. }
  1195. this.endDelimited_(bookmark);
  1196. };
  1197. /**
  1198. * Writes an array of numbers to the buffer as a packed signed 64-bit int field.
  1199. * @param {number} field The field number.
  1200. * @param {?Array<number>} value The array of ints to write.
  1201. */
  1202. jspb.BinaryWriter.prototype.writePackedSint64 = function(field, value) {
  1203. if (value == null || !value.length) return;
  1204. var bookmark = this.beginDelimited_(field);
  1205. for (var i = 0; i < value.length; i++) {
  1206. this.encoder_.writeZigzagVarint64(value[i]);
  1207. }
  1208. this.endDelimited_(bookmark);
  1209. };
  1210. /**
  1211. * Writes an array of decimal strings to the buffer as a packed signed 64-bit
  1212. * int field.
  1213. * @param {number} field The field number.
  1214. * @param {?Array<string>} value The array of decimal strings to write.
  1215. */
  1216. jspb.BinaryWriter.prototype.writePackedSint64String = function(field, value) {
  1217. if (value == null || !value.length) return;
  1218. var bookmark = this.beginDelimited_(field);
  1219. for (var i = 0; i < value.length; i++) {
  1220. // TODO(haberman): make lossless
  1221. this.encoder_.writeZigzagVarint64(parseInt(value[i], 10));
  1222. }
  1223. this.endDelimited_(bookmark);
  1224. };
  1225. /**
  1226. * Writes an array of numbers to the buffer as a packed fixed32 field.
  1227. * @param {number} field The field number.
  1228. * @param {?Array<number>} value The array of ints to write.
  1229. */
  1230. jspb.BinaryWriter.prototype.writePackedFixed32 = function(field, value) {
  1231. if (value == null || !value.length) return;
  1232. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1233. this.encoder_.writeUnsignedVarint32(value.length * 4);
  1234. for (var i = 0; i < value.length; i++) {
  1235. this.encoder_.writeUint32(value[i]);
  1236. }
  1237. };
  1238. /**
  1239. * Writes an array of numbers to the buffer as a packed fixed64 field.
  1240. * @param {number} field The field number.
  1241. * @param {?Array<number>} value The array of ints to write.
  1242. */
  1243. jspb.BinaryWriter.prototype.writePackedFixed64 = function(field, value) {
  1244. if (value == null || !value.length) return;
  1245. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1246. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1247. for (var i = 0; i < value.length; i++) {
  1248. this.encoder_.writeUint64(value[i]);
  1249. }
  1250. };
  1251. /**
  1252. * Writes an array of numbers represented as strings to the buffer as a packed
  1253. * fixed64 field.
  1254. * @param {number} field The field number.
  1255. * @param {?Array<string>} value The array of strings to write.
  1256. */
  1257. jspb.BinaryWriter.prototype.writePackedFixed64String = function(field, value) {
  1258. if (value == null || !value.length) return;
  1259. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1260. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1261. for (var i = 0; i < value.length; i++) {
  1262. var num = jspb.arith.UInt64.fromString(value[i]);
  1263. this.encoder_.writeSplitFixed64(num.lo, num.hi);
  1264. }
  1265. };
  1266. /**
  1267. * Writes an array of numbers to the buffer as a packed sfixed32 field.
  1268. * @param {number} field The field number.
  1269. * @param {?Array<number>} value The array of ints to write.
  1270. */
  1271. jspb.BinaryWriter.prototype.writePackedSfixed32 = function(field, value) {
  1272. if (value == null || !value.length) return;
  1273. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1274. this.encoder_.writeUnsignedVarint32(value.length * 4);
  1275. for (var i = 0; i < value.length; i++) {
  1276. this.encoder_.writeInt32(value[i]);
  1277. }
  1278. };
  1279. /**
  1280. * Writes an array of numbers to the buffer as a packed sfixed64 field.
  1281. * @param {number} field The field number.
  1282. * @param {?Array<number>} value The array of ints to write.
  1283. */
  1284. jspb.BinaryWriter.prototype.writePackedSfixed64 = function(field, value) {
  1285. if (value == null || !value.length) return;
  1286. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1287. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1288. for (var i = 0; i < value.length; i++) {
  1289. this.encoder_.writeInt64(value[i]);
  1290. }
  1291. };
  1292. /**
  1293. * Writes an array of numbers to the buffer as a packed sfixed64 field.
  1294. * @param {number} field The field number.
  1295. * @param {?Array<string>} value The array of decimal strings to write.
  1296. */
  1297. jspb.BinaryWriter.prototype.writePackedSfixed64String = function(field, value) {
  1298. if (value == null || !value.length) return;
  1299. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1300. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1301. for (var i = 0; i < value.length; i++) {
  1302. this.encoder_.writeInt64String(value[i]);
  1303. }
  1304. };
  1305. /**
  1306. * Writes an array of numbers to the buffer as a packed float field.
  1307. * @param {number} field The field number.
  1308. * @param {?Array<number>} value The array of ints to write.
  1309. */
  1310. jspb.BinaryWriter.prototype.writePackedFloat = function(field, value) {
  1311. if (value == null || !value.length) return;
  1312. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1313. this.encoder_.writeUnsignedVarint32(value.length * 4);
  1314. for (var i = 0; i < value.length; i++) {
  1315. this.encoder_.writeFloat(value[i]);
  1316. }
  1317. };
  1318. /**
  1319. * Writes an array of numbers to the buffer as a packed double field.
  1320. * @param {number} field The field number.
  1321. * @param {?Array<number>} value The array of ints to write.
  1322. */
  1323. jspb.BinaryWriter.prototype.writePackedDouble = function(field, value) {
  1324. if (value == null || !value.length) return;
  1325. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1326. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1327. for (var i = 0; i < value.length; i++) {
  1328. this.encoder_.writeDouble(value[i]);
  1329. }
  1330. };
  1331. /**
  1332. * Writes an array of booleans to the buffer as a packed bool field.
  1333. * @param {number} field The field number.
  1334. * @param {?Array<boolean>} value The array of ints to write.
  1335. */
  1336. jspb.BinaryWriter.prototype.writePackedBool = function(field, value) {
  1337. if (value == null || !value.length) return;
  1338. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1339. this.encoder_.writeUnsignedVarint32(value.length);
  1340. for (var i = 0; i < value.length; i++) {
  1341. this.encoder_.writeBool(value[i]);
  1342. }
  1343. };
  1344. /**
  1345. * Writes an array of enums to the buffer as a packed enum field.
  1346. * @param {number} field The field number.
  1347. * @param {?Array<number>} value The array of ints to write.
  1348. */
  1349. jspb.BinaryWriter.prototype.writePackedEnum = function(field, value) {
  1350. if (value == null || !value.length) return;
  1351. var bookmark = this.beginDelimited_(field);
  1352. for (var i = 0; i < value.length; i++) {
  1353. this.encoder_.writeEnum(value[i]);
  1354. }
  1355. this.endDelimited_(bookmark);
  1356. };
  1357. /**
  1358. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  1359. * the buffer.
  1360. * @param {number} field The field number.
  1361. * @param {?Array<string>} value The array of hashes to write.
  1362. */
  1363. jspb.BinaryWriter.prototype.writePackedFixedHash64 = function(field, value) {
  1364. if (value == null || !value.length) return;
  1365. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1366. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1367. for (var i = 0; i < value.length; i++) {
  1368. this.encoder_.writeFixedHash64(value[i]);
  1369. }
  1370. };
  1371. /**
  1372. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  1373. * the buffer.
  1374. * @param {number} field The field number.
  1375. * @param {?Array<string>} value The array of hashes to write.
  1376. */
  1377. jspb.BinaryWriter.prototype.writePackedVarintHash64 = function(field, value) {
  1378. if (value == null || !value.length) return;
  1379. var bookmark = this.beginDelimited_(field);
  1380. for (var i = 0; i < value.length; i++) {
  1381. this.encoder_.writeVarintHash64(value[i]);
  1382. }
  1383. this.endDelimited_(bookmark);
  1384. };