2010-03-19 20:50:29 -07:00
|
|
|
var Buffer = process.binding('buffer').Buffer;
|
|
|
|
|
|
|
|
exports.Buffer = Buffer;
|
|
|
|
|
2010-03-25 09:50:49 -07:00
|
|
|
function toHex (n) {
|
|
|
|
if (n < 16) return "0" + n.toString(16);
|
|
|
|
return n.toString(16);
|
|
|
|
}
|
|
|
|
|
2010-07-15 14:37:03 -07:00
|
|
|
Buffer.isBuffer = function (b) {
|
|
|
|
return b instanceof Buffer;
|
|
|
|
};
|
|
|
|
|
2010-03-26 08:35:46 -07:00
|
|
|
Buffer.prototype.inspect = function () {
|
2010-03-25 09:50:49 -07:00
|
|
|
var s = "<Buffer ";
|
|
|
|
for (var i = 0; i < this.length; i++) {
|
|
|
|
s += toHex(this[i]);
|
|
|
|
if (i != this.length - 1) s += ' ';
|
|
|
|
}
|
|
|
|
s += ">";
|
|
|
|
return s;
|
2010-03-19 20:50:29 -07:00
|
|
|
};
|
|
|
|
|
2010-03-26 08:35:46 -07:00
|
|
|
Buffer.prototype.toString = function (encoding, start, stop) {
|
2010-04-08 16:31:02 -07:00
|
|
|
encoding = (encoding || 'utf8').toLowerCase();
|
2010-03-26 08:35:46 -07:00
|
|
|
if (!start) start = 0;
|
2010-07-20 17:23:30 -05:00
|
|
|
if (stop === undefined) stop = this.length;
|
|
|
|
|
|
|
|
// Fastpath empty strings
|
|
|
|
if (stop === start) {
|
|
|
|
return '';
|
|
|
|
}
|
2010-03-26 08:35:46 -07:00
|
|
|
|
2010-04-08 16:31:02 -07: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);
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Error('Unknown encoding');
|
2010-03-26 08:35:46 -07:00
|
|
|
}
|
2010-03-19 20:50:29 -07:00
|
|
|
};
|
|
|
|
|
2010-06-29 19:10:39 -07:00
|
|
|
Buffer.prototype.write = function (string) {
|
|
|
|
// Support both (string, offset, encoding)
|
|
|
|
// and the legacy (string, encoding, offset)
|
|
|
|
var offset, encoding;
|
|
|
|
|
|
|
|
if (typeof arguments[1] == 'string') {
|
|
|
|
encoding = arguments[1];
|
|
|
|
offset = arguments[2];
|
|
|
|
} else {
|
|
|
|
offset = arguments[1];
|
|
|
|
encoding = arguments[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = offset || 0;
|
|
|
|
encoding = encoding || 'utf8';
|
|
|
|
|
2010-04-08 16:31:02 -07: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);
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Error('Unknown encoding');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-03-19 20:50:29 -07:00
|
|
|
|