var browserSupport = (function () {
// Detect if browser supports Typed Arrays. Supported browsers are IE 10+,
// Firefox 4+, Chrome 7+, Safari 5.1+, Opera 11.6+, iOS 4.2+.
- if (typeof Uint8Array === 'undefined' || typeof ArrayBuffer === 'undefined' ||
- typeof DataView === 'undefined')
+ if (typeof Uint8Array === 'undefined' || typeof ArrayBuffer === 'undefined')
return false
// Does the browser support adding properties to `Uint8Array` instances? If
if (offset >= len)
return
- if (browserSupport) {
- if (offset + 1 < len) {
- return buf._dataview.getUint16(offset, littleEndian)
- } else {
- var dv = new DataView(new ArrayBuffer(2))
- dv.setUint8(0, buf[len - 1])
- return dv.getUint16(0, littleEndian)
- }
+ var val
+ if (littleEndian) {
+ val = buf[offset]
+ if (offset + 1 < len)
+ val |= buf[offset + 1] << 8
} else {
- var val
- if (littleEndian) {
- val = buf[offset]
- if (offset + 1 < len)
- val |= buf[offset + 1] << 8
- } else {
- val = buf[offset] << 8
- if (offset + 1 < len)
- val |= buf[offset + 1]
- }
- return val
+ val = buf[offset] << 8
+ if (offset + 1 < len)
+ val |= buf[offset + 1]
}
+ return val
}
Buffer.prototype.readUInt16LE = function (offset, noAssert) {
if (offset >= len)
return
- if (browserSupport) {
- if (offset + 3 < len) {
- return buf._dataview.getUint32(offset, littleEndian)
- } else {
- var dv = new DataView(new ArrayBuffer(4))
- for (var i = 0; i + offset < len; i++) {
- dv.setUint8(i, buf[i + offset])
- }
- return dv.getUint32(0, littleEndian)
- }
+ var val
+ if (littleEndian) {
+ if (offset + 2 < len)
+ val = buf[offset + 2] << 16
+ if (offset + 1 < len)
+ val |= buf[offset + 1] << 8
+ val |= buf[offset]
+ if (offset + 3 < len)
+ val = val + (buf[offset + 3] << 24 >>> 0)
} else {
- var val
- if (littleEndian) {
- if (offset + 2 < len)
- val = buf[offset + 2] << 16
- if (offset + 1 < len)
- val |= buf[offset + 1] << 8
- val |= buf[offset]
- if (offset + 3 < len)
- val = val + (buf[offset + 3] << 24 >>> 0)
- } else {
- if (offset + 1 < len)
- val = buf[offset + 1] << 16
- if (offset + 2 < len)
- val |= buf[offset + 2] << 8
- if (offset + 3 < len)
- val |= buf[offset + 3]
- val = val + (buf[offset] << 24 >>> 0)
- }
- return val
+ if (offset + 1 < len)
+ val = buf[offset + 1] << 16
+ if (offset + 2 < len)
+ val |= buf[offset + 2] << 8
+ if (offset + 3 < len)
+ val |= buf[offset + 3]
+ val = val + (buf[offset] << 24 >>> 0)
}
+ return val
}
Buffer.prototype.readUInt32LE = function (offset, noAssert) {
if (offset >= buf.length)
return
- if (browserSupport) {
- return buf._dataview.getInt8(offset)
- } else {
- var neg = buf[offset] & 0x80
- if (neg)
- return (0xff - buf[offset] + 1) * -1
- else
- return buf[offset]
- }
+ var neg = buf[offset] & 0x80
+ if (neg)
+ return (0xff - buf[offset] + 1) * -1
+ else
+ return buf[offset]
}
function _readInt16 (buf, offset, littleEndian, noAssert) {
if (offset >= len)
return
- if (browserSupport) {
- if (offset + 1 === len) {
- var dv = new DataView(new ArrayBuffer(2))
- dv.setUint8(0, buf[len - 1])
- return dv.getInt16(0, littleEndian)
- } else {
- return buf._dataview.getInt16(offset, littleEndian)
- }
- } else {
- var val = _readUInt16(buf, offset, littleEndian, true)
- var neg = val & 0x8000
- if (neg)
- return (0xffff - val + 1) * -1
- else
- return val
- }
+ var val = _readUInt16(buf, offset, littleEndian, true)
+ var neg = val & 0x8000
+ if (neg)
+ return (0xffff - val + 1) * -1
+ else
+ return val
}
Buffer.prototype.readInt16LE = function (offset, noAssert) {
if (offset >= len)
return
- if (browserSupport) {
- if (offset + 3 >= len) {
- var dv = new DataView(new ArrayBuffer(4))
- for (var i = 0; i + offset < len; i++) {
- dv.setUint8(i, buf[i + offset])
- }
- return dv.getInt32(0, littleEndian)
- } else {
- return buf._dataview.getInt32(offset, littleEndian)
- }
- } else {
- var val = _readUInt32(buf, offset, littleEndian, true)
- var neg = val & 0x80000000
- if (neg)
- return (0xffffffff - val + 1) * -1
- else
- return val
- }
+ var val = _readUInt32(buf, offset, littleEndian, true)
+ var neg = val & 0x80000000
+ if (neg)
+ return (0xffffffff - val + 1) * -1
+ else
+ return val
}
Buffer.prototype.readInt32LE = function (offset, noAssert) {
assert(offset + 3 < buf.length, 'Trying to read beyond buffer length')
}
- if (browserSupport) {
- return buf._dataview.getFloat32(offset, littleEndian)
- } else {
- return ieee754.read(buf, offset, littleEndian, 23, 4)
- }
+ return ieee754.read(buf, offset, littleEndian, 23, 4)
}
Buffer.prototype.readFloatLE = function (offset, noAssert) {
assert(offset + 7 < buf.length, 'Trying to read beyond buffer length')
}
- if (browserSupport) {
- return buf._dataview.getFloat64(offset, littleEndian)
- } else {
- return ieee754.read(buf, offset, littleEndian, 52, 8)
- }
+ return ieee754.read(buf, offset, littleEndian, 52, 8)
}
Buffer.prototype.readDoubleLE = function (offset, noAssert) {
if (offset >= len)
return
- if (browserSupport) {
- if (offset + 1 === len) {
- var dv = new DataView(new ArrayBuffer(2))
- dv.setUint16(0, value, littleEndian)
- buf[offset] = dv.getUint8(0)
- } else {
- buf._dataview.setUint16(offset, value, littleEndian)
- }
- } else {
- for (var i = 0, j = Math.min(len - offset, 2); i < j; i++) {
- buf[offset + i] =
- (value & (0xff << (8 * (littleEndian ? i : 1 - i)))) >>>
- (littleEndian ? i : 1 - i) * 8
- }
+ for (var i = 0, j = Math.min(len - offset, 2); i < j; i++) {
+ buf[offset + i] =
+ (value & (0xff << (8 * (littleEndian ? i : 1 - i)))) >>>
+ (littleEndian ? i : 1 - i) * 8
}
}
if (offset >= len)
return
- var i
- if (browserSupport) {
- if (offset + 3 >= len) {
- var dv = new DataView(new ArrayBuffer(4))
- dv.setUint32(0, value, littleEndian)
- for (i = 0; i + offset < len; i++) {
- buf[i + offset] = dv.getUint8(i)
- }
- } else {
- buf._dataview.setUint32(offset, value, littleEndian)
- }
- } else {
- for (i = 0, j = Math.min(len - offset, 4); i < j; i++) {
- buf[offset + i] =
- (value >>> (littleEndian ? i : 3 - i) * 8) & 0xff
- }
+ for (var i = 0, j = Math.min(len - offset, 4); i < j; i++) {
+ buf[offset + i] =
+ (value >>> (littleEndian ? i : 3 - i) * 8) & 0xff
}
}
if (offset >= buf.length)
return
- if (browserSupport) {
- buf._dataview.setInt8(offset, value)
- } else {
- if (value >= 0)
- buf.writeUInt8(value, offset, noAssert)
- else
- buf.writeUInt8(0xff + value + 1, offset, noAssert)
- }
+ if (value >= 0)
+ buf.writeUInt8(value, offset, noAssert)
+ else
+ buf.writeUInt8(0xff + value + 1, offset, noAssert)
}
function _writeInt16 (buf, value, offset, littleEndian, noAssert) {
if (offset >= len)
return
- if (browserSupport) {
- if (offset + 1 === len) {
- var dv = new DataView(new ArrayBuffer(2))
- dv.setInt16(0, value, littleEndian)
- buf[offset] = dv.getUint8(0)
- } else {
- buf._dataview.setInt16(offset, value, littleEndian)
- }
- } else {
- if (value >= 0)
- _writeUInt16(buf, value, offset, littleEndian, noAssert)
- else
- _writeUInt16(buf, 0xffff + value + 1, offset, littleEndian, noAssert)
- }
+ if (value >= 0)
+ _writeUInt16(buf, value, offset, littleEndian, noAssert)
+ else
+ _writeUInt16(buf, 0xffff + value + 1, offset, littleEndian, noAssert)
}
Buffer.prototype.writeInt16LE = function (value, offset, noAssert) {
if (offset >= len)
return
- if (browserSupport) {
- if (offset + 3 >= len) {
- var dv = new DataView(new ArrayBuffer(4))
- dv.setInt32(0, value, littleEndian)
- for (var i = 0; i + offset < len; i++) {
- buf[i + offset] = dv.getUint8(i)
- }
- } else {
- buf._dataview.setInt32(offset, value, littleEndian)
- }
- } else {
- if (value >= 0)
- _writeUInt32(buf, value, offset, littleEndian, noAssert)
- else
- _writeUInt32(buf, 0xffffffff + value + 1, offset, littleEndian, noAssert)
- }
+ if (value >= 0)
+ _writeUInt32(buf, value, offset, littleEndian, noAssert)
+ else
+ _writeUInt32(buf, 0xffffffff + value + 1, offset, littleEndian, noAssert)
}
Buffer.prototype.writeInt32LE = function (value, offset, noAssert) {
if (offset >= len)
return
- if (browserSupport) {
- if (offset + 3 >= len) {
- var dv = new DataView(new ArrayBuffer(4))
- dv.setFloat32(0, value, littleEndian)
- for (var i = 0; i + offset < len; i++) {
- buf[i + offset] = dv.getUint8(i)
- }
- } else {
- buf._dataview.setFloat32(offset, value, littleEndian)
- }
- } else {
- ieee754.write(buf, value, offset, littleEndian, 23, 4)
- }
+ ieee754.write(buf, value, offset, littleEndian, 23, 4)
}
Buffer.prototype.writeFloatLE = function (value, offset, noAssert) {
if (offset >= len)
return
- if (browserSupport) {
- if (offset + 7 >= len) {
- var dv = new DataView(new ArrayBuffer(8))
- dv.setFloat64(0, value, littleEndian)
- for (var i = 0; i + offset < len; i++) {
- buf[i + offset] = dv.getUint8(i)
- }
- } else {
- buf._dataview.setFloat64(offset, value, littleEndian)
- }
- } else {
- ieee754.write(buf, value, offset, littleEndian, 52, 8)
- }
+ ieee754.write(buf, value, offset, littleEndian, 52, 8)
}
Buffer.prototype.writeDoubleLE = function (value, offset, noAssert) {
arr.inspect = BP.inspect
arr.toArrayBuffer = BufferToArrayBuffer
- if (arr.byteLength !== 0)
- arr._dataview = new DataView(arr.buffer, arr.byteOffset, arr.byteLength)
-
return arr
}