2010-03-20 11:50:29 +08:00
|
|
|
var Buffer = process.binding('buffer').Buffer;
|
|
|
|
|
|
|
|
exports.Buffer = Buffer;
|
|
|
|
|
2010-03-26 00:50:49 +08:00
|
|
|
function toHex (n) {
|
|
|
|
if (n < 16) return "0" + n.toString(16);
|
|
|
|
return n.toString(16);
|
|
|
|
}
|
|
|
|
|
2010-07-16 05:37:03 +08:00
|
|
|
Buffer.isBuffer = function (b) {
|
|
|
|
return b instanceof Buffer;
|
|
|
|
};
|
|
|
|
|
2010-03-26 23:35:46 +08:00
|
|
|
Buffer.prototype.inspect = function () {
|
2010-07-22 19:51:26 +08:00
|
|
|
var out = [],
|
|
|
|
len = this.length;
|
|
|
|
for (var i = 0; i < len; i++) {
|
|
|
|
out[i] = toHex(this[i]);
|
2010-03-26 00:50:49 +08:00
|
|
|
}
|
2010-07-22 19:51:26 +08:00
|
|
|
return "<Buffer " + out.join(" ") + ">";
|
2010-03-20 11:50:29 +08:00
|
|
|
};
|
|
|
|
|
2010-03-26 23:35:46 +08:00
|
|
|
Buffer.prototype.toString = function (encoding, start, stop) {
|
2010-07-22 19:51:26 +08:00
|
|
|
encoding = String(encoding || 'utf8').toLowerCase();
|
|
|
|
start = +start || 0;
|
|
|
|
if (typeof stop == "undefined") stop = this.length;
|
2010-07-21 06:23:30 +08:00
|
|
|
|
|
|
|
// Fastpath empty strings
|
2010-07-22 19:51:26 +08:00
|
|
|
if (+stop == start) {
|
2010-07-21 06:23:30 +08:00
|
|
|
return '';
|
|
|
|
}
|
2010-03-26 23:35:46 +08:00
|
|
|
|
2010-04-09 07:31:02 +08:00
|
|
|
switch (encoding) {
|
|
|
|
case 'utf8':
|
|
|
|
case 'utf-8':
|
|
|
|
return this.utf8Slice(start, stop);
|
|
|
|
|
|
|
|
case 'ascii':
|
|
|
|
return this.asciiSlice(start, stop);
|
|
|
|
|
|
|
|
case 'binary':
|
|
|
|
return this.binarySlice(start, stop);
|
|
|
|
|
2010-07-24 04:52:44 +08:00
|
|
|
case 'base64':
|
|
|
|
return this.base64Slice(start, stop);
|
|
|
|
|
2010-04-09 07:31:02 +08:00
|
|
|
default:
|
|
|
|
throw new Error('Unknown encoding');
|
2010-03-26 23:35:46 +08:00
|
|
|
}
|
2010-03-20 11:50:29 +08:00
|
|
|
};
|
|
|
|
|
2010-07-22 19:51:26 +08:00
|
|
|
Buffer.prototype.write = function (string, offset, encoding) {
|
2010-06-30 10:10:39 +08:00
|
|
|
// Support both (string, offset, encoding)
|
|
|
|
// and the legacy (string, encoding, offset)
|
2010-07-22 19:51:26 +08:00
|
|
|
if (!isFinite(offset)) {
|
|
|
|
var swap = encoding;
|
|
|
|
encoding = offset;
|
|
|
|
offset = swap;
|
2010-06-30 10:10:39 +08:00
|
|
|
}
|
|
|
|
|
2010-07-22 19:51:26 +08:00
|
|
|
offset = +offset || 0;
|
|
|
|
encoding = String(encoding || 'utf8').toLowerCase();
|
2010-06-30 10:10:39 +08:00
|
|
|
|
2010-04-09 07:31:02 +08:00
|
|
|
switch (encoding) {
|
|
|
|
case 'utf8':
|
|
|
|
case 'utf-8':
|
|
|
|
return this.utf8Write(string, offset);
|
|
|
|
|
|
|
|
case 'ascii':
|
|
|
|
return this.asciiWrite(string, offset);
|
|
|
|
|
|
|
|
case 'binary':
|
|
|
|
return this.binaryWrite(string, offset);
|
|
|
|
|
2010-07-24 07:36:47 +08:00
|
|
|
case 'base64':
|
|
|
|
return this.base64Write(string, offset);
|
|
|
|
|
2010-04-09 07:31:02 +08:00
|
|
|
default:
|
|
|
|
throw new Error('Unknown encoding');
|
|
|
|
}
|
2010-08-20 14:29:06 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Buffer.prototype.get = function (index) {
|
|
|
|
return this[index];
|
|
|
|
};
|
|
|
|
|
|
|
|
Buffer.prototype.set = function (index, value) {
|
|
|
|
return this[index] = value;
|
|
|
|
};
|