The readable.read() method reads data out of the internal buffer and
returns it. If no data is available to be read, null is returned. By default,
the data is returned as a Buffer object unless an encoding has been
specified using the readable.setEncoding() method or the stream is operating
in object mode.
The optional size argument specifies a specific number of bytes to read. Ifsize bytes are not available to be read, null will be returned _unless_the stream has ended, in which
case all of the data remaining in the internal
buffer will be returned.
If the size argument is not specified, all of the data contained in the
internal buffer will be returned.
The size argument must be less than or equal to 1 GiB.
The readable.read() method should only be called on Readable streams
operating in paused mode. In flowing mode, readable.read() is called
automatically until the internal buffer is fully drained.
constreadable = getReadableStreamSomehow();
// 'readable' may be triggered multiple times as data is buffered in readable.on('readable', () => { letchunk; console.log('Stream is readable (new data received in buffer)'); // Use a loop to make sure we read all currently available data while (null !== (chunk = readable.read())) { console.log(`Read ${chunk.length} bytes of data...`); } });
// 'end' will be triggered once when there is no more data available readable.on('end', () => { console.log('Reached end of stream.'); });
Each call to readable.read() returns a chunk of data, or null. The chunks
are not concatenated. A while loop is necessary to consume all data
currently in the buffer. When reading a large file .read() may return null,
having consumed all buffered content so far, but there is still more data to
come not yet buffered. In this case a new 'readable' event will be emitted
when there is more data in the buffer. Finally the 'end' event will be
emitted when there is no more data to come.
Therefore to read a file's whole contents from a readable, it is necessary
to collect chunks across multiple 'readable' events:
The
readable.read()
method reads data out of the internal buffer and returns it. If no data is available to be read,null
is returned. By default, the data is returned as aBuffer
object unless an encoding has been specified using thereadable.setEncoding()
method or the stream is operating in object mode.The optional
size
argument specifies a specific number of bytes to read. Ifsize
bytes are not available to be read,null
will be returned _unless_the stream has ended, in which case all of the data remaining in the internal buffer will be returned.If the
size
argument is not specified, all of the data contained in the internal buffer will be returned.The
size
argument must be less than or equal to 1 GiB.The
readable.read()
method should only be called onReadable
streams operating in paused mode. In flowing mode,readable.read()
is called automatically until the internal buffer is fully drained.Each call to
readable.read()
returns a chunk of data, ornull
. The chunks are not concatenated. Awhile
loop is necessary to consume all data currently in the buffer. When reading a large file.read()
may returnnull
, having consumed all buffered content so far, but there is still more data to come not yet buffered. In this case a new'readable'
event will be emitted when there is more data in the buffer. Finally the'end'
event will be emitted when there is no more data to come.Therefore to read a file's whole contents from a
readable
, it is necessary to collect chunks across multiple'readable'
events:A
Readable
stream in object mode will always return a single item from a call toreadable.read(size)
, regardless of the value of thesize
argument.If the
readable.read()
method returns a chunk of data, a'data'
event will also be emitted.Calling read after the
'end'
event has been emitted will returnnull
. No runtime error will be raised.