writer.js 59 KB

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