Buffer.poolSize = 8 * 1024;
-var poolSize = Buffer.poolSize;
-var poolOffset = 0;
-var allocPool = alloc({}, poolSize);
+var poolSize, poolOffset, allocPool;
function createPool() {
allocPool = alloc({}, poolSize);
poolOffset = 0;
}
+createPool();
function Buffer(subject, encoding) {
return new Buffer(subject, encoding);
if (util.isNumber(subject))
- this.length = subject > 0 ? Math.floor(subject) : 0;
+ this.length = subject > 0 ? subject >>> 0 : 0;
else if (util.isString(subject))
this.length = Buffer.byteLength(subject, encoding = encoding || 'utf8');
else if (util.isObject(subject))
'size: 0x' + kMaxLength.toString(16) + ' bytes');
}
- if (this.length < Buffer.poolSize / 2 && this.length > 0) {
+ if (this.length <= (Buffer.poolSize >>> 1) && this.length > 0) {
if (this.length > poolSize - poolOffset)
createPool();
this.parent = sliceOnto(allocPool,
for (var i = 0; i < list.length; i++)
length += list[i].length;
} else {
- length = ~~length;
+ length = length >>> 0;
}
- if (length < 0) length = 0;
-
if (list.length === 0)
return new Buffer(0);
else if (list.length === 1)
Buffer.prototype.write = function(string, offset, length, encoding) {
// Buffer#write(string);
if (util.isUndefined(offset)) {
- offset = 0;
encoding = 'utf8';
+ length = this.length;
+ offset = 0;
// Buffer#write(string, encoding)
} else if (util.isUndefined(length) && util.isString(offset)) {
encoding = offset;
+ length = this.length;
offset = 0;
// Buffer#write(string, offset[, length][, encoding])
} else if (isFinite(offset)) {
- offset = ~~offset;
+ offset = offset >>> 0;
if (isFinite(length)) {
- length = ~~length;
+ length = length >>> 0;
if (util.isUndefined(encoding))
encoding = 'utf8';
} else {
var swap = encoding;
encoding = offset;
- offset = ~~length;
+ offset = length >>> 0;
length = swap;
}
encoding = !!encoding ? (encoding + '').toLowerCase() : 'utf8';
if (string.length > 0 && (length < 0 || offset < 0))
- throw new RangeError('attempt to write beyond buffer bounds');
+ throw new RangeError('attempt to write outside buffer bounds');
var ret;
switch (encoding) {
function checkOffset(offset, ext, length) {
- if (offset < 0 || offset + ext > length)
+ if (offset + ext > length)
throw new RangeError('index out of range');
}
Buffer.prototype.readUInt8 = function(offset, noAssert) {
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 1, this.length);
return this[offset];
Buffer.prototype.readUInt16LE = function(offset, noAssert) {
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 2, this.length);
return readUInt16(this, offset, false, noAssert);
Buffer.prototype.readUInt16BE = function(offset, noAssert) {
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 2, this.length);
return readUInt16(this, offset, true, noAssert);
Buffer.prototype.readUInt32LE = function(offset, noAssert) {
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 4, this.length);
return readUInt32(this, offset, false);
Buffer.prototype.readUInt32BE = function(offset, noAssert) {
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 4, this.length);
return readUInt32(this, offset, true);
*/
Buffer.prototype.readInt8 = function(offset, noAssert) {
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 1, this.length);
- if (!(this[offset] & 0x80))
- return (this[offset]);
- return ((0xff - this[offset] + 1) * -1);
+ var val = this[offset];
+ return !(val & 0x80) ? val : (0xff - val + 1) * -1;
};
function readInt16(buffer, offset, isBigEndian) {
var val = readUInt16(buffer, offset, isBigEndian);
- if (!(val & 0x8000))
- return val;
- return (0xffff - val + 1) * -1;
+ return !(val & 0x8000) ? val : (0xffff - val + 1) * -1;
}
Buffer.prototype.readInt16LE = function(offset, noAssert) {
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 2, this.length);
return readInt16(this, offset, false);
Buffer.prototype.readInt16BE = function(offset, noAssert) {
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 2, this.length);
return readInt16(this, offset, true);
function readInt32(buffer, offset, isBigEndian) {
var val = readUInt32(buffer, offset, isBigEndian);
- if (!(val & 0x80000000))
- return (val);
- return (0xffffffff - val + 1) * -1;
+ return !(val & 0x80000000) ? val : (0xffffffff - val + 1) * -1;
}
Buffer.prototype.readInt32LE = function(offset, noAssert) {
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 4, this.length);
return readInt32(this, offset, false);
Buffer.prototype.readInt32BE = function(offset, noAssert) {
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 4, this.length);
return readInt32(this, offset, true);
function checkInt(buffer, value, offset, ext, max, min) {
+ if (!(buffer instanceof Buffer))
+ throw new TypeError('buffer must be a Buffer instance');
if (value > max || value < min)
throw new TypeError('value is out of bounds');
- if (offset < 0 || offset + ext > buffer.length || buffer.length + offset < 0)
+ if (offset + ext > buffer.length)
throw new RangeError('index out of range');
}
Buffer.prototype.writeUInt8 = function(value, offset, noAssert) {
value = +value;
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 1, 0xff, 0);
this[offset] = value;
Buffer.prototype.writeUInt16LE = function(value, offset, noAssert) {
value = +value;
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 2, 0xffff, 0);
return writeUInt16(this, value, offset, false);
Buffer.prototype.writeUInt16BE = function(value, offset, noAssert) {
value = +value;
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 2, 0xffff, 0);
return writeUInt16(this, value, offset, true);
Buffer.prototype.writeUInt32LE = function(value, offset, noAssert) {
value = +value;
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 4, 0xffffffff, 0);
return writeUInt32(this, value, offset, false);
Buffer.prototype.writeUInt32BE = function(value, offset, noAssert) {
value = +value;
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 4, 0xffffffff, 0);
return writeUInt32(this, value, offset, true);
Buffer.prototype.writeInt8 = function(value, offset, noAssert) {
value = +value;
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 1, 0x7f, -0x80);
if (value < 0) value = 0xff + value + 1;
Buffer.prototype.writeInt16LE = function(value, offset, noAssert) {
value = +value;
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 2, 0x7fff, -0x8000);
if (value < 0) value = 0xffff + value + 1;
Buffer.prototype.writeInt16BE = function(value, offset, noAssert) {
value = +value;
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 2, 0x7fff, -0x8000);
if (value < 0) value = 0xffff + value + 1;
Buffer.prototype.writeInt32LE = function(value, offset, noAssert) {
value = +value;
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
if (value < 0) value = 0xffffffff + value + 1;
Buffer.prototype.writeInt32BE = function(value, offset, noAssert) {
value = +value;
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
if (value < 0) value = 0xffffffff + value + 1;