Skip to content

Commit cd245b1

Browse files
Martiirvagg
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 718c304 commit cd245b1

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
@@ -126,7 +126,7 @@ Example:
126126
### Class Method: Buffer.concat(list[, totalLength])
127127

128128
* `list` {Array} List of Buffer objects to concat
129-
* `totalLength` {Number} Total length of the buffers when concatenated
129+
* `totalLength` {Number} Total length of the buffers in the list when concatenated
130130

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

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

143169
* `buf1` {Buffer}

0 commit comments

Comments
 (0)