Skip to content

Instantly share code, notes, and snippets.

@nat-n
Last active December 30, 2015 15:19
Show Gist options
  • Save nat-n/7847459 to your computer and use it in GitHub Desktop.
Save nat-n/7847459 to your computer and use it in GitHub Desktop.
A proof of concept script for streaming a binary file of variable length integers through a nodejs context.
var fs, vi, sys, rs, bufferSize, remainder, startTime;
startTime = Date.now();
fs = require('fs');
vi = require('varint');
sys = require('sys');
rs = fs.createReadStream('/path/to.file');
total = 0;
rs.on('data', function(buffer) {
var buffer, value, byteLen, offset;
if (remainder) {
buffer = Buffer.concat([remainder, buffer]);
remainder = null;
}
offset = 0;
while(offset < buffer.length){
// decode the value and check how many bytes it required
value = vi.decode(buffer, offset);
offset += vi.decode.bytesRead;
if (offset <= buffer.length){
// keep the result and update position in the buffer
total += value;
} else {
// result is incomplete, store remainder to be prepended to the next chunk
remainder = new Buffer(buffer.length - offset);
buffer.copy(remainder, 0, offset);
}
}
});
rs.on('end', function() {
sys.puts((total));
sys.puts(Date.now() - startTime);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment