|
|
@ -90,19 +90,22 @@ function allocPool () { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function Buffer (subject, encoding, legacy, slice_legacy) { |
|
|
|
function Buffer (subject, encoding, offset) { |
|
|
|
if (!(this instanceof Buffer)) { |
|
|
|
return new Buffer(subject, encoding, legacy, slice_legacy); |
|
|
|
return new Buffer(subject, encoding, offset); |
|
|
|
} |
|
|
|
|
|
|
|
var length, type; |
|
|
|
|
|
|
|
// Are we slicing?
|
|
|
|
if (typeof legacy === 'number') { |
|
|
|
this.parent = subject; |
|
|
|
if (typeof offset === 'number') { |
|
|
|
this.length = encoding; |
|
|
|
this.offset = legacy; |
|
|
|
legacy = slice_legacy; |
|
|
|
Object.defineProperty(this, "parent", { enumerable: false, |
|
|
|
value: subject, |
|
|
|
writable: false }); |
|
|
|
Object.defineProperty(this, "offset", { enumerable: false, |
|
|
|
value: offset, |
|
|
|
writable: false }); |
|
|
|
} else { |
|
|
|
// Find the length
|
|
|
|
switch (type = typeof subject) { |
|
|
@ -111,7 +114,7 @@ function Buffer (subject, encoding, legacy, slice_legacy) { |
|
|
|
break; |
|
|
|
|
|
|
|
case 'string': |
|
|
|
length = Buffer.byteLength(subject); |
|
|
|
length = Buffer.byteLength(subject, encoding); |
|
|
|
break; |
|
|
|
|
|
|
|
case 'object': // Assume object is an array
|
|
|
@ -126,13 +129,22 @@ function Buffer (subject, encoding, legacy, slice_legacy) { |
|
|
|
|
|
|
|
if (length > POOLSIZE) { |
|
|
|
// Big buffer, just alloc one.
|
|
|
|
this.parent = new SlowBuffer(subject, encoding); |
|
|
|
this.offset = 0; |
|
|
|
var parent = new SlowBuffer(subject, encoding); |
|
|
|
Object.defineProperty(this, "parent", { enumerable: false, |
|
|
|
value: parent, |
|
|
|
writable: false }); |
|
|
|
Object.defineProperty(this, "offset", { enumerable: false, |
|
|
|
value: 0, |
|
|
|
writable: false }); |
|
|
|
} else { |
|
|
|
// Small buffer.
|
|
|
|
if (!pool || pool.length - pool.used < length) allocPool(); |
|
|
|
this.parent = pool; |
|
|
|
this.offset = pool.used; |
|
|
|
Object.defineProperty(this, "parent", { enumerable: false, |
|
|
|
value: pool, |
|
|
|
writable: false }); |
|
|
|
Object.defineProperty(this, "offset", { enumerable: false, |
|
|
|
value: pool.used, |
|
|
|
writable: false }); |
|
|
|
pool.used += length; |
|
|
|
|
|
|
|
// Do we need to write stuff?
|
|
|
@ -150,11 +162,7 @@ function Buffer (subject, encoding, legacy, slice_legacy) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Make sure the api is equivilent to old buffers, unless user doesn't
|
|
|
|
// want overhead
|
|
|
|
if (legacy !== false) { |
|
|
|
SlowBuffer.makeFastBuffer(this.parent, this, this.offset, this.length); |
|
|
|
} |
|
|
|
SlowBuffer.makeFastBuffer(this.parent, this, this.offset, this.length); |
|
|
|
} |
|
|
|
|
|
|
|
exports.Buffer = Buffer; |
|
|
@ -197,23 +205,35 @@ Buffer.prototype.write = function write (string, offset, encoding) { |
|
|
|
offset = swap; |
|
|
|
} |
|
|
|
|
|
|
|
offset || (offset = 0); |
|
|
|
encoding || (encoding = 'utf8'); |
|
|
|
offset = +offset || 0; |
|
|
|
encoding = String(encoding || 'utf8').toLowerCase(); |
|
|
|
|
|
|
|
// Make sure we are not going to overflow
|
|
|
|
var max_length = this.length - offset; |
|
|
|
if (Buffer.byteLength(string) > max_length) { |
|
|
|
// FIXME: Char length !== byte length
|
|
|
|
string = string.slice(0, max_length); |
|
|
|
} |
|
|
|
var maxLength = this.length - offset; |
|
|
|
|
|
|
|
switch (encoding) { |
|
|
|
case 'utf8': |
|
|
|
case 'utf-8': |
|
|
|
return this.parent.utf8Write(string, this.offset + offset, maxLength); |
|
|
|
|
|
|
|
return this.parent.write(string, this.offset + offset, encoding); |
|
|
|
case 'ascii': |
|
|
|
return this.parent.asciiWrite(string, this.offset + offset, maxLength); |
|
|
|
|
|
|
|
case 'binary': |
|
|
|
return this.parent.binaryWrite(string, this.offset + offset, maxLength); |
|
|
|
|
|
|
|
case 'base64': |
|
|
|
return this.parent.base64Write(string, this.offset + offset, maxLength); |
|
|
|
|
|
|
|
default: |
|
|
|
throw new Error('Unknown encoding'); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// toString(encoding, start=0, end=buffer.length)
|
|
|
|
Buffer.prototype.toString = function (encoding, start, end) { |
|
|
|
if (typeof encoding == 'undefined') encoding = 'utf8'; |
|
|
|
encoding = String(encoding || 'utf8').toLowerCase(); |
|
|
|
|
|
|
|
if (typeof start == 'undefined' || start < 0) { |
|
|
|
start = 0; |
|
|
@ -227,7 +247,26 @@ Buffer.prototype.toString = function (encoding, start, end) { |
|
|
|
end = 0; |
|
|
|
} |
|
|
|
|
|
|
|
return this.parent.toString(encoding, start + this.offset, end + this.offset); |
|
|
|
start = start + this.offset; |
|
|
|
end = end + this.offset; |
|
|
|
|
|
|
|
switch (encoding) { |
|
|
|
case 'utf8': |
|
|
|
case 'utf-8': |
|
|
|
return this.parent.utf8Slice(start, end); |
|
|
|
|
|
|
|
case 'ascii': |
|
|
|
return this.parent.asciiSlice(start, end); |
|
|
|
|
|
|
|
case 'binary': |
|
|
|
return this.parent.binarySlice(start, end); |
|
|
|
|
|
|
|
case 'base64': |
|
|
|
return this.parent.base64Slice(start, end); |
|
|
|
|
|
|
|
default: |
|
|
|
throw new Error('Unknown encoding'); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
@ -276,7 +315,7 @@ Buffer.prototype.copy = function copy (target, target_start, start, end) { |
|
|
|
|
|
|
|
|
|
|
|
// slice(start, end)
|
|
|
|
Buffer.prototype.slice = function (start, end, legacy) { |
|
|
|
Buffer.prototype.slice = function (start, end) { |
|
|
|
if (end > this.length) { |
|
|
|
throw new Error("oob"); |
|
|
|
} |
|
|
@ -284,6 +323,6 @@ Buffer.prototype.slice = function (start, end, legacy) { |
|
|
|
throw new Error("oob"); |
|
|
|
} |
|
|
|
|
|
|
|
return new Buffer(this.parent, end - start, +start + this.offset, legacy); |
|
|
|
return new Buffer(this.parent, end - start, +start + this.offset); |
|
|
|
}; |
|
|
|
|
|
|
|