Created
September 13, 2010 23:59
-
-
Save akzhan/578286 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
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')); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.