Created
July 1, 2017 04:33
-
-
Save magicianShiro/d1b5c4423b01545419fcaa160a086e78 to your computer and use it in GitHub Desktop.
拼接buffer
This file contains 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 chunks = []; | |
var size = 0; | |
res.on('data', function (chunk) { | |
chunks.push(chunk); | |
size += chunk.length; | |
}); | |
res.on('end', function () { | |
var data = null; | |
switch(chunks.length) { | |
case 0: data = new Buffer(0); | |
break; | |
case 1: data = chunks[0]; | |
break; | |
default: | |
data = new Buffer(size); | |
for (var i = 0, pos = 0, l = chunks.length; i < l; i++) { | |
var chunk = chunks[i]; | |
chunk.copy(data, pos); | |
pos += chunk.length; | |
} | |
break; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment