Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@akzhan
Created September 13, 2010 23:59
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 akzhan/578286 to your computer and use it in GitHub Desktop.
Save akzhan/578286 to your computer and use it in GitHub Desktop.
var body = [];
var body_length = 0;
sourceResponse.on('data', function(chunk)
{
body.push(chunk);
body_length += chunk.length;
});
sourceResponse.on('end', function()
{
clientPool.returnToPool(sourceResponse.client); // it's from my clientPool implementation, not required!
var full_body;
if (body.length == 1)
{
full_body = body[0];
}
else
{
full_body = new Buffer(body_length);
var position = 0;
for (var i = 0; i < body.length; i++)
{
body[i].copy(full_body, position, 0);
position += body[i].length;
}
}
body = null;
callback(null, full_body.toString('utf8'));
});
@akzhan
Copy link
Author

akzhan commented Sep 14, 2010

Take a note that convertion to utf8 string done after concatenation of all chunks together. This is because chunks can divide utf8 characters (up to 4 bytes each) between them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment