writer.js 50 KB

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