Created
November 3, 2011 21:05
-
-
Save mattrobenolt/1337782 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
* 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()); | |
}); |
This file contains hidden or 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
/* | |
* 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