|
|
@ -1,24 +1,24 @@ |
|
|
|
var Buffer = process.binding('buffer').Buffer; |
|
|
|
var SlowBuffer = process.binding('buffer').Buffer; |
|
|
|
|
|
|
|
function toHex (n) { |
|
|
|
if (n < 16) return "0" + n.toString(16); |
|
|
|
return n.toString(16); |
|
|
|
} |
|
|
|
|
|
|
|
Buffer.isBuffer = function (b) { |
|
|
|
return b instanceof Buffer; |
|
|
|
SlowBuffer.isSlowBuffer = function (b) { |
|
|
|
return b instanceof SlowBuffer; |
|
|
|
}; |
|
|
|
|
|
|
|
Buffer.prototype.inspect = function () { |
|
|
|
SlowBuffer.prototype.inspect = function () { |
|
|
|
var out = [], |
|
|
|
len = this.length; |
|
|
|
for (var i = 0; i < len; i++) { |
|
|
|
out[i] = toHex(this[i]); |
|
|
|
} |
|
|
|
return "<Buffer " + out.join(" ") + ">"; |
|
|
|
return "<SlowBuffer " + out.join(" ") + ">"; |
|
|
|
}; |
|
|
|
|
|
|
|
Buffer.prototype.toString = function (encoding, start, stop) { |
|
|
|
SlowBuffer.prototype.toString = function (encoding, start, stop) { |
|
|
|
encoding = String(encoding || 'utf8').toLowerCase(); |
|
|
|
start = +start || 0; |
|
|
|
if (typeof stop == "undefined") stop = this.length; |
|
|
@ -47,7 +47,7 @@ Buffer.prototype.toString = function (encoding, start, stop) { |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
Buffer.prototype.write = function (string, offset, encoding) { |
|
|
|
SlowBuffer.prototype.write = function (string, offset, encoding) { |
|
|
|
// Support both (string, offset, encoding)
|
|
|
|
// and the legacy (string, encoding, offset)
|
|
|
|
if (!isFinite(offset)) { |
|
|
@ -78,24 +78,24 @@ Buffer.prototype.write = function (string, offset, encoding) { |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
Buffer.prototype.get = function (index) { |
|
|
|
SlowBuffer.prototype.get = function (index) { |
|
|
|
return this[index]; |
|
|
|
}; |
|
|
|
|
|
|
|
Buffer.prototype.set = function (index, value) { |
|
|
|
SlowBuffer.prototype.set = function (index, value) { |
|
|
|
return this[index] = value; |
|
|
|
}; |
|
|
|
|
|
|
|
// FastBuffer
|
|
|
|
// Buffer
|
|
|
|
var POOLSIZE = 8*1024; |
|
|
|
var pool; |
|
|
|
|
|
|
|
function allocPool () { |
|
|
|
pool = new Buffer(POOLSIZE); |
|
|
|
pool = new SlowBuffer(POOLSIZE); |
|
|
|
pool.used = 0; |
|
|
|
} |
|
|
|
|
|
|
|
function FastBuffer (subject, encoding, legacy, slice_legacy) { |
|
|
|
function Buffer (subject, encoding, legacy, slice_legacy) { |
|
|
|
var length, type; |
|
|
|
|
|
|
|
// Are we slicing?
|
|
|
@ -124,7 +124,7 @@ function FastBuffer (subject, encoding, legacy, slice_legacy) { |
|
|
|
|
|
|
|
if (length > POOLSIZE) { |
|
|
|
// Big buffer, just alloc one.
|
|
|
|
this.parent = new Buffer(subject, encoding); |
|
|
|
this.parent = new SlowBuffer(subject, encoding); |
|
|
|
this.offset = 0; |
|
|
|
} else { |
|
|
|
// Small buffer.
|
|
|
@ -151,20 +151,19 @@ function FastBuffer (subject, encoding, legacy, slice_legacy) { |
|
|
|
// Make sure the api is equivilent to old buffers, unless user doesn't
|
|
|
|
// want overhead
|
|
|
|
if (legacy !== false) { |
|
|
|
Buffer.makeFastBuffer(this.parent, this, this.offset, this.length); |
|
|
|
SlowBuffer.makeFastBuffer(this.parent, this, this.offset, this.length); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
exports.FastBuffer = FastBuffer; |
|
|
|
exports.Buffer = FastBuffer; |
|
|
|
exports.Buffer = Buffer; |
|
|
|
|
|
|
|
// Static methods
|
|
|
|
FastBuffer.isBuffer = function isBuffer(b) { |
|
|
|
return b instanceof FastBuffer; |
|
|
|
Buffer.isBuffer = function isBuffer(b) { |
|
|
|
return b instanceof Buffer; |
|
|
|
}; |
|
|
|
|
|
|
|
// Inspect
|
|
|
|
FastBuffer.prototype.inspect = function inspect() { |
|
|
|
Buffer.prototype.inspect = function inspect() { |
|
|
|
var out = [], |
|
|
|
len = this.length; |
|
|
|
for (var i = 0; i < len; i++) { |
|
|
@ -173,12 +172,12 @@ FastBuffer.prototype.inspect = function inspect() { |
|
|
|
return "<Buffer " + out.join(" ") + ">"; |
|
|
|
}; |
|
|
|
|
|
|
|
FastBuffer.prototype.get = function (i) { |
|
|
|
Buffer.prototype.get = function get (i) { |
|
|
|
if (i < 0 || i >= this.length) throw new Error("oob"); |
|
|
|
return this.parent[this.offset + i]; |
|
|
|
}; |
|
|
|
|
|
|
|
FastBuffer.prototype.set = function (i, v) { |
|
|
|
Buffer.prototype.set = function set (i, v) { |
|
|
|
if (i < 0 || i >= this.length) throw new Error("oob"); |
|
|
|
return this.parent[this.offset + i] = v; |
|
|
|
}; |
|
|
@ -187,7 +186,7 @@ FastBuffer.prototype.set = function (i, v) { |
|
|
|
// slice should not use c++
|
|
|
|
|
|
|
|
// write(string, offset = 0, encoding = 'uft8')
|
|
|
|
FastBuffer.prototype.write = function write (string, offset, encoding) { |
|
|
|
Buffer.prototype.write = function write (string, offset, encoding) { |
|
|
|
if (!isFinite(offset)) { |
|
|
|
var swap = encoding; |
|
|
|
encoding = offset; |
|
|
@ -208,7 +207,7 @@ FastBuffer.prototype.write = function write (string, offset, encoding) { |
|
|
|
} |
|
|
|
|
|
|
|
// toString(encoding, start=0, end=buffer.length)
|
|
|
|
FastBuffer.prototype.toString = function toSting (encoding, start, end) { |
|
|
|
Buffer.prototype.toString = function toString (encoding, start, end) { |
|
|
|
encoding || (encoding = 'utf8'); |
|
|
|
start || (start = 0); |
|
|
|
end || (end = this.length); |
|
|
@ -222,10 +221,10 @@ FastBuffer.prototype.toString = function toSting (encoding, start, end) { |
|
|
|
}; |
|
|
|
|
|
|
|
// byteLength
|
|
|
|
FastBuffer.byteLength = Buffer.byteLength; |
|
|
|
Buffer.byteLength = SlowBuffer.byteLength; |
|
|
|
|
|
|
|
// copy(targetBuffer, targetStart, sourceStart, sourceEnd=buffer.length)
|
|
|
|
FastBuffer.prototype.copy = function copy (target_buffer, target_start, start, end) { |
|
|
|
Buffer.prototype.copy = function copy (target, target_start, start, end) { |
|
|
|
start || (start = 0); |
|
|
|
end || (end = this.length); |
|
|
|
|
|
|
@ -234,11 +233,11 @@ FastBuffer.prototype.copy = function copy (target_buffer, target_start, start, e |
|
|
|
end = this.length; |
|
|
|
} |
|
|
|
|
|
|
|
return this.parent.copy(target_buffer, target_start, 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)
|
|
|
|
FastBuffer.prototype.slice = function slice (start, end, legacy) { |
|
|
|
Buffer.prototype.slice = function slice (start, end, legacy) { |
|
|
|
if (end > this.length) { |
|
|
|
throw new Error("oob"); |
|
|
|
} |
|
|
@ -246,5 +245,5 @@ FastBuffer.prototype.slice = function slice (start, end, legacy) { |
|
|
|
throw new Error("oob"); |
|
|
|
} |
|
|
|
|
|
|
|
return new FastBuffer(this.parent, end - start, +start + this.offset, legacy); |
|
|
|
return new Buffer(this.parent, end - start, +start + this.offset, legacy); |
|
|
|
}; |
|
|
|