uint8arrays.js 671 B

12345678910111213141516171819202122232425262728
  1. /**
  2. * @fileoverview Helper methods for Uint8Arrays.
  3. */
  4. goog.module('protobuf.binary.uint8arrays');
  5. /**
  6. * Combines multiple bytes arrays (either Uint8Array or number array whose
  7. * values are bytes) into a single Uint8Array.
  8. * @param {!Array<!Uint8Array>|!Array<!Array<number>>} arrays
  9. * @return {!Uint8Array}
  10. */
  11. function concatenateByteArrays(arrays) {
  12. let totalLength = 0;
  13. for (const array of arrays) {
  14. totalLength += array.length;
  15. }
  16. const result = new Uint8Array(totalLength);
  17. let offset = 0;
  18. for (const array of arrays) {
  19. result.set(array, offset);
  20. offset += array.length;
  21. }
  22. return result;
  23. }
  24. exports = {
  25. concatenateByteArrays,
  26. };