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();
============================================= =Warum Nodejs lesbaren Stream den ersten Buchstaben überspringen?
Warum ich stdout 'ello World!' anstelle von 'Hallo Welt!'
Aber warum readerInst._read() bekommen "Hallo Welt"? Vielen Dank . –
Weil die '.push()' bewirkt, dass '._read()' wiederholt aufgerufen wird, bis die highWaterMark erreicht ist, was nie passiert, weil die Stringlänge viel kleiner ist als die standardmäßige highWaterMark. – mscdex
Aber warum nicht 'readerInst._read()' gibt das erste Zeichen als 'readerInst.read()' zurück? Was ist der Unterschied zwischen 'readerInst._read()' und 'readerInst.read()'? –