@@ -118,9 +118,19 @@ next time `buf.write()` is called.
118
118
* ` start ` Number, Optional, Default: 0
119
119
* ` end ` Number, Optional, Default: ` buffer.length `
120
120
121
- Decodes and returns a string from buffer data encoded with ` encoding `
122
- (defaults to ` 'utf8' ` ) beginning at ` start ` (defaults to ` 0 ` ) and ending at
123
- ` end ` (defaults to ` buffer.length ` ).
121
+ Decodes and returns a string from buffer data encoded using the specified
122
+ character set encoding. If ` encoding ` is ` undefined ` or ` null ` , then ` encoding `
123
+ defaults to ` 'utf8'. The ` start` and ` end` parameters default to ` 0` and
124
+ ` buffer.length ` when ` undefined ` .
125
+
126
+ buf = new Buffer(26);
127
+ for (var i = 0 ; i < 26 ; i++) {
128
+ buf[i] = i + 97; // 97 is ASCII a
129
+ }
130
+ buf.toString('ascii'); // outputs: abcdefghijklmnopqrstuvwxyz
131
+ buf.toString('ascii',0,5); // outputs: abcde
132
+ buf.toString('utf8',0,5); // outputs: abcde
133
+ buf.toString(undefined,0,5); // encoding defaults to 'utf8', outputs abcde
124
134
125
135
See ` buffer.write() ` example, above.
126
136
@@ -228,19 +238,28 @@ buffer object. It does not change when the contents of the buffer are changed.
228
238
// 1234
229
239
// 1234
230
240
241
+ While the ` length ` property is not immutable, changing the value of ` length `
242
+ can result in undefined and inconsistent behavior. Applications that wish to
243
+ modify the length of a buffer should therefore treat ` length ` as read-only and
244
+ use ` buf.slice ` to create a new buffer.
245
+
246
+ buf = new Buffer(10);
247
+ buf.write("abcdefghj", 0, "ascii");
248
+ console.log(buf.length); // 10
249
+ buf = buf.slice(0,5);
250
+ console.log(buf.length); // 5
251
+
231
252
### buf.copy(targetBuffer, [ targetStart] , [ sourceStart] , [ sourceEnd] )
232
253
233
254
* ` targetBuffer ` Buffer object - Buffer to copy into
234
255
* ` targetStart ` Number, Optional, Default: 0
235
256
* ` sourceStart ` Number, Optional, Default: 0
236
257
* ` sourceEnd ` Number, Optional, Default: ` buffer.length `
237
258
238
- Does copy between buffers. The source and target regions can be overlapped.
239
- ` targetStart ` and ` sourceStart ` default to ` 0 ` .
240
- ` sourceEnd ` defaults to ` buffer.length ` .
241
-
242
- All values passed that are ` undefined ` /` NaN ` or are out of bounds are set equal
243
- to their respective defaults.
259
+ Copies data from a region of this buffer to a region in the target buffer even
260
+ if the target memory region overlaps with the source. If ` undefined ` the
261
+ ` targetStart ` and ` sourceStart ` parameters default to ` 0 ` while ` sourceEnd `
262
+ defaults to ` buffer.length ` .
244
263
245
264
Example: build two Buffers, then copy ` buf1 ` from byte 16 through byte 19
246
265
into ` buf2 ` , starting at the 8th byte in ` buf2 ` .
@@ -258,6 +277,20 @@ into `buf2`, starting at the 8th byte in `buf2`.
258
277
259
278
// !!!!!!!!qrst!!!!!!!!!!!!!
260
279
280
+ Example: Build a single buffer, then copy data from one region to an overlapping
281
+ region in the same buffer
282
+
283
+ buf = new Buffer(26);
284
+
285
+ for (var i = 0 ; i < 26 ; i++) {
286
+ buf[i] = i + 97; // 97 is ASCII a
287
+ }
288
+
289
+ buf.copy(buf, 0, 4, 10);
290
+ console.log(buf.toString());
291
+
292
+ // efghijghijklmnopqrstuvwxyz
293
+
261
294
262
295
### buf.slice([ start] , [ end] )
263
296
0 commit comments