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