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