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