0
var util = require('util');
var ReadableStream = require('stream').Readable;
function MyReadStream() {
ReadableStream.call(this);
this._index = 0;
this._string = 'Hello World!';
}
util.inherits(MyReadStream, ReadableStream);
MyReadStream.prototype._read = function() {
var i = this._index++;
if (i == this._string.length) {
this.push(null);
this.pipe(process.stdout);
}
else {
var buf = new Buffer(this._string[i], 'utf8');
this.push(buf);
}
};
var readerInst = new MyReadStream();
readerInst.read();
============================================= =なぜnodejsの読み込み可能なストリームが最初の文字をスキップするのですか?
は、なぜ私が 'ello世界!' stdoutを取得します「Hello World!」の代わりに?何が起こっている
しかし、なぜreaderInst._read() 'のHello World' を取得しますか?ありがとうございました 。 '.push()は' '._readの原因となるのでhighwatermarkのは、文字列の長さがデフォルトhighwatermarkのよりもはるかに小さいので、起こることはありませんこれは、ヒットするまで –
()'繰り返し呼び出されます。 – mscdex
しかし、なぜ 'readerInst._read()'は 'readerInst.read()'のように最初の文字を返しますか? readerInst._read() 'と' readerInst.read() 'の違いは何'? –