var StringDecoder = exports.StringDecoder = function (encoding){ this.encoding = _AN_Call_replace('replace', (encoding || 'utf8').toLowerCase(), /[-_]/, ''); if (this.encoding === 'utf8') { this.charBuffer = new Buffer(4); this.charReceived = 0; this.charLength = 0; } } ; StringDecoder.prototype.write = function (buffer){ if (this.encoding !== 'utf8') { return buffer.toString(this.encoding); } var charStr = ''; if (this.charLength) { var i = (_AN_Read_length('length', buffer) >= this.charLength - this.charReceived)? this.charLength - this.charReceived: _AN_Read_length('length', buffer); buffer.copy(this.charBuffer, this.charReceived, 0, i); this.charReceived += i; if (this.charReceived < this.charLength) { return ''; } charStr = this.charBuffer.slice(0, this.charLength).toString(); this.charReceived = this.charLength = 0; if (i == _AN_Read_length('length', buffer)) return charStr; buffer = buffer.slice(i, _AN_Read_length('length', buffer)); } var i = (_AN_Read_length('length', buffer) >= 3)? 3: _AN_Read_length('length', buffer); for (; i > 0; i-- ){ var c = buffer[_AN_Read_length('length', buffer) - i]; if (i == 1 && c >> 5 == 6) { this.charLength = 2; break ; } if (i <= 2 && c >> 4 == 14) { this.charLength = 3; break ; } if (i <= 3 && c >> 3 == 30) { this.charLength = 4; break ; } } if (!this.charLength) { return charStr + buffer.toString(); } buffer.copy(this.charBuffer, 0, _AN_Read_length('length', buffer) - i, _AN_Read_length('length', buffer)); this.charReceived = i; if (_AN_Read_length('length', buffer) - i > 0) { return charStr + buffer.toString('utf8', 0, _AN_Read_length('length', buffer) - i); } return charStr; } ;