Skip to content

Commit e04cb1e

Browse files
Martiijasnell
Martii
authored andcommitted
doc: clarify API buffer.concat
* Add a simple example for buffer.concat * Change grammar slightly. Fixes: #3219 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trevor Norris <[email protected]> PR-URL: #3255
1 parent eae714c commit e04cb1e

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

doc/api/buffer.markdown

+27-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ Example:
127127
### Class Method: Buffer.concat(list[, totalLength])
128128

129129
* `list` {Array} List of Buffer objects to concat
130-
* `totalLength` {Number} Total length of the buffers when concatenated
130+
* `totalLength` {Number} Total length of the buffers in the list when concatenated
131131

132132
Returns a buffer which is the result of concatenating all the buffers in
133133
the list together.
@@ -139,6 +139,32 @@ If totalLength is not provided, it is read from the buffers in the list.
139139
However, this adds an additional loop to the function, so it is faster
140140
to provide the length explicitly.
141141

142+
Example: build a single buffer from a list of three buffers:
143+
144+
var buf1 = new Buffer(10);
145+
var buf2 = new Buffer(14);
146+
var buf3 = new Buffer(18);
147+
148+
buf1.fill(0);
149+
buf2.fill(0);
150+
buf3.fill(0);
151+
152+
var buffers = [buf1, buf2, buf3];
153+
154+
var totalLength = 0;
155+
for (var i = 0; i < buffers.length; i++) {
156+
totalLength += buffers[i].length;
157+
}
158+
159+
console.log(totalLength);
160+
var bufA = Buffer.concat(buffers, totalLength);
161+
console.log(bufA);
162+
console.log(bufA.length);
163+
164+
// 42
165+
// <Buffer 00 00 00 00 ...>
166+
// 42
167+
142168
### Class Method: Buffer.compare(buf1, buf2)
143169

144170
* `buf1` {Buffer}

0 commit comments

Comments
 (0)