writer.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /**
  2. * @fileoverview Implements Writer for writing data as the binary wire format
  3. * bytes array.
  4. */
  5. goog.module('protobuf.binary.Writer');
  6. const BufferDecoder = goog.require('protobuf.binary.BufferDecoder');
  7. const ByteString = goog.require('protobuf.ByteString');
  8. const Int64 = goog.require('protobuf.Int64');
  9. const WireType = goog.require('protobuf.binary.WireType');
  10. const {POLYFILL_TEXT_ENCODING, checkFieldNumber, checkTypeUnsignedInt32, checkWireType} = goog.require('protobuf.internal.checks');
  11. const {concatenateByteArrays} = goog.require('protobuf.binary.uint8arrays');
  12. const {createTag, getTagLength} = goog.require('protobuf.binary.tag');
  13. const {encode} = goog.require('protobuf.binary.textencoding');
  14. /**
  15. * Returns a valid utf-8 encoder function based on TextEncoder if available or
  16. * a polyfill.
  17. * Some of the environments we run in do not have TextEncoder defined.
  18. * TextEncoder is faster than our polyfill so we prefer it over the polyfill.
  19. * @return {function(string):!Uint8Array}
  20. */
  21. function getEncoderFunction() {
  22. if (goog.global['TextEncoder']) {
  23. const textEncoder = new goog.global['TextEncoder']('utf-8');
  24. return s => s.length === 0 ? new Uint8Array(0) : textEncoder.encode(s);
  25. }
  26. if (POLYFILL_TEXT_ENCODING) {
  27. return encode;
  28. } else {
  29. throw new Error(
  30. 'TextEncoder is missing. ' +
  31. 'Enable protobuf.defines.POLYFILL_TEXT_ENCODING');
  32. }
  33. }
  34. /** @const {function(string): !Uint8Array} */
  35. const encoderFunction = getEncoderFunction();
  36. /**
  37. * Writer provides methods for encoding all protobuf supported type into a
  38. * binary format bytes array.
  39. * Check https://developers.google.com/protocol-buffers/docs/encoding for binary
  40. * format definition.
  41. * @final
  42. * @package
  43. */
  44. class Writer {
  45. constructor() {
  46. /**
  47. * Blocks of data that needs to be serialized. After writing all the data,
  48. * the blocks are concatenated into a single Uint8Array.
  49. * @private {!Array<!Uint8Array>}
  50. */
  51. this.blocks_ = [];
  52. /**
  53. * A buffer for writing varint data (tag number + field number for each
  54. * field, int32, uint32 etc.). Before writing a non-varint data block
  55. * (string, fixed32 etc.), the buffer is appended to the block array as a
  56. * new block, and a new buffer is started.
  57. *
  58. * We could've written each varint as a new block instead of writing
  59. * multiple varints in this buffer. But this will increase the number of
  60. * blocks, and concatenating many small blocks is slower than concatenating
  61. * few large blocks.
  62. *
  63. * TODO: Experiment with writing data in a fixed-length
  64. * Uint8Array instead of using a growing buffer.
  65. *
  66. * @private {!Array<number>}
  67. */
  68. this.currentBuffer_ = [];
  69. }
  70. /**
  71. * Converts the encoded data into a Uint8Array.
  72. * The writer is also reset.
  73. * @return {!ArrayBuffer}
  74. */
  75. getAndResetResultBuffer() {
  76. this.closeAndStartNewBuffer_();
  77. const result = concatenateByteArrays(this.blocks_);
  78. this.blocks_ = [];
  79. return result.buffer;
  80. }
  81. /**
  82. * Encodes a (field number, wire type) tuple into a wire-format field header.
  83. * @param {number} fieldNumber
  84. * @param {!WireType} wireType
  85. */
  86. writeTag(fieldNumber, wireType) {
  87. checkFieldNumber(fieldNumber);
  88. checkWireType(wireType);
  89. const tag = createTag(wireType, fieldNumber);
  90. this.writeUnsignedVarint32_(tag);
  91. }
  92. /**
  93. * Appends the current buffer into the blocks array and starts a new buffer.
  94. * @private
  95. */
  96. closeAndStartNewBuffer_() {
  97. this.blocks_.push(new Uint8Array(this.currentBuffer_));
  98. this.currentBuffer_ = [];
  99. }
  100. /**
  101. * Encodes a 32-bit integer into its wire-format varint representation and
  102. * stores it in the buffer.
  103. * @param {number} value
  104. * @private
  105. */
  106. writeUnsignedVarint32_(value) {
  107. checkTypeUnsignedInt32(value);
  108. while (value > 0x7f) {
  109. this.currentBuffer_.push((value & 0x7f) | 0x80);
  110. value = value >>> 7;
  111. }
  112. this.currentBuffer_.push(value);
  113. }
  114. /****************************************************************************
  115. * OPTIONAL METHODS
  116. ****************************************************************************/
  117. /**
  118. * Writes a boolean value field to the buffer as a varint.
  119. * @param {boolean} value
  120. * @private
  121. */
  122. writeBoolValue_(value) {
  123. this.currentBuffer_.push(value ? 1 : 0);
  124. }
  125. /**
  126. * Writes a boolean value field to the buffer as a varint.
  127. * @param {number} fieldNumber
  128. * @param {boolean} value
  129. */
  130. writeBool(fieldNumber, value) {
  131. this.writeTag(fieldNumber, WireType.VARINT);
  132. this.writeBoolValue_(value);
  133. }
  134. /**
  135. * Writes a bytes value field to the buffer as a length delimited field.
  136. * @param {number} fieldNumber
  137. * @param {!ByteString} value
  138. */
  139. writeBytes(fieldNumber, value) {
  140. this.writeTag(fieldNumber, WireType.DELIMITED);
  141. const buffer = value.toArrayBuffer();
  142. this.writeUnsignedVarint32_(buffer.byteLength);
  143. this.writeRaw_(buffer);
  144. }
  145. /**
  146. * Writes a double value field to the buffer without tag.
  147. * @param {number} value
  148. * @private
  149. */
  150. writeDoubleValue_(value) {
  151. const buffer = new ArrayBuffer(8);
  152. const view = new DataView(buffer);
  153. view.setFloat64(0, value, true);
  154. this.writeRaw_(buffer);
  155. }
  156. /**
  157. * Writes a double value field to the buffer.
  158. * @param {number} fieldNumber
  159. * @param {number} value
  160. */
  161. writeDouble(fieldNumber, value) {
  162. this.writeTag(fieldNumber, WireType.FIXED64);
  163. this.writeDoubleValue_(value);
  164. }
  165. /**
  166. * Writes a fixed32 value field to the buffer without tag.
  167. * @param {number} value
  168. * @private
  169. */
  170. writeFixed32Value_(value) {
  171. const buffer = new ArrayBuffer(4);
  172. const view = new DataView(buffer);
  173. view.setUint32(0, value, true);
  174. this.writeRaw_(buffer);
  175. }
  176. /**
  177. * Writes a fixed32 value field to the buffer.
  178. * @param {number} fieldNumber
  179. * @param {number} value
  180. */
  181. writeFixed32(fieldNumber, value) {
  182. this.writeTag(fieldNumber, WireType.FIXED32);
  183. this.writeFixed32Value_(value);
  184. }
  185. /**
  186. * Writes a float value field to the buffer without tag.
  187. * @param {number} value
  188. * @private
  189. */
  190. writeFloatValue_(value) {
  191. const buffer = new ArrayBuffer(4);
  192. const view = new DataView(buffer);
  193. view.setFloat32(0, value, true);
  194. this.writeRaw_(buffer);
  195. }
  196. /**
  197. * Writes a float value field to the buffer.
  198. * @param {number} fieldNumber
  199. * @param {number} value
  200. */
  201. writeFloat(fieldNumber, value) {
  202. this.writeTag(fieldNumber, WireType.FIXED32);
  203. this.writeFloatValue_(value);
  204. }
  205. /**
  206. * Writes a int32 value field to the buffer as a varint without tag.
  207. * @param {number} value
  208. * @private
  209. */
  210. writeInt32Value_(value) {
  211. if (value >= 0) {
  212. this.writeVarint64_(0, value);
  213. } else {
  214. this.writeVarint64_(0xFFFFFFFF, value);
  215. }
  216. }
  217. /**
  218. * Writes a int32 value field to the buffer as a varint.
  219. * @param {number} fieldNumber
  220. * @param {number} value
  221. */
  222. writeInt32(fieldNumber, value) {
  223. this.writeTag(fieldNumber, WireType.VARINT);
  224. this.writeInt32Value_(value);
  225. }
  226. /**
  227. * Writes a int64 value field to the buffer as a varint.
  228. * @param {number} fieldNumber
  229. * @param {!Int64} value
  230. */
  231. writeInt64(fieldNumber, value) {
  232. this.writeTag(fieldNumber, WireType.VARINT);
  233. this.writeVarint64_(value.getHighBits(), value.getLowBits());
  234. }
  235. /**
  236. * Writes a sfixed32 value field to the buffer.
  237. * @param {number} value
  238. * @private
  239. */
  240. writeSfixed32Value_(value) {
  241. const buffer = new ArrayBuffer(4);
  242. const view = new DataView(buffer);
  243. view.setInt32(0, value, true);
  244. this.writeRaw_(buffer);
  245. }
  246. /**
  247. * Writes a sfixed32 value field to the buffer.
  248. * @param {number} fieldNumber
  249. * @param {number} value
  250. */
  251. writeSfixed32(fieldNumber, value) {
  252. this.writeTag(fieldNumber, WireType.FIXED32);
  253. this.writeSfixed32Value_(value);
  254. }
  255. /**
  256. * Writes a sfixed64 value field to the buffer without tag.
  257. * @param {!Int64} value
  258. * @private
  259. */
  260. writeSfixed64Value_(value) {
  261. const buffer = new ArrayBuffer(8);
  262. const view = new DataView(buffer);
  263. view.setInt32(0, value.getLowBits(), true);
  264. view.setInt32(4, value.getHighBits(), true);
  265. this.writeRaw_(buffer);
  266. }
  267. /**
  268. * Writes a sfixed64 value field to the buffer.
  269. * @param {number} fieldNumber
  270. * @param {!Int64} value
  271. */
  272. writeSfixed64(fieldNumber, value) {
  273. this.writeTag(fieldNumber, WireType.FIXED64);
  274. this.writeSfixed64Value_(value);
  275. }
  276. /**
  277. * Writes a sfixed64 value field to the buffer.
  278. * @param {number} fieldNumber
  279. */
  280. writeStartGroup(fieldNumber) {
  281. this.writeTag(fieldNumber, WireType.START_GROUP);
  282. }
  283. /**
  284. * Writes a sfixed64 value field to the buffer.
  285. * @param {number} fieldNumber
  286. */
  287. writeEndGroup(fieldNumber) {
  288. this.writeTag(fieldNumber, WireType.END_GROUP);
  289. }
  290. /**
  291. * Writes a uint32 value field to the buffer as a varint without tag.
  292. * @param {number} value
  293. * @private
  294. */
  295. writeUint32Value_(value) {
  296. this.writeVarint64_(0, value);
  297. }
  298. /**
  299. * Writes a uint32 value field to the buffer as a varint.
  300. * @param {number} fieldNumber
  301. * @param {number} value
  302. */
  303. writeUint32(fieldNumber, value) {
  304. this.writeTag(fieldNumber, WireType.VARINT);
  305. this.writeUint32Value_(value);
  306. }
  307. /**
  308. * Writes the bits of a 64 bit number to the buffer as a varint.
  309. * @param {number} highBits
  310. * @param {number} lowBits
  311. * @private
  312. */
  313. writeVarint64_(highBits, lowBits) {
  314. for (let i = 0; i < 28; i = i + 7) {
  315. const shift = lowBits >>> i;
  316. const hasNext = !((shift >>> 7) === 0 && highBits === 0);
  317. const byte = (hasNext ? shift | 0x80 : shift) & 0xFF;
  318. this.currentBuffer_.push(byte);
  319. if (!hasNext) {
  320. return;
  321. }
  322. }
  323. const splitBits = ((lowBits >>> 28) & 0x0F) | ((highBits & 0x07) << 4);
  324. const hasMoreBits = !((highBits >> 3) === 0);
  325. this.currentBuffer_.push(
  326. (hasMoreBits ? splitBits | 0x80 : splitBits) & 0xFF);
  327. if (!hasMoreBits) {
  328. return;
  329. }
  330. for (let i = 3; i < 31; i = i + 7) {
  331. const shift = highBits >>> i;
  332. const hasNext = !((shift >>> 7) === 0);
  333. const byte = (hasNext ? shift | 0x80 : shift) & 0xFF;
  334. this.currentBuffer_.push(byte);
  335. if (!hasNext) {
  336. return;
  337. }
  338. }
  339. this.currentBuffer_.push((highBits >>> 31) & 0x01);
  340. }
  341. /**
  342. * Writes a sint32 value field to the buffer as a varint without tag.
  343. * @param {number} value
  344. * @private
  345. */
  346. writeSint32Value_(value) {
  347. value = (value << 1) ^ (value >> 31);
  348. this.writeVarint64_(0, value);
  349. }
  350. /**
  351. * Writes a sint32 value field to the buffer as a varint.
  352. * @param {number} fieldNumber
  353. * @param {number} value
  354. */
  355. writeSint32(fieldNumber, value) {
  356. this.writeTag(fieldNumber, WireType.VARINT);
  357. this.writeSint32Value_(value);
  358. }
  359. /**
  360. * Writes a sint64 value field to the buffer as a varint without tag.
  361. * @param {!Int64} value
  362. * @private
  363. */
  364. writeSint64Value_(value) {
  365. const highBits = value.getHighBits();
  366. const lowBits = value.getLowBits();
  367. const sign = highBits >> 31;
  368. const encodedLowBits = (lowBits << 1) ^ sign;
  369. const encodedHighBits = ((highBits << 1) | (lowBits >>> 31)) ^ sign;
  370. this.writeVarint64_(encodedHighBits, encodedLowBits);
  371. }
  372. /**
  373. * Writes a sint64 value field to the buffer as a varint.
  374. * @param {number} fieldNumber
  375. * @param {!Int64} value
  376. */
  377. writeSint64(fieldNumber, value) {
  378. this.writeTag(fieldNumber, WireType.VARINT);
  379. this.writeSint64Value_(value);
  380. }
  381. /**
  382. * Writes a string value field to the buffer as a varint.
  383. * @param {number} fieldNumber
  384. * @param {string} value
  385. */
  386. writeString(fieldNumber, value) {
  387. this.writeTag(fieldNumber, WireType.DELIMITED);
  388. const array = encoderFunction(value);
  389. this.writeUnsignedVarint32_(array.length);
  390. this.closeAndStartNewBuffer_();
  391. this.blocks_.push(array);
  392. }
  393. /**
  394. * Writes raw bytes to the buffer.
  395. * @param {!ArrayBuffer} arrayBuffer
  396. * @private
  397. */
  398. writeRaw_(arrayBuffer) {
  399. this.closeAndStartNewBuffer_();
  400. this.blocks_.push(new Uint8Array(arrayBuffer));
  401. }
  402. /**
  403. * Writes raw bytes to the buffer.
  404. * @param {!BufferDecoder} bufferDecoder
  405. * @param {number} start
  406. * @param {!WireType} wireType
  407. * @param {number} fieldNumber
  408. * @package
  409. */
  410. writeBufferDecoder(bufferDecoder, start, wireType, fieldNumber) {
  411. this.closeAndStartNewBuffer_();
  412. const dataLength =
  413. getTagLength(bufferDecoder, start, wireType, fieldNumber);
  414. this.blocks_.push(
  415. bufferDecoder.subBufferDecoder(start, dataLength).asUint8Array());
  416. }
  417. /**
  418. * Write the whole bytes as a length delimited field.
  419. * @param {number} fieldNumber
  420. * @param {!ArrayBuffer} arrayBuffer
  421. */
  422. writeDelimited(fieldNumber, arrayBuffer) {
  423. this.writeTag(fieldNumber, WireType.DELIMITED);
  424. this.writeUnsignedVarint32_(arrayBuffer.byteLength);
  425. this.writeRaw_(arrayBuffer);
  426. }
  427. /****************************************************************************
  428. * REPEATED METHODS
  429. ****************************************************************************/
  430. /**
  431. * Writes repeated boolean values to the buffer as unpacked varints.
  432. * @param {number} fieldNumber
  433. * @param {!Array<boolean>} values
  434. */
  435. writeRepeatedBool(fieldNumber, values) {
  436. values.forEach(val => this.writeBool(fieldNumber, val));
  437. }
  438. /**
  439. * Writes repeated boolean values to the buffer as packed varints.
  440. * @param {number} fieldNumber
  441. * @param {!Array<boolean>} values
  442. */
  443. writePackedBool(fieldNumber, values) {
  444. this.writeFixedPacked_(
  445. fieldNumber, values, val => this.writeBoolValue_(val), 1);
  446. }
  447. /**
  448. * Writes repeated double values to the buffer as unpacked fixed64.
  449. * @param {number} fieldNumber
  450. * @param {!Array<number>} values
  451. */
  452. writeRepeatedDouble(fieldNumber, values) {
  453. values.forEach(val => this.writeDouble(fieldNumber, val));
  454. }
  455. /**
  456. * Writes repeated double values to the buffer as packed fixed64.
  457. * @param {number} fieldNumber
  458. * @param {!Array<number>} values
  459. */
  460. writePackedDouble(fieldNumber, values) {
  461. this.writeFixedPacked_(
  462. fieldNumber, values, val => this.writeDoubleValue_(val), 8);
  463. }
  464. /**
  465. * Writes repeated fixed32 values to the buffer as unpacked fixed32.
  466. * @param {number} fieldNumber
  467. * @param {!Array<number>} values
  468. */
  469. writeRepeatedFixed32(fieldNumber, values) {
  470. values.forEach(val => this.writeFixed32(fieldNumber, val));
  471. }
  472. /**
  473. * Writes repeated fixed32 values to the buffer as packed fixed32.
  474. * @param {number} fieldNumber
  475. * @param {!Array<number>} values
  476. */
  477. writePackedFixed32(fieldNumber, values) {
  478. this.writeFixedPacked_(
  479. fieldNumber, values, val => this.writeFixed32Value_(val), 4);
  480. }
  481. /**
  482. * Writes repeated float values to the buffer as unpacked fixed64.
  483. * @param {number} fieldNumber
  484. * @param {!Array<number>} values
  485. */
  486. writeRepeatedFloat(fieldNumber, values) {
  487. values.forEach(val => this.writeFloat(fieldNumber, val));
  488. }
  489. /**
  490. * Writes repeated float values to the buffer as packed fixed64.
  491. * @param {number} fieldNumber
  492. * @param {!Array<number>} values
  493. */
  494. writePackedFloat(fieldNumber, values) {
  495. this.writeFixedPacked_(
  496. fieldNumber, values, val => this.writeFloatValue_(val), 4);
  497. }
  498. /**
  499. * Writes repeated int32 values to the buffer as unpacked int32.
  500. * @param {number} fieldNumber
  501. * @param {!Array<number>} values
  502. */
  503. writeRepeatedInt32(fieldNumber, values) {
  504. values.forEach(val => this.writeInt32(fieldNumber, val));
  505. }
  506. /**
  507. * Writes repeated int32 values to the buffer as packed int32.
  508. * @param {number} fieldNumber
  509. * @param {!Array<number>} values
  510. */
  511. writePackedInt32(fieldNumber, values) {
  512. this.writeVariablePacked_(
  513. fieldNumber, values, (writer, val) => writer.writeInt32Value_(val));
  514. }
  515. /**
  516. * Writes repeated int64 values to the buffer as unpacked varint.
  517. * @param {number} fieldNumber
  518. * @param {!Array<!Int64>} values
  519. */
  520. writeRepeatedInt64(fieldNumber, values) {
  521. values.forEach(val => this.writeInt64(fieldNumber, val));
  522. }
  523. /**
  524. * Writes repeated int64 values to the buffer as packed varint.
  525. * @param {number} fieldNumber
  526. * @param {!Array<!Int64>} values
  527. */
  528. writePackedInt64(fieldNumber, values) {
  529. this.writeVariablePacked_(
  530. fieldNumber, values,
  531. (writer, val) =>
  532. writer.writeVarint64_(val.getHighBits(), val.getLowBits()));
  533. }
  534. /**
  535. * Writes repeated sfixed32 values to the buffer as unpacked fixed32.
  536. * @param {number} fieldNumber
  537. * @param {!Array<number>} values
  538. */
  539. writeRepeatedSfixed32(fieldNumber, values) {
  540. values.forEach(val => this.writeSfixed32(fieldNumber, val));
  541. }
  542. /**
  543. * Writes repeated sfixed32 values to the buffer as packed fixed32.
  544. * @param {number} fieldNumber
  545. * @param {!Array<number>} values
  546. */
  547. writePackedSfixed32(fieldNumber, values) {
  548. this.writeFixedPacked_(
  549. fieldNumber, values, val => this.writeSfixed32Value_(val), 4);
  550. }
  551. /**
  552. * Writes repeated sfixed64 values to the buffer as unpacked fixed64.
  553. * @param {number} fieldNumber
  554. * @param {!Array<!Int64>} values
  555. */
  556. writeRepeatedSfixed64(fieldNumber, values) {
  557. values.forEach(val => this.writeSfixed64(fieldNumber, val));
  558. }
  559. /**
  560. * Writes repeated sfixed64 values to the buffer as packed fixed64.
  561. * @param {number} fieldNumber
  562. * @param {!Array<!Int64>} values
  563. */
  564. writePackedSfixed64(fieldNumber, values) {
  565. this.writeFixedPacked_(
  566. fieldNumber, values, val => this.writeSfixed64Value_(val), 8);
  567. }
  568. /**
  569. * Writes repeated sint32 values to the buffer as unpacked sint32.
  570. * @param {number} fieldNumber
  571. * @param {!Array<number>} values
  572. */
  573. writeRepeatedSint32(fieldNumber, values) {
  574. values.forEach(val => this.writeSint32(fieldNumber, val));
  575. }
  576. /**
  577. * Writes repeated sint32 values to the buffer as packed sint32.
  578. * @param {number} fieldNumber
  579. * @param {!Array<number>} values
  580. */
  581. writePackedSint32(fieldNumber, values) {
  582. this.writeVariablePacked_(
  583. fieldNumber, values, (writer, val) => writer.writeSint32Value_(val));
  584. }
  585. /**
  586. * Writes repeated sint64 values to the buffer as unpacked varint.
  587. * @param {number} fieldNumber
  588. * @param {!Array<!Int64>} values
  589. */
  590. writeRepeatedSint64(fieldNumber, values) {
  591. values.forEach(val => this.writeSint64(fieldNumber, val));
  592. }
  593. /**
  594. * Writes repeated sint64 values to the buffer as packed varint.
  595. * @param {number} fieldNumber
  596. * @param {!Array<!Int64>} values
  597. */
  598. writePackedSint64(fieldNumber, values) {
  599. this.writeVariablePacked_(
  600. fieldNumber, values, (writer, val) => writer.writeSint64Value_(val));
  601. }
  602. /**
  603. * Writes repeated uint32 values to the buffer as unpacked uint32.
  604. * @param {number} fieldNumber
  605. * @param {!Array<number>} values
  606. */
  607. writeRepeatedUint32(fieldNumber, values) {
  608. values.forEach(val => this.writeUint32(fieldNumber, val));
  609. }
  610. /**
  611. * Writes repeated uint32 values to the buffer as packed uint32.
  612. * @param {number} fieldNumber
  613. * @param {!Array<number>} values
  614. */
  615. writePackedUint32(fieldNumber, values) {
  616. this.writeVariablePacked_(
  617. fieldNumber, values, (writer, val) => writer.writeUint32Value_(val));
  618. }
  619. /**
  620. * Writes repeated bytes values to the buffer.
  621. * @param {number} fieldNumber
  622. * @param {!Array<!ByteString>} values
  623. */
  624. writeRepeatedBytes(fieldNumber, values) {
  625. values.forEach(val => this.writeBytes(fieldNumber, val));
  626. }
  627. /**
  628. * Writes packed fields with fixed length.
  629. * @param {number} fieldNumber
  630. * @param {!Array<T>} values
  631. * @param {function(T)} valueWriter
  632. * @param {number} entitySize
  633. * @template T
  634. * @private
  635. */
  636. writeFixedPacked_(fieldNumber, values, valueWriter, entitySize) {
  637. if (values.length === 0) {
  638. return;
  639. }
  640. this.writeTag(fieldNumber, WireType.DELIMITED);
  641. this.writeUnsignedVarint32_(values.length * entitySize);
  642. this.closeAndStartNewBuffer_();
  643. values.forEach(value => valueWriter(value));
  644. }
  645. /**
  646. * Writes packed fields with variable length.
  647. * @param {number} fieldNumber
  648. * @param {!Array<T>} values
  649. * @param {function(!Writer, T)} valueWriter
  650. * @template T
  651. * @private
  652. */
  653. writeVariablePacked_(fieldNumber, values, valueWriter) {
  654. if (values.length === 0) {
  655. return;
  656. }
  657. const writer = new Writer();
  658. values.forEach(val => valueWriter(writer, val));
  659. const bytes = writer.getAndResetResultBuffer();
  660. this.writeDelimited(fieldNumber, bytes);
  661. }
  662. /**
  663. * Writes repeated string values to the buffer.
  664. * @param {number} fieldNumber
  665. * @param {!Array<string>} values
  666. */
  667. writeRepeatedString(fieldNumber, values) {
  668. values.forEach(val => this.writeString(fieldNumber, val));
  669. }
  670. }
  671. exports = Writer;