Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
errpunk opened this issue Oct 12, 2016 · 2 comments
Closed

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

errpunk opened this issue Oct 12, 2016 · 2 comments
Labels
fs Issues and PRs related to the fs subsystem / file system.

Comments

@errpunk
Copy link

errpunk commented Oct 12, 2016

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>
@mscdex mscdex added the fs Issues and PRs related to the fs subsystem / file system. label Oct 12, 2016
@vsemozhetbyt
Copy link
Contributor

vsemozhetbyt commented Oct 14, 2016

See nodejs/help#198 for explanation.

@errpunk
Copy link
Author

errpunk commented Oct 17, 2016

Thanks for the help @vsemozhetbyt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fs Issues and PRs related to the fs subsystem / file system.
Projects
None yet
Development

No branches or pull requests

3 participants