writer.js 51 KB

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