Created
March 18, 2011 08:45
-
-
Save piscisaureus/875782 to your computer and use it in GitHub Desktop.
node.js bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var FILENAME = "/some/pretty/big/file", | |
BSIZE = 1024 * 1024; | |
var fs = require('fs'); | |
var foo = []; | |
fs.stat(FILENAME, function(err, stats) { | |
if (err) throw err; | |
var size = stats.size; | |
if (size < BSIZE * 50) { | |
throw new Error("You should provide a bigger file"); | |
} | |
fs.open(FILENAME, "r", function(err, fd) { | |
if (err) throw err; | |
function doReads() { | |
for (var i = 0; i < 100; i++) { | |
var buffer = new Buffer(BSIZE); | |
/* Uncomment this line to avoid the bug */ | |
//foo.push(buffer); | |
/* The random offset does not cause the bug but ensures the reads are slow */ | |
var offset = Math.floor(Math.random() * (size - BSIZE)); | |
fs.read(fd, buffer, 0, BSIZE, offset, afterRead); | |
} | |
} | |
function afterRead(err, bytes) { | |
if (err) console.log('read error: ' + err.message); | |
else console.log('read ok: ' + bytes); | |
} | |
doReads(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment