@@ -127,7 +127,7 @@ Example:
127
127
### Class Method: Buffer.concat(list[ , totalLength] )
128
128
129
129
* ` 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
131
131
132
132
Returns a buffer which is the result of concatenating all the buffers in
133
133
the list together.
@@ -139,6 +139,32 @@ If totalLength is not provided, it is read from the buffers in the list.
139
139
However, this adds an additional loop to the function, so it is faster
140
140
to provide the length explicitly.
141
141
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
+
142
168
### Class Method: Buffer.compare(buf1, buf2)
143
169
144
170
* ` buf1 ` {Buffer}
0 commit comments