fs.ReadStrem.read(size) trigger 'readable' event twice or once #9046
Closed
Description
- Version: v4.5.0
- Platform: Linux *** 3.13.0-95-generic docs: Supplement docs for Writable and Transform streams #142-Ubuntu SMP Fri Aug 12 17:00:09 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
- Subsystem: fs
I'm using read(2)
to read 2 byte data of a file, and I found that the read(2)
method trigger readable
event twice if the file I'm reading is small (in my case, the file is 14 bytes). however, if the file I read from is large (1.3 GB, in my case), the read(2)
method will only trigger readable
event once.
Here is the code:
const fs = require('fs');
var aBigFile = fs.createReadStream("/home/kt/ubuntu-gnome-16.04.1-desktop-amd64.iso");
var aSmallFile = fs.createReadStream("/home/kt/1.txt");
var chunkCount = 0;
var bigFileChunkCount = 0;
aSmallFile.on('readable', ()=>{
console.log(`[${chunkCount++}]smallfile:`, aSmallFile.read(2));
});
aBigFile.on('readable', ()=>{
console.log(`[${bigFileChunkCount++}]bigfile:`, aBigFile.read(2));
});
and the output goes:
[0]bigfile: <Buffer 4b 44>
[0]smallfile: <Buffer 61 e4>
[1]smallfile: <Buffer bd a0>
Activity