readable._read(size)


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

此函数绝对不能被应用代码直接调用。它应由子类实现,并且仅由内部的 Readable 类方法调用。

【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.】

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

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

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

【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.】

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

【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.】

size 参数仅供参考。在某些实现中,‘读取’ 是一次返回数据的单操作,可以使用 size 参数来确定获取多少数据。其他实现可能会忽略此参数,只要数据可用就提供数据。调用 stream.push(chunk) 时,无需等待 size 字节的数据全部可用。

【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).】

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

【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.】