readable._read(size)


  • size <number> 异步地读取的字节数

此函数不得由应用程序代码直接调用。 它应该由子类实现,并且只能由内部 Readable 类方法调用。

所有 Readable 流实现都必须提供 readable._read() 方法的实现,以从底层资源中获取数据。

调用 readable._read() 时,如果资源中的数据可用,则实现应开始使用 this.push(dataChunk) 方法将该数据推送到读取队列中。 一旦流准备好接受更多数据,则 _read() 将在每次调用 this.push(dataChunk) 后再次调用。 _read() 可能会继续从资源中读取并推送数据,直到 readable.push() 返回 false。 只有当 _read() 停止后再次被调用时,它才能继续将额外的数据推入队列。

一旦调用了 readable._read() 方法,则不会再次调用它,直到通过 readable.push() 方法推送更多数据。 空缓冲区和字符串等空数据不会导致调用 readable._read()

size 参数是建议性的。 对于“读取”是返回数据的单个操作的实现,可以使用 size 参数来确定要获取多少数据。 其他实现可能会忽略此参数,并在数据可用时简单地提供数据。 在调用 stream.push(chunk) 之前不需要“等待”直到 size 个字节可用。

readable._read() 方法以下划线为前缀,因为它是定义它的类的内部方法,不应由用户程序直接调用。

  • size <number> Number of bytes to read asynchronously

This function MUST NOT be called by application code directly. It should be implemented by child classes, and called by the internal Readable class methods only.

All Readable stream implementations must provide an implementation of the readable._read() method to fetch data from the underlying resource.

When readable._read() is called, if data is available from the resource, the implementation should begin pushing that data into the read queue using the this.push(dataChunk) method. _read() will be called again after each call to this.push(dataChunk) once the stream is ready to accept more data. _read() may continue reading from the resource and pushing data until readable.push() returns false. Only when _read() is called again after it has stopped should it resume pushing additional data into the queue.

Once the readable._read() method has been called, it will not be called again until more data is pushed through the readable.push() method. Empty data such as empty buffers and strings will not cause readable._read() to be called.

The size argument is advisory. For implementations where a "read" is a single operation that returns data can use the size argument to determine how much data to fetch. Other implementations may ignore this argument and simply provide data whenever it becomes available. There is no need to "wait" until size bytes are available before calling stream.push(chunk).

The readable._read() method is prefixed with an underscore because it is internal to the class that defines it, and should never be called directly by user programs.