关于流私有方法到底是什么鬼?
fs模块创建的流和stream创建的可读可写流有什么区别吗,还有_write,_read这些到底是什么?
fs模块创建的流和stream创建的可读可写流有什么区别吗,还有_write,_read这些到底是什么?
2016-05-14
// abstract method. to be overridden in specific implementation classes.
// call cb(er, data) where data is <= n in length.
// for virtual (non-string, non-buffer) streams, "length" is somewhat
// arbitrary, and perhaps not very meaningful.
Readable.prototype._read = function(n) {
this.emit('error', new Error('not implemented'));
};
https://github.com/nodejs/node/blob/master/lib/_stream_readable.js
这是他的解释,它是readable里的一个抽象方法,这里是对这个抽象方法进行了实现,而fs.js里面已经对这个方法实现过了
fs.createReadStream = function(path, options) { return new ReadStream(path, options); }; ----------------- ReadStream.prototype._read = function(n) { if (typeof this.fd !== 'number') return this.once('open', function() { this._read(n); }); if (this.destroyed) return; if (!pool || pool.length - pool.used < kMinPoolSpace) { // discard the old pool. pool = null; allocNewPool(this._readableState.highWaterMark); }
代码是部分的,大概是告诉你这是真的
https://github.com/nodejs/node/blob/master/lib/fs.js
有兴趣自己去看看吧。
举报