-
Notifications
You must be signed in to change notification settings - Fork 13
/
browser-buffer.js
52 lines (51 loc) · 1.6 KB
/
browser-buffer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
function Buffer(arraybuffer, offset, length) {
var data;
if (arguments.length === 1) {
if (Array.isArray(arraybuffer)) {
data = arraybuffer;
arraybuffer = new ArrayBuffer(data.length);
var v = new Uint8Array(arraybuffer);
for (var i = 0, l = data.length; i < l; i++) {
v[i] = data[i];
}
}
else if (typeof arraybuffer === "number") {
arraybuffer = new ArrayBuffer(arraybuffer);
}
offset = 0;
length = arraybuffer.byteLength;
}
var buffer = new Uint8Array(arraybuffer, offset, length);
buffer.__proto__ = Buffer.prototype;
buffer.view = new DataView(arraybuffer, offset, length);
buffer.offset = offset;
buffer.length = length;
return buffer;
}
Buffer.prototype.__proto__ = Uint8Array.prototype;
Buffer.prototype.slice = function (start, end) {
return Buffer(this.buffer, start + this.offset, end - start);
};
Buffer.prototype.readUInt32LE = function (offset) {
return this.view.getUint32(offset, true);
};
Buffer.prototype.readUInt16LE = function (offset) {
return this.view.getUint16(offset, true);
};
Buffer.prototype.readInt16LE = function (offset) {
return this.view.getInt16(offset, true);
};
Buffer.prototype.readDoubleLE = function (offset) {
return this.view.getFloat64(offset, true);
};
Buffer.prototype.writeDoubleLE = function (value, offset) {
return this.view.setFloat64(offset, value, true);
};
Buffer.prototype.writeUInt32LE = function (value, offset) {
this.view.setUint32(offset, value, true);
};
Buffer.prototype.toString = function (encoding) {
if (!encoding) {
return String.fromCharCode.apply(null, this);
}
};