Skip to content

Instantly share code, notes, and snippets.

@bobrik
Created September 4, 2012 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobrik/3621892 to your computer and use it in GitHub Desktop.
Save bobrik/3621892 to your computer and use it in GitHub Desktop.
// writing 3 buffers starting from offset 0
// 3 bytes | 5 bytes | 10 bytes
// second | first | third
// last byte of the last buffer should be at offset 18
// file should be 18 bytes long
// this is WRONG on linux, file grows up for 18 bytes after each run
// and every single written buffer can't be read properly
//
// holy fuck, the whole day lost.
var fs = require("fs"),
f = fs.openSync("ae", "a+");
b1 = new Buffer(5);
b1t = new Buffer(b1.length);
b2 = new Buffer(3);
b2t = new Buffer(b2.length);
b3 = new Buffer(10);
b3t = new Buffer(b3.length);
fs.write(f, b1, 0, b1.length, b2.length, function(error) {
if (error) {
return console.log(error);
}
fs.write(f, b2, 0, b2.length, 0, function(error) {
if (error) {
return console.log(error);
}
fs.write(f, b3, 0, b3.length, b1.length + b2.length, function(error) {
if (error) {
return console.log(error);
}
fs.read(f, b1t, 0, b1t.length, b2.length, function(error) {
if (error) {
return console.log(error);
}
if (b1t.toString("base64") != b1.toString("base64")) {
console.log("wrong first buffer!", b1, b1t);
}
fs.read(f, b2t, 0, b2t.length, 0, function(error) {
if (error) {
return console.log(error);
}
if (b2t.toString("base64") != b2.toString("base64")) {
console.log("wrong second buffer!");
}
fs.read(f, b3t, 0, b3t.length, b1.length + b2.length, function(error) {
if (error) {
return console.log(error);
}
if (b3t.toString("base64") != b3.toString("base64")) {
console.log("wrong third buffer!");
}
});
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment