|
@ -1,13 +1,11 @@ |
|
|
var SlowBuffer = process.binding('buffer').Buffer; |
|
|
var SlowBuffer = process.binding('buffer').Buffer; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function toHex (n) { |
|
|
function toHex (n) { |
|
|
if (n < 16) return "0" + n.toString(16); |
|
|
if (n < 16) return "0" + n.toString(16); |
|
|
return n.toString(16); |
|
|
return n.toString(16); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
SlowBuffer.isSlowBuffer = function (b) { |
|
|
|
|
|
return b instanceof SlowBuffer; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
SlowBuffer.prototype.inspect = function () { |
|
|
SlowBuffer.prototype.inspect = function () { |
|
|
var out = [], |
|
|
var out = [], |
|
@ -18,6 +16,7 @@ SlowBuffer.prototype.inspect = function () { |
|
|
return "<SlowBuffer " + out.join(" ") + ">"; |
|
|
return "<SlowBuffer " + out.join(" ") + ">"; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SlowBuffer.prototype.toString = function (encoding, start, stop) { |
|
|
SlowBuffer.prototype.toString = function (encoding, start, stop) { |
|
|
encoding = String(encoding || 'utf8').toLowerCase(); |
|
|
encoding = String(encoding || 'utf8').toLowerCase(); |
|
|
start = +start || 0; |
|
|
start = +start || 0; |
|
@ -47,6 +46,7 @@ SlowBuffer.prototype.toString = function (encoding, start, stop) { |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SlowBuffer.prototype.write = function (string, offset, encoding) { |
|
|
SlowBuffer.prototype.write = function (string, offset, encoding) { |
|
|
// Support both (string, offset, encoding)
|
|
|
// Support both (string, offset, encoding)
|
|
|
// and the legacy (string, encoding, offset)
|
|
|
// and the legacy (string, encoding, offset)
|
|
@ -78,23 +78,18 @@ SlowBuffer.prototype.write = function (string, offset, encoding) { |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
SlowBuffer.prototype.get = function (index) { |
|
|
|
|
|
return this[index]; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
SlowBuffer.prototype.set = function (index, value) { |
|
|
|
|
|
return this[index] = value; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// Buffer
|
|
|
// Buffer
|
|
|
var POOLSIZE = 8*1024; |
|
|
var POOLSIZE = 8*1024; |
|
|
var pool; |
|
|
var pool; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function allocPool () { |
|
|
function allocPool () { |
|
|
pool = new SlowBuffer(POOLSIZE); |
|
|
pool = new SlowBuffer(POOLSIZE); |
|
|
pool.used = 0; |
|
|
pool.used = 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function Buffer (subject, encoding, legacy, slice_legacy) { |
|
|
function Buffer (subject, encoding, legacy, slice_legacy) { |
|
|
var length, type; |
|
|
var length, type; |
|
|
|
|
|
|
|
@ -157,11 +152,13 @@ function Buffer (subject, encoding, legacy, slice_legacy) { |
|
|
|
|
|
|
|
|
exports.Buffer = Buffer; |
|
|
exports.Buffer = Buffer; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Static methods
|
|
|
// Static methods
|
|
|
Buffer.isBuffer = function isBuffer(b) { |
|
|
Buffer.isBuffer = function isBuffer(b) { |
|
|
return b instanceof Buffer; |
|
|
return b instanceof Buffer; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Inspect
|
|
|
// Inspect
|
|
|
Buffer.prototype.inspect = function inspect() { |
|
|
Buffer.prototype.inspect = function inspect() { |
|
|
var out = [], |
|
|
var out = [], |
|
@ -172,18 +169,18 @@ Buffer.prototype.inspect = function inspect() { |
|
|
return "<Buffer " + out.join(" ") + ">"; |
|
|
return "<Buffer " + out.join(" ") + ">"; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Buffer.prototype.get = function get (i) { |
|
|
Buffer.prototype.get = function get (i) { |
|
|
if (i < 0 || i >= this.length) throw new Error("oob"); |
|
|
if (i < 0 || i >= this.length) throw new Error("oob"); |
|
|
return this.parent[this.offset + i]; |
|
|
return this.parent[this.offset + i]; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Buffer.prototype.set = function set (i, v) { |
|
|
Buffer.prototype.set = function set (i, v) { |
|
|
if (i < 0 || i >= this.length) throw new Error("oob"); |
|
|
if (i < 0 || i >= this.length) throw new Error("oob"); |
|
|
return this.parent[this.offset + i] = v; |
|
|
return this.parent[this.offset + i] = v; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
// TODO define slice, toString, write, etc.
|
|
|
|
|
|
// slice should not use c++
|
|
|
|
|
|
|
|
|
|
|
|
// write(string, offset = 0, encoding = 'uft8')
|
|
|
// write(string, offset = 0, encoding = 'uft8')
|
|
|
Buffer.prototype.write = function write (string, offset, encoding) { |
|
|
Buffer.prototype.write = function write (string, offset, encoding) { |
|
@ -193,18 +190,18 @@ Buffer.prototype.write = function write (string, offset, encoding) { |
|
|
offset = swap; |
|
|
offset = swap; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var max_length; |
|
|
|
|
|
offset || (offset = 0); |
|
|
offset || (offset = 0); |
|
|
encoding || (encoding = 'uft8'); |
|
|
encoding || (encoding = 'uft8'); |
|
|
|
|
|
|
|
|
// Make sure we are not going to overflow
|
|
|
// Make sure we are not going to overflow
|
|
|
max_length = this.length - offset; |
|
|
var max_length = this.length - offset; |
|
|
if (string.length > max_length) { |
|
|
if (string.length > max_length) { |
|
|
string = string.slice(0, max_length); |
|
|
string = string.slice(0, max_length); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return this.parent.write(string, this.offset + offset, encoding); |
|
|
return this.parent.write(string, this.offset + offset, encoding); |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// toString(encoding, start=0, end=buffer.length)
|
|
|
// toString(encoding, start=0, end=buffer.length)
|
|
|
Buffer.prototype.toString = function toString (encoding, start, end) { |
|
|
Buffer.prototype.toString = function toString (encoding, start, end) { |
|
@ -220,9 +217,11 @@ Buffer.prototype.toString = function toString (encoding, start, end) { |
|
|
return this.parent.toString(encoding, start + this.offset, end + this.offset); |
|
|
return this.parent.toString(encoding, start + this.offset, end + this.offset); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// byteLength
|
|
|
// byteLength
|
|
|
Buffer.byteLength = SlowBuffer.byteLength; |
|
|
Buffer.byteLength = SlowBuffer.byteLength; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// copy(targetBuffer, targetStart, sourceStart, sourceEnd=buffer.length)
|
|
|
// copy(targetBuffer, targetStart, sourceStart, sourceEnd=buffer.length)
|
|
|
Buffer.prototype.copy = function copy (target, target_start, start, end) { |
|
|
Buffer.prototype.copy = function copy (target, target_start, start, end) { |
|
|
start || (start = 0); |
|
|
start || (start = 0); |
|
@ -233,11 +232,15 @@ Buffer.prototype.copy = function copy (target, target_start, start, end) { |
|
|
end = this.length; |
|
|
end = this.length; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return this.parent.copy(target.parent, target_start + target.offset, start + this.offset, end + this.offset); |
|
|
return this.parent.copy(target.parent, |
|
|
|
|
|
target_start + target.offset, |
|
|
|
|
|
start + this.offset, |
|
|
|
|
|
end + this.offset); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// slice(start, end)
|
|
|
// slice(start, end)
|
|
|
Buffer.prototype.slice = function slice (start, end, legacy) { |
|
|
Buffer.prototype.slice = function (start, end, legacy) { |
|
|
if (end > this.length) { |
|
|
if (end > this.length) { |
|
|
throw new Error("oob"); |
|
|
throw new Error("oob"); |
|
|
} |
|
|
} |
|
@ -247,3 +250,4 @@ Buffer.prototype.slice = function slice (start, end, legacy) { |
|
|
|
|
|
|
|
|
return new Buffer(this.parent, end - start, +start + this.offset, legacy); |
|
|
return new Buffer(this.parent, end - start, +start + this.offset, legacy); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|