Browse Source

Use Utf8Stream for net Streams with utf8 encoding

v0.7.4-release
Felix Geisendörfer 15 years ago
committed by Ryan Dahl
parent
commit
ed5f1162fa
  1. 12
      lib/net.js

12
lib/net.js

@ -1,5 +1,6 @@
var sys = require("sys");
var fs = require("fs");
var Utf8Decoder = require("utf8decoder").Utf8Decoder;
var events = require("events");
var dns = require('dns');
@ -381,6 +382,8 @@ function initStream (self) {
// Optimization: emit the original buffer with end points
if (self.ondata) self.ondata(pool, start, end);
} else if (this._decoder) {
this._decoder.write(pool.slice(start, end));
} else {
var string = pool.toString(self._encoding, start, end);
self.emit('data', string);
@ -713,8 +716,17 @@ Stream.prototype._writeQueueLast = function () {
Stream.prototype.setEncoding = function (enc) {
var self = this;
// TODO check values, error out on bad, and deprecation message?
this._encoding = enc.toLowerCase();
if (this._encoding == 'utf-8' || this._encoding == 'utf8') {
this._decoder = new Utf8Decoder();
this._decoder.onString = function(str) {
self.emit('data', str);
};
} else if (this._decoder) {
delete this._decoder;
}
};

Loading…
Cancel
Save