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 = [],
return "<SlowBuffer " + out.join(" ") + ">";
};
+
SlowBuffer.prototype.toString = function (encoding, start, stop) {
encoding = String(encoding || 'utf8').toLowerCase();
start = +start || 0;
}
};
+
SlowBuffer.prototype.write = function (string, offset, encoding) {
// Support both (string, offset, encoding)
// and the legacy (string, encoding, offset)
}
};
-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;
exports.Buffer = Buffer;
+
// Static methods
Buffer.isBuffer = function isBuffer(b) {
return b instanceof Buffer;
};
+
// Inspect
Buffer.prototype.inspect = function inspect() {
var out = [],
return "<Buffer " + out.join(" ") + ">";
};
+
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) {
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) {
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);
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");
}
return new Buffer(this.parent, end - start, +start + this.offset, legacy);
};
+