Skip to content

Instantly share code, notes, and snippets.

@mattrobenolt
Created November 3, 2011 21:05
Show Gist options
  • Save mattrobenolt/1337782 to your computer and use it in GitHub Desktop.
Save mattrobenolt/1337782 to your computer and use it in GitHub Desktop.
/*
* This is how we have to do it now.
*/
var buffers = [];
var nread = 0;
something.on('data', function(chunk) {
buffers.push(chunk);
nread += chunk.length;
});
something.on('end', function() {
var buffer;
switch(buffers.length) {
case 0: buffer = new Buffer(0); break;
case 1: buffer = buffers[0]; break;
default:
buffer = new Buffer(nread);
var n = 0;
buffers.forEach(function(b) {
var l = b.length;
b.copy(buffer, n, 0, l);
n += l;
});
break;
}
console.log(buffer.toString());
});
/*
* Would it make sense to implement something like this?
* Maybe a new class "BufferStream" instead of stacking
* on top of Buffer.
*/
var buffer = new Buffer();
something.on('data', buffer.onData);
something.on('end', function(){
buffer = buffer.end();
console.log(buffer.toString());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment