可写流中的解码缓冲区


\Decoding buffers in a writable stream

解码缓冲区是一项常见任务,例如,在使用输入为字符串的转换器时。当使用多字节字符编码(例如 UTF-8)时,这不是一个简单的过程。以下示例显示如何使用 StringDecoderWritable 解码多字节字符串。

\Decoding buffers is a common task, for instance, when using transformers whose input is a string. This is not a trivial process when using multi-byte characters encoding, such as UTF-8. The following example shows how to decode multi-byte strings using StringDecoder and Writable.

const { Writable } = require('node:stream');
const { StringDecoder } = require('node:string_decoder');
class StringWritable extends Writable {
 constructor(options) {
 super(options);
 this._decoder = new StringDecoder(options?.defaultEncoding);
 this.data = '';
 }
 _write(chunk, encoding, callback) {
 if (encoding === 'buffer') {
 chunk = this._decoder.write(chunk);
 }
 this.data += chunk;
 callback();
 }
 _final(callback) {
 this.data += this._decoder.end();
 callback();
 }
}
const euro = [[0xE2, 0x82], [0xAC]].map(Buffer.from);
const w = new StringWritable();
w.write('currency: ');
w.write(euro[0]);
w.end(euro[1]);
console.log(w.data); // currency: € 

AltStyle によって変換されたページ (->オリジナル) /