writer.js 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561
  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 an int32 field to the buffer. Numbers outside the range [-2^31,2^31)
  383. * will be truncated.
  384. * @param {number} field The field number.
  385. * @param {number?} value The value to write.
  386. */
  387. jspb.BinaryWriter.prototype.writeInt32 = function(field, value) {
  388. if (value == null) return;
  389. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
  390. (value < jspb.BinaryConstants.TWO_TO_31));
  391. this.writeSignedVarint32_(field, value);
  392. };
  393. /**
  394. * Writes an int32 field represented as a string to the buffer. Numbers outside
  395. * the range [-2^31,2^31) will be truncated.
  396. * @param {number} field The field number.
  397. * @param {string?} value The value to write.
  398. */
  399. jspb.BinaryWriter.prototype.writeInt32String = function(field, value) {
  400. if (value == null) return;
  401. var intValue = /** {number} */ parseInt(value, 10);
  402. goog.asserts.assert((intValue >= -jspb.BinaryConstants.TWO_TO_31) &&
  403. (intValue < jspb.BinaryConstants.TWO_TO_31));
  404. this.writeSignedVarint32_(field, intValue);
  405. };
  406. /**
  407. * Writes an int64 field to the buffer. Numbers outside the range [-2^63,2^63)
  408. * will be truncated.
  409. * @param {number} field The field number.
  410. * @param {number?} value The value to write.
  411. */
  412. jspb.BinaryWriter.prototype.writeInt64 = function(field, value) {
  413. if (value == null) return;
  414. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
  415. (value < jspb.BinaryConstants.TWO_TO_63));
  416. this.writeSignedVarint64_(field, value);
  417. };
  418. /**
  419. * Writes a int64 field (with value as a string) to the buffer.
  420. * @param {number} field The field number.
  421. * @param {string?} value The value to write.
  422. */
  423. jspb.BinaryWriter.prototype.writeInt64String = function(field, value) {
  424. if (value == null) return;
  425. var num = jspb.arith.Int64.fromString(value);
  426. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  427. this.encoder_.writeSplitVarint64(num.lo, num.hi);
  428. };
  429. /**
  430. * Writes a uint32 field to the buffer. Numbers outside the range [0,2^32)
  431. * will be truncated.
  432. * @param {number} field The field number.
  433. * @param {number?} value The value to write.
  434. */
  435. jspb.BinaryWriter.prototype.writeUint32 = function(field, value) {
  436. if (value == null) return;
  437. goog.asserts.assert((value >= 0) &&
  438. (value < jspb.BinaryConstants.TWO_TO_32));
  439. this.writeUnsignedVarint32_(field, value);
  440. };
  441. /**
  442. * Writes a uint32 field represented as a string to the buffer. Numbers outside
  443. * the range [0,2^32) will be truncated.
  444. * @param {number} field The field number.
  445. * @param {string?} value The value to write.
  446. */
  447. jspb.BinaryWriter.prototype.writeUint32String = function(field, value) {
  448. if (value == null) return;
  449. var intValue = /** {number} */ parseInt(value, 10);
  450. goog.asserts.assert((intValue >= 0) &&
  451. (intValue < jspb.BinaryConstants.TWO_TO_32));
  452. this.writeUnsignedVarint32_(field, intValue);
  453. };
  454. /**
  455. * Writes a uint64 field to the buffer. Numbers outside the range [0,2^64)
  456. * will be truncated.
  457. * @param {number} field The field number.
  458. * @param {number?} value The value to write.
  459. */
  460. jspb.BinaryWriter.prototype.writeUint64 = function(field, value) {
  461. if (value == null) return;
  462. goog.asserts.assert((value >= 0) &&
  463. (value < jspb.BinaryConstants.TWO_TO_64));
  464. this.writeUnsignedVarint64_(field, value);
  465. };
  466. /**
  467. * Writes a uint64 field (with value as a string) to the buffer.
  468. * @param {number} field The field number.
  469. * @param {string?} value The value to write.
  470. */
  471. jspb.BinaryWriter.prototype.writeUint64String = function(field, value) {
  472. if (value == null) return;
  473. var num = jspb.arith.UInt64.fromString(value);
  474. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  475. this.encoder_.writeSplitVarint64(num.lo, num.hi);
  476. };
  477. /**
  478. * Writes a sint32 field to the buffer. Numbers outside the range [-2^31,2^31)
  479. * will be truncated.
  480. * @param {number} field The field number.
  481. * @param {number?} value The value to write.
  482. */
  483. jspb.BinaryWriter.prototype.writeSint32 = function(field, value) {
  484. if (value == null) return;
  485. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
  486. (value < jspb.BinaryConstants.TWO_TO_31));
  487. this.writeZigzagVarint32_(field, value);
  488. };
  489. /**
  490. * Writes a sint64 field to the buffer. Numbers outside the range [-2^63,2^63)
  491. * will be truncated.
  492. * @param {number} field The field number.
  493. * @param {number?} value The value to write.
  494. */
  495. jspb.BinaryWriter.prototype.writeSint64 = function(field, value) {
  496. if (value == null) return;
  497. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
  498. (value < jspb.BinaryConstants.TWO_TO_63));
  499. this.writeZigzagVarint64_(field, value);
  500. };
  501. /**
  502. * Writes a fixed32 field to the buffer. Numbers outside the range [0,2^32)
  503. * will be truncated.
  504. * @param {number} field The field number.
  505. * @param {number?} value The value to write.
  506. */
  507. jspb.BinaryWriter.prototype.writeFixed32 = function(field, value) {
  508. if (value == null) return;
  509. goog.asserts.assert((value >= 0) &&
  510. (value < jspb.BinaryConstants.TWO_TO_32));
  511. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32);
  512. this.encoder_.writeUint32(value);
  513. };
  514. /**
  515. * Writes a fixed64 field to the buffer. Numbers outside the range [0,2^64)
  516. * will be truncated.
  517. * @param {number} field The field number.
  518. * @param {number?} value The value to write.
  519. */
  520. jspb.BinaryWriter.prototype.writeFixed64 = function(field, value) {
  521. if (value == null) return;
  522. goog.asserts.assert((value >= 0) &&
  523. (value < jspb.BinaryConstants.TWO_TO_64));
  524. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  525. this.encoder_.writeUint64(value);
  526. };
  527. /**
  528. * Writes a sfixed32 field to the buffer. Numbers outside the range
  529. * [-2^31,2^31) will be truncated.
  530. * @param {number} field The field number.
  531. * @param {number?} value The value to write.
  532. */
  533. jspb.BinaryWriter.prototype.writeSfixed32 = function(field, value) {
  534. if (value == null) return;
  535. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
  536. (value < jspb.BinaryConstants.TWO_TO_31));
  537. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32);
  538. this.encoder_.writeInt32(value);
  539. };
  540. /**
  541. * Writes a sfixed64 field to the buffer. Numbers outside the range
  542. * [-2^63,2^63) will be truncated.
  543. * @param {number} field The field number.
  544. * @param {number?} value The value to write.
  545. */
  546. jspb.BinaryWriter.prototype.writeSfixed64 = function(field, value) {
  547. if (value == null) return;
  548. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
  549. (value < jspb.BinaryConstants.TWO_TO_63));
  550. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  551. this.encoder_.writeInt64(value);
  552. };
  553. /**
  554. * Writes a single-precision floating point field to the buffer. Numbers
  555. * requiring more than 32 bits of precision will be truncated.
  556. * @param {number} field The field number.
  557. * @param {number?} value The value to write.
  558. */
  559. jspb.BinaryWriter.prototype.writeFloat = function(field, value) {
  560. if (value == null) return;
  561. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32);
  562. this.encoder_.writeFloat(value);
  563. };
  564. /**
  565. * Writes a double-precision floating point field to the buffer. As this is the
  566. * native format used by JavaScript, no precision will be lost.
  567. * @param {number} field The field number.
  568. * @param {number?} value The value to write.
  569. */
  570. jspb.BinaryWriter.prototype.writeDouble = function(field, value) {
  571. if (value == null) return;
  572. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  573. this.encoder_.writeDouble(value);
  574. };
  575. /**
  576. * Writes a boolean field to the buffer.
  577. * @param {number} field The field number.
  578. * @param {boolean?} value The value to write.
  579. */
  580. jspb.BinaryWriter.prototype.writeBool = function(field, value) {
  581. if (value == null) return;
  582. goog.asserts.assert(goog.isBoolean(value));
  583. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  584. this.encoder_.writeBool(value);
  585. };
  586. /**
  587. * Writes an enum field to the buffer.
  588. * @param {number} field The field number.
  589. * @param {number?} value The value to write.
  590. */
  591. jspb.BinaryWriter.prototype.writeEnum = function(field, value) {
  592. if (value == null) return;
  593. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
  594. (value < jspb.BinaryConstants.TWO_TO_31));
  595. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  596. this.encoder_.writeSignedVarint32(value);
  597. };
  598. /**
  599. * Writes a string field to the buffer.
  600. * @param {number} field The field number.
  601. * @param {string?} value The string to write.
  602. */
  603. jspb.BinaryWriter.prototype.writeString = function(field, value) {
  604. if (value == null) return;
  605. var bookmark = this.beginDelimited_(field);
  606. this.encoder_.writeString(value);
  607. this.endDelimited_(bookmark);
  608. };
  609. /**
  610. * Writes an arbitrary byte field to the buffer. Note - to match the behavior
  611. * of the C++ implementation, empty byte arrays _are_ serialized.
  612. * @param {number} field The field number.
  613. * @param {?jspb.ByteSource} value The array of bytes to write.
  614. */
  615. jspb.BinaryWriter.prototype.writeBytes = function(field, value) {
  616. if (value == null) return;
  617. var bytes = jspb.utils.byteSourceToUint8Array(value);
  618. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  619. this.encoder_.writeUnsignedVarint32(bytes.length);
  620. this.appendUint8Array_(bytes);
  621. };
  622. /**
  623. * Writes a message to the buffer.
  624. * @template MessageType
  625. * @param {number} field The field number.
  626. * @param {?MessageType} value The message to write.
  627. * @param {!jspb.WriterFunction} writerCallback Will be invoked with the value
  628. * to write and the writer to write it with.
  629. */
  630. jspb.BinaryWriter.prototype.writeMessage = function(
  631. field, value, writerCallback) {
  632. if (value == null) return;
  633. var bookmark = this.beginDelimited_(field);
  634. writerCallback(value, this);
  635. this.endDelimited_(bookmark);
  636. };
  637. /**
  638. * Writes a group message to the buffer.
  639. *
  640. * @template MessageType
  641. * @param {number} field The field number.
  642. * @param {?MessageType} value The message to write, wrapped with START_GROUP /
  643. * END_GROUP tags. Will be a no-op if 'value' is null.
  644. * @param {!jspb.WriterFunction} writerCallback Will be invoked with the value
  645. * to write and the writer to write it with.
  646. */
  647. jspb.BinaryWriter.prototype.writeGroup = function(
  648. field, value, writerCallback) {
  649. if (value == null) return;
  650. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.START_GROUP);
  651. writerCallback(value, this);
  652. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.END_GROUP);
  653. };
  654. /**
  655. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  656. * the buffer.
  657. * @param {number} field The field number.
  658. * @param {string?} value The hash string.
  659. */
  660. jspb.BinaryWriter.prototype.writeFixedHash64 = function(field, value) {
  661. if (value == null) return;
  662. goog.asserts.assert(value.length == 8);
  663. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  664. this.encoder_.writeFixedHash64(value);
  665. };
  666. /**
  667. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  668. * the buffer.
  669. * @param {number} field The field number.
  670. * @param {string?} value The hash string.
  671. */
  672. jspb.BinaryWriter.prototype.writeVarintHash64 = function(field, value) {
  673. if (value == null) return;
  674. goog.asserts.assert(value.length == 8);
  675. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  676. this.encoder_.writeVarintHash64(value);
  677. };
  678. /**
  679. * Writes an array of numbers to the buffer as a repeated varint field.
  680. * @param {number} field The field number.
  681. * @param {?Array.<number>} value The array of ints to write.
  682. * @private
  683. */
  684. jspb.BinaryWriter.prototype.writeRepeatedUnsignedVarint32_ =
  685. function(field, value) {
  686. if (value == null) return;
  687. for (var i = 0; i < value.length; i++) {
  688. this.writeUnsignedVarint32_(field, value[i]);
  689. }
  690. };
  691. /**
  692. * Writes an array of numbers to the buffer as a repeated varint field.
  693. * @param {number} field The field number.
  694. * @param {?Array.<number>} value The array of ints to write.
  695. * @private
  696. */
  697. jspb.BinaryWriter.prototype.writeRepeatedSignedVarint32_ =
  698. function(field, value) {
  699. if (value == null) return;
  700. for (var i = 0; i < value.length; i++) {
  701. this.writeSignedVarint32_(field, value[i]);
  702. }
  703. };
  704. /**
  705. * Writes an array of numbers to the buffer as a repeated varint field.
  706. * @param {number} field The field number.
  707. * @param {?Array.<number>} value The array of ints to write.
  708. * @private
  709. */
  710. jspb.BinaryWriter.prototype.writeRepeatedUnsignedVarint64_ =
  711. function(field, value) {
  712. if (value == null) return;
  713. for (var i = 0; i < value.length; i++) {
  714. this.writeUnsignedVarint64_(field, value[i]);
  715. }
  716. };
  717. /**
  718. * Writes an array of numbers to the buffer as a repeated varint field.
  719. * @param {number} field The field number.
  720. * @param {?Array.<number>} value The array of ints to write.
  721. * @private
  722. */
  723. jspb.BinaryWriter.prototype.writeRepeatedSignedVarint64_ =
  724. function(field, value) {
  725. if (value == null) return;
  726. for (var i = 0; i < value.length; i++) {
  727. this.writeSignedVarint64_(field, value[i]);
  728. }
  729. };
  730. /**
  731. * Writes an array of numbers to the buffer as a repeated zigzag field.
  732. * @param {number} field The field number.
  733. * @param {?Array.<number>} value The array of ints to write.
  734. * @private
  735. */
  736. jspb.BinaryWriter.prototype.writeRepeatedZigzag32_ = function(field, value) {
  737. if (value == null) return;
  738. for (var i = 0; i < value.length; i++) {
  739. this.writeZigzagVarint32_(field, value[i]);
  740. }
  741. };
  742. /**
  743. * Writes an array of numbers to the buffer as a repeated zigzag field.
  744. * @param {number} field The field number.
  745. * @param {?Array.<number>} value The array of ints to write.
  746. * @private
  747. */
  748. jspb.BinaryWriter.prototype.writeRepeatedZigzag_ = function(field, value) {
  749. if (value == null) return;
  750. for (var i = 0; i < value.length; i++) {
  751. this.writeZigzagVarint64_(field, value[i]);
  752. }
  753. };
  754. /**
  755. * Writes an array of numbers to the buffer as a repeated 32-bit int field.
  756. * @param {number} field The field number.
  757. * @param {?Array.<number>} value The array of ints to write.
  758. */
  759. jspb.BinaryWriter.prototype.writeRepeatedInt32 =
  760. jspb.BinaryWriter.prototype.writeRepeatedSignedVarint32_;
  761. /**
  762. * Writes an array of numbers formatted as strings to the buffer as a repeated
  763. * 32-bit int field.
  764. * @param {number} field The field number.
  765. * @param {?Array.<string>} value The array of ints to write.
  766. */
  767. jspb.BinaryWriter.prototype.writeRepeatedInt32String =
  768. function(field, value) {
  769. if (value == null) return;
  770. for (var i = 0; i < value.length; i++) {
  771. this.writeInt32String(field, value[i]);
  772. }
  773. };
  774. /**
  775. * Writes an array of numbers to the buffer as a repeated 64-bit int field.
  776. * @param {number} field The field number.
  777. * @param {?Array.<number>} value The array of ints to write.
  778. */
  779. jspb.BinaryWriter.prototype.writeRepeatedInt64 =
  780. jspb.BinaryWriter.prototype.writeRepeatedSignedVarint64_;
  781. /**
  782. * Writes an array of numbers formatted as strings to the buffer as a repeated
  783. * 64-bit int field.
  784. * @param {number} field The field number.
  785. * @param {?Array.<string>} value The array of ints to write.
  786. */
  787. jspb.BinaryWriter.prototype.writeRepeatedInt64String =
  788. function(field, value) {
  789. if (value == null) return;
  790. for (var i = 0; i < value.length; i++) {
  791. this.writeInt64String(field, value[i]);
  792. }
  793. };
  794. /**
  795. * Writes an array numbers to the buffer as a repeated unsigned 32-bit int
  796. * field.
  797. * @param {number} field The field number.
  798. * @param {?Array.<number>} value The array of ints to write.
  799. */
  800. jspb.BinaryWriter.prototype.writeRepeatedUint32 =
  801. jspb.BinaryWriter.prototype.writeRepeatedUnsignedVarint32_;
  802. /**
  803. * Writes an array of numbers formatted as strings to the buffer as a repeated
  804. * unsigned 32-bit int field.
  805. * @param {number} field The field number.
  806. * @param {?Array.<string>} value The array of ints to write.
  807. */
  808. jspb.BinaryWriter.prototype.writeRepeatedUint32String =
  809. 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 =
  822. jspb.BinaryWriter.prototype.writeRepeatedUnsignedVarint64_;
  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 =
  830. function(field, value) {
  831. if (value == null) return;
  832. for (var i = 0; i < value.length; i++) {
  833. this.writeUint64String(field, value[i]);
  834. }
  835. };
  836. /**
  837. * Writes an array numbers to the buffer as a repeated signed 32-bit int field.
  838. * @param {number} field The field number.
  839. * @param {?Array.<number>} value The array of ints to write.
  840. */
  841. jspb.BinaryWriter.prototype.writeRepeatedSint32 =
  842. jspb.BinaryWriter.prototype.writeRepeatedZigzag32_;
  843. /**
  844. * Writes an array numbers to the buffer as a repeated signed 64-bit int field.
  845. * @param {number} field The field number.
  846. * @param {?Array.<number>} value The array of ints to write.
  847. */
  848. jspb.BinaryWriter.prototype.writeRepeatedSint64 =
  849. jspb.BinaryWriter.prototype.writeRepeatedZigzag_;
  850. /**
  851. * Writes an array of numbers to the buffer as a repeated fixed32 field. This
  852. * works for both signed and unsigned fixed32s.
  853. * @param {number} field The field number.
  854. * @param {?Array.<number>} value The array of ints to write.
  855. */
  856. jspb.BinaryWriter.prototype.writeRepeatedFixed32 = function(field, value) {
  857. if (value == null) return;
  858. for (var i = 0; i < value.length; i++) {
  859. this.writeFixed32(field, value[i]);
  860. }
  861. };
  862. /**
  863. * Writes an array of numbers to the buffer as a repeated fixed64 field. This
  864. * works for both signed and unsigned fixed64s.
  865. * @param {number} field The field number.
  866. * @param {?Array.<number>} value The array of ints to write.
  867. */
  868. jspb.BinaryWriter.prototype.writeRepeatedFixed64 = function(field, value) {
  869. if (value == null) return;
  870. for (var i = 0; i < value.length; i++) {
  871. this.writeFixed64(field, value[i]);
  872. }
  873. };
  874. /**
  875. * Writes an array of numbers to the buffer as a repeated sfixed32 field.
  876. * @param {number} field The field number.
  877. * @param {?Array.<number>} value The array of ints to write.
  878. */
  879. jspb.BinaryWriter.prototype.writeRepeatedSfixed32 = function(field, value) {
  880. if (value == null) return;
  881. for (var i = 0; i < value.length; i++) {
  882. this.writeSfixed32(field, value[i]);
  883. }
  884. };
  885. /**
  886. * Writes an array of numbers to the buffer as a repeated sfixed64 field.
  887. * @param {number} field The field number.
  888. * @param {?Array.<number>} value The array of ints to write.
  889. */
  890. jspb.BinaryWriter.prototype.writeRepeatedSfixed64 = function(field, value) {
  891. if (value == null) return;
  892. for (var i = 0; i < value.length; i++) {
  893. this.writeSfixed64(field, value[i]);
  894. }
  895. };
  896. /**
  897. * Writes an array of numbers to the buffer as a repeated float field.
  898. * @param {number} field The field number.
  899. * @param {?Array.<number>} value The array of ints to write.
  900. */
  901. jspb.BinaryWriter.prototype.writeRepeatedFloat = function(field, value) {
  902. if (value == null) return;
  903. for (var i = 0; i < value.length; i++) {
  904. this.writeFloat(field, value[i]);
  905. }
  906. };
  907. /**
  908. * Writes an array of numbers to the buffer as a repeated double field.
  909. * @param {number} field The field number.
  910. * @param {?Array.<number>} value The array of ints to write.
  911. */
  912. jspb.BinaryWriter.prototype.writeRepeatedDouble = function(field, value) {
  913. if (value == null) return;
  914. for (var i = 0; i < value.length; i++) {
  915. this.writeDouble(field, value[i]);
  916. }
  917. };
  918. /**
  919. * Writes an array of booleans to the buffer as a repeated bool field.
  920. * @param {number} field The field number.
  921. * @param {?Array.<boolean>} value The array of ints to write.
  922. */
  923. jspb.BinaryWriter.prototype.writeRepeatedBool = function(field, value) {
  924. if (value == null) return;
  925. for (var i = 0; i < value.length; i++) {
  926. this.writeBool(field, value[i]);
  927. }
  928. };
  929. /**
  930. * Writes an array of enums to the buffer as a repeated enum field.
  931. * @param {number} field The field number.
  932. * @param {?Array.<number>} value The array of ints to write.
  933. */
  934. jspb.BinaryWriter.prototype.writeRepeatedEnum = function(field, value) {
  935. if (value == null) return;
  936. for (var i = 0; i < value.length; i++) {
  937. this.writeEnum(field, value[i]);
  938. }
  939. };
  940. /**
  941. * Writes an array of strings to the buffer as a repeated string field.
  942. * @param {number} field The field number.
  943. * @param {?Array.<string>} value The array of strings to write.
  944. */
  945. jspb.BinaryWriter.prototype.writeRepeatedString = function(field, value) {
  946. if (value == null) return;
  947. for (var i = 0; i < value.length; i++) {
  948. this.writeString(field, value[i]);
  949. }
  950. };
  951. /**
  952. * Writes an array of arbitrary byte fields to the buffer.
  953. * @param {number} field The field number.
  954. * @param {?Array.<!jspb.ByteSource>} value The arrays of arrays of bytes to
  955. * write.
  956. */
  957. jspb.BinaryWriter.prototype.writeRepeatedBytes = function(field, value) {
  958. if (value == null) return;
  959. for (var i = 0; i < value.length; i++) {
  960. this.writeBytes(field, value[i]);
  961. }
  962. };
  963. /**
  964. * Writes an array of messages to the buffer.
  965. * @template MessageType
  966. * @param {number} field The field number.
  967. * @param {?Array.<MessageType>} value The array of messages to
  968. * write.
  969. * @param {!jspb.WriterFunction} writerCallback Will be invoked with the value
  970. * to write and the writer to write it with.
  971. */
  972. jspb.BinaryWriter.prototype.writeRepeatedMessage = function(
  973. field, value, writerCallback) {
  974. if (value == null) return;
  975. for (var i = 0; i < value.length; i++) {
  976. var bookmark = this.beginDelimited_(field);
  977. writerCallback(value[i], this);
  978. this.endDelimited_(bookmark);
  979. }
  980. };
  981. /**
  982. * Writes an array of group messages to the buffer.
  983. * @template MessageType
  984. * @param {number} field The field number.
  985. * @param {?Array.<MessageType>} value The array of messages to
  986. * write.
  987. * @param {!jspb.WriterFunction} writerCallback Will be invoked with the value
  988. * to write and the writer to write it with.
  989. */
  990. jspb.BinaryWriter.prototype.writeRepeatedGroup = function(
  991. field, value, writerCallback) {
  992. if (value == null) return;
  993. for (var i = 0; i < value.length; i++) {
  994. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.START_GROUP);
  995. writerCallback(value[i], this);
  996. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.END_GROUP);
  997. }
  998. };
  999. /**
  1000. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  1001. * the buffer.
  1002. * @param {number} field The field number.
  1003. * @param {?Array.<string>} value The array of hashes to write.
  1004. */
  1005. jspb.BinaryWriter.prototype.writeRepeatedFixedHash64 =
  1006. function(field, value) {
  1007. if (value == null) return;
  1008. for (var i = 0; i < value.length; i++) {
  1009. this.writeFixedHash64(field, value[i]);
  1010. }
  1011. };
  1012. /**
  1013. * Writes a repeated 64-bit hash string field (8 characters @ 8 bits of data
  1014. * each) to the buffer.
  1015. * @param {number} field The field number.
  1016. * @param {?Array.<string>} value The array of hashes to write.
  1017. */
  1018. jspb.BinaryWriter.prototype.writeRepeatedVarintHash64 =
  1019. function(field, value) {
  1020. if (value == null) return;
  1021. for (var i = 0; i < value.length; i++) {
  1022. this.writeVarintHash64(field, value[i]);
  1023. }
  1024. };
  1025. /**
  1026. * Writes an array of numbers to the buffer as a packed varint field.
  1027. * @param {number} field The field number.
  1028. * @param {?Array.<number>} value The array of ints to write.
  1029. * @private
  1030. */
  1031. jspb.BinaryWriter.prototype.writePackedUnsignedVarint32_ = function(
  1032. field, value) {
  1033. if (value == null || !value.length) return;
  1034. var bookmark = this.beginDelimited_(field);
  1035. for (var i = 0; i < value.length; i++) {
  1036. this.encoder_.writeUnsignedVarint32(value[i]);
  1037. }
  1038. this.endDelimited_(bookmark);
  1039. };
  1040. /**
  1041. * Writes an array of numbers to the buffer as a packed varint field.
  1042. * @param {number} field The field number.
  1043. * @param {?Array.<number>} value The array of ints to write.
  1044. * @private
  1045. */
  1046. jspb.BinaryWriter.prototype.writePackedSignedVarint32_ = function(
  1047. field, value) {
  1048. if (value == null || !value.length) return;
  1049. var bookmark = this.beginDelimited_(field);
  1050. for (var i = 0; i < value.length; i++) {
  1051. this.encoder_.writeSignedVarint32(value[i]);
  1052. }
  1053. this.endDelimited_(bookmark);
  1054. };
  1055. /**
  1056. * Writes an array of numbers to the buffer as a packed varint field.
  1057. * @param {number} field The field number.
  1058. * @param {?Array.<number>} value The array of ints to write.
  1059. * @private
  1060. */
  1061. jspb.BinaryWriter.prototype.writePackedUnsignedVarint64_ = function(
  1062. field, value) {
  1063. if (value == null || !value.length) return;
  1064. var bookmark = this.beginDelimited_(field);
  1065. for (var i = 0; i < value.length; i++) {
  1066. this.encoder_.writeUnsignedVarint64(value[i]);
  1067. }
  1068. this.endDelimited_(bookmark);
  1069. };
  1070. /**
  1071. * Writes an array of numbers to the buffer as a packed varint field.
  1072. * @param {number} field The field number.
  1073. * @param {?Array.<number>} value The array of ints to write.
  1074. * @private
  1075. */
  1076. jspb.BinaryWriter.prototype.writePackedSignedVarint64_ = function(
  1077. 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_.writeSignedVarint64(value[i]);
  1082. }
  1083. this.endDelimited_(bookmark);
  1084. };
  1085. /**
  1086. * Writes an array of numbers to the buffer as a packed zigzag field.
  1087. * @param {number} field The field number.
  1088. * @param {?Array.<number>} value The array of ints to write.
  1089. * @private
  1090. */
  1091. jspb.BinaryWriter.prototype.writePackedZigzag32_ = 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_.writeZigzagVarint32(value[i]);
  1096. }
  1097. this.endDelimited_(bookmark);
  1098. };
  1099. /**
  1100. * Writes an array of numbers to the buffer as a packed zigzag field.
  1101. * @param {number} field The field number.
  1102. * @param {?Array.<number>} value The array of ints to write.
  1103. * @private
  1104. */
  1105. jspb.BinaryWriter.prototype.writePackedZigzag64_ = function(field, value) {
  1106. if (value == null || !value.length) return;
  1107. var bookmark = this.beginDelimited_(field);
  1108. for (var i = 0; i < value.length; i++) {
  1109. this.encoder_.writeZigzagVarint64(value[i]);
  1110. }
  1111. this.endDelimited_(bookmark);
  1112. };
  1113. /**
  1114. * Writes an array of numbers to the buffer as a packed 32-bit int field.
  1115. * @param {number} field The field number.
  1116. * @param {?Array.<number>} value The array of ints to write.
  1117. */
  1118. jspb.BinaryWriter.prototype.writePackedInt32 =
  1119. jspb.BinaryWriter.prototype.writePackedSignedVarint32_;
  1120. /**
  1121. * Writes an array of numbers represented as strings to the buffer as a packed
  1122. * 32-bit int field.
  1123. * @param {number} field
  1124. * @param {?Array.<string>} value
  1125. */
  1126. jspb.BinaryWriter.prototype.writePackedInt32String = function(field, value) {
  1127. if (value == null || !value.length) return;
  1128. var bookmark = this.beginDelimited_(field);
  1129. for (var i = 0; i < value.length; i++) {
  1130. this.encoder_.writeSignedVarint32(parseInt(value[i], 10));
  1131. }
  1132. this.endDelimited_(bookmark);
  1133. };
  1134. /**
  1135. * Writes an array of numbers to the buffer as a packed 64-bit int field.
  1136. * @param {number} field The field number.
  1137. * @param {?Array.<number>} value The array of ints to write.
  1138. */
  1139. jspb.BinaryWriter.prototype.writePackedInt64 =
  1140. jspb.BinaryWriter.prototype.writePackedSignedVarint64_;
  1141. /**
  1142. * Writes an array of numbers represented as strings to the buffer as a packed
  1143. * 64-bit int field.
  1144. * @param {number} field
  1145. * @param {?Array.<string>} value
  1146. */
  1147. jspb.BinaryWriter.prototype.writePackedInt64String =
  1148. function(field, value) {
  1149. if (value == null || !value.length) return;
  1150. var bookmark = this.beginDelimited_(field);
  1151. for (var i = 0; i < value.length; i++) {
  1152. var num = jspb.arith.Int64.fromString(value[i]);
  1153. this.encoder_.writeSplitVarint64(num.lo, num.hi);
  1154. }
  1155. this.endDelimited_(bookmark);
  1156. };
  1157. /**
  1158. * Writes an array numbers to the buffer as a packed unsigned 32-bit int field.
  1159. * @param {number} field The field number.
  1160. * @param {?Array.<number>} value The array of ints to write.
  1161. */
  1162. jspb.BinaryWriter.prototype.writePackedUint32 =
  1163. jspb.BinaryWriter.prototype.writePackedUnsignedVarint32_;
  1164. /**
  1165. * Writes an array of numbers represented as strings to the buffer as a packed
  1166. * unsigned 32-bit int field.
  1167. * @param {number} field
  1168. * @param {?Array.<string>} value
  1169. */
  1170. jspb.BinaryWriter.prototype.writePackedUint32String =
  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. this.encoder_.writeUnsignedVarint32(parseInt(value[i], 10));
  1176. }
  1177. this.endDelimited_(bookmark);
  1178. };
  1179. /**
  1180. * Writes an array numbers to the buffer as a packed unsigned 64-bit int field.
  1181. * @param {number} field The field number.
  1182. * @param {?Array.<number>} value The array of ints to write.
  1183. */
  1184. jspb.BinaryWriter.prototype.writePackedUint64 =
  1185. jspb.BinaryWriter.prototype.writePackedUnsignedVarint64_;
  1186. /**
  1187. * Writes an array of numbers represented as strings to the buffer as a packed
  1188. * unsigned 64-bit int field.
  1189. * @param {number} field
  1190. * @param {?Array.<string>} value
  1191. */
  1192. jspb.BinaryWriter.prototype.writePackedUint64String =
  1193. function(field, value) {
  1194. if (value == null || !value.length) return;
  1195. var bookmark = this.beginDelimited_(field);
  1196. for (var i = 0; i < value.length; i++) {
  1197. var num = jspb.arith.UInt64.fromString(value[i]);
  1198. this.encoder_.writeSplitVarint64(num.lo, num.hi);
  1199. }
  1200. this.endDelimited_(bookmark);
  1201. };
  1202. /**
  1203. * Writes an array numbers to the buffer as a packed signed 32-bit int field.
  1204. * @param {number} field The field number.
  1205. * @param {?Array.<number>} value The array of ints to write.
  1206. */
  1207. jspb.BinaryWriter.prototype.writePackedSint32 =
  1208. jspb.BinaryWriter.prototype.writePackedZigzag32_;
  1209. /**
  1210. * Writes an array numbers to the buffer as a packed signed 64-bit int field.
  1211. * @param {number} field The field number.
  1212. * @param {?Array.<number>} value The array of ints to write.
  1213. */
  1214. jspb.BinaryWriter.prototype.writePackedSint64 =
  1215. jspb.BinaryWriter.prototype.writePackedZigzag64_;
  1216. /**
  1217. * Writes an array of numbers to the buffer as a packed fixed32 field.
  1218. * @param {number} field The field number.
  1219. * @param {?Array.<number>} value The array of ints to write.
  1220. */
  1221. jspb.BinaryWriter.prototype.writePackedFixed32 = function(field, value) {
  1222. if (value == null || !value.length) return;
  1223. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1224. this.encoder_.writeUnsignedVarint32(value.length * 4);
  1225. for (var i = 0; i < value.length; i++) {
  1226. this.encoder_.writeUint32(value[i]);
  1227. }
  1228. };
  1229. /**
  1230. * Writes an array of numbers to the buffer as a packed fixed64 field.
  1231. * @param {number} field The field number.
  1232. * @param {?Array.<number>} value The array of ints to write.
  1233. */
  1234. jspb.BinaryWriter.prototype.writePackedFixed64 = function(field, value) {
  1235. if (value == null || !value.length) return;
  1236. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1237. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1238. for (var i = 0; i < value.length; i++) {
  1239. this.encoder_.writeUint64(value[i]);
  1240. }
  1241. };
  1242. /**
  1243. * Writes an array of numbers to the buffer as a packed sfixed32 field.
  1244. * @param {number} field The field number.
  1245. * @param {?Array.<number>} value The array of ints to write.
  1246. */
  1247. jspb.BinaryWriter.prototype.writePackedSfixed32 = function(field, value) {
  1248. if (value == null || !value.length) return;
  1249. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1250. this.encoder_.writeUnsignedVarint32(value.length * 4);
  1251. for (var i = 0; i < value.length; i++) {
  1252. this.encoder_.writeInt32(value[i]);
  1253. }
  1254. };
  1255. /**
  1256. * Writes an array of numbers to the buffer as a packed sfixed64 field.
  1257. * @param {number} field The field number.
  1258. * @param {?Array.<number>} value The array of ints to write.
  1259. */
  1260. jspb.BinaryWriter.prototype.writePackedSfixed64 = function(field, value) {
  1261. if (value == null || !value.length) return;
  1262. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1263. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1264. for (var i = 0; i < value.length; i++) {
  1265. this.encoder_.writeInt64(value[i]);
  1266. }
  1267. };
  1268. /**
  1269. * Writes an array of numbers to the buffer as a packed float field.
  1270. * @param {number} field The field number.
  1271. * @param {?Array.<number>} value The array of ints to write.
  1272. */
  1273. jspb.BinaryWriter.prototype.writePackedFloat = function(field, value) {
  1274. if (value == null || !value.length) return;
  1275. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1276. this.encoder_.writeUnsignedVarint32(value.length * 4);
  1277. for (var i = 0; i < value.length; i++) {
  1278. this.encoder_.writeFloat(value[i]);
  1279. }
  1280. };
  1281. /**
  1282. * Writes an array of numbers to the buffer as a packed double field.
  1283. * @param {number} field The field number.
  1284. * @param {?Array.<number>} value The array of ints to write.
  1285. */
  1286. jspb.BinaryWriter.prototype.writePackedDouble = function(field, value) {
  1287. if (value == null || !value.length) return;
  1288. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1289. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1290. for (var i = 0; i < value.length; i++) {
  1291. this.encoder_.writeDouble(value[i]);
  1292. }
  1293. };
  1294. /**
  1295. * Writes an array of booleans to the buffer as a packed bool field.
  1296. * @param {number} field The field number.
  1297. * @param {?Array.<boolean>} value The array of ints to write.
  1298. */
  1299. jspb.BinaryWriter.prototype.writePackedBool = function(field, value) {
  1300. if (value == null || !value.length) return;
  1301. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1302. this.encoder_.writeUnsignedVarint32(value.length);
  1303. for (var i = 0; i < value.length; i++) {
  1304. this.encoder_.writeBool(value[i]);
  1305. }
  1306. };
  1307. /**
  1308. * Writes an array of enums to the buffer as a packed enum field.
  1309. * @param {number} field The field number.
  1310. * @param {?Array.<number>} value The array of ints to write.
  1311. */
  1312. jspb.BinaryWriter.prototype.writePackedEnum = function(field, value) {
  1313. if (value == null || !value.length) return;
  1314. var bookmark = this.beginDelimited_(field);
  1315. for (var i = 0; i < value.length; i++) {
  1316. this.encoder_.writeEnum(value[i]);
  1317. }
  1318. this.endDelimited_(bookmark);
  1319. };
  1320. /**
  1321. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  1322. * the buffer.
  1323. * @param {number} field The field number.
  1324. * @param {?Array.<string>} value The array of hashes to write.
  1325. */
  1326. jspb.BinaryWriter.prototype.writePackedFixedHash64 = function(field, value) {
  1327. if (value == null || !value.length) return;
  1328. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1329. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1330. for (var i = 0; i < value.length; i++) {
  1331. this.encoder_.writeFixedHash64(value[i]);
  1332. }
  1333. };
  1334. /**
  1335. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  1336. * the buffer.
  1337. * @param {number} field The field number.
  1338. * @param {?Array.<string>} value The array of hashes to write.
  1339. */
  1340. jspb.BinaryWriter.prototype.writePackedVarintHash64 = function(field, value) {
  1341. if (value == null || !value.length) return;
  1342. var bookmark = this.beginDelimited_(field);
  1343. for (var i = 0; i < value.length; i++) {
  1344. this.encoder_.writeVarintHash64(value[i]);
  1345. }
  1346. this.endDelimited_(bookmark);
  1347. };