Skip to content

Commit

Permalink
doc: clarify API buffer.concat
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Martii authored and jasnell committed Oct 26, 2015
1 parent 282618e commit 4afcf57
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion doc/api/buffer.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ Example:
### Class Method: Buffer.concat(list[, totalLength])

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

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

Example: build a single buffer from a list of three buffers:

var buf1 = new Buffer(10);
var buf2 = new Buffer(14);
var buf3 = new Buffer(18);

buf1.fill(0);
buf2.fill(0);
buf3.fill(0);

var buffers = [buf1, buf2, buf3];

var totalLength = 0;
for (var i = 0; i < buffers.length; i++) {
totalLength += buffers[i].length;
}

console.log(totalLength);
var bufA = Buffer.concat(buffers, totalLength);
console.log(bufA);
console.log(bufA.length);

// 42
// <Buffer 00 00 00 00 ...>
// 42

### Class Method: Buffer.compare(buf1, buf2)

* `buf1` {Buffer}
Expand Down

0 comments on commit 4afcf57

Please sign in to comment.