index.js 725 B

123456789101112131415161718192021222324252627282930
  1. /**
  2. * An abstraction for slicing an arraybuffer even when
  3. * ArrayBuffer.prototype.slice is not supported
  4. *
  5. * @api public
  6. */
  7. module.exports = function(arraybuffer, start, end) {
  8. var bytes = arraybuffer.byteLength;
  9. start = start || 0;
  10. end = end || bytes;
  11. if (arraybuffer.slice) { return arraybuffer.slice(start, end); }
  12. if (start < 0) { start += bytes; }
  13. if (end < 0) { end += bytes; }
  14. if (end > bytes) { end = bytes; }
  15. if (start >= bytes || start >= end || bytes === 0) {
  16. return new ArrayBuffer(0);
  17. }
  18. var abv = new Uint8Array(arraybuffer);
  19. var result = new Uint8Array(end - start);
  20. for (var i = start, ii = 0; i < end; i++, ii++) {
  21. result[ii] = abv[i];
  22. }
  23. return result.buffer;
  24. };