Skip to content

fs.ReadStrem.read(size) trigger 'readable' event twice or once #9046

Closed
@errpunk

Description

@errpunk

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>

Metadata

Metadata

Assignees

No one assigned

    Labels

    fsIssues and PRs related to the fs subsystem / file system.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions