Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cd245b1

Browse filesBrowse files
Martiirvagg
authored andcommitted
doc: clarify API buffer.concat
* Add a simple example for buffer.concat * Change grammar slightly. Fixes: #3219 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> PR-URL: #3255
1 parent 718c304 commit cd245b1
Copy full SHA for cd245b1

File tree

Expand file treeCollapse file tree

1 file changed

+27
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+27
-1
lines changed
Open diff view settings
Collapse file

‎doc/api/buffer.markdown‎

Copy file name to clipboardExpand all lines: doc/api/buffer.markdown
+27-1Lines changed: 27 additions & 1 deletion
  • Display the source diff
  • Display the rich diff
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.