@@ -120,8 +120,6 @@ impact* on performance. Use of the `--zero-fill-buffers` option is recommended
120
120
only when necessary to enforce that newly allocated ` Buffer ` instances cannot
121
121
contain potentially sensitive data.
122
122
123
- Example:
124
-
125
123
``` txt
126
124
$ node --zero-fill-buffers
127
125
> Buffer.allocUnsafe(5);
@@ -157,8 +155,6 @@ such as UTF-8, UCS2, Base64, or even Hex-encoded data. It is possible to
157
155
convert back and forth between ` Buffer ` instances and ordinary JavaScript strings
158
156
by using an explicit character encoding.
159
157
160
- Example:
161
-
162
158
``` js
163
159
const buf = Buffer .from (' hello world' , ' ascii' );
164
160
@@ -229,8 +225,6 @@ elements, and not as a byte array of the target type. That is,
229
225
It is possible to create a new ` Buffer ` that shares the same allocated memory as
230
226
a [ ` TypedArray ` ] instance by using the TypeArray object's ` .buffer ` property.
231
227
232
- Example:
233
-
234
228
``` js
235
229
const arr = new Uint16Array (2 );
236
230
@@ -259,8 +253,6 @@ Note that when creating a `Buffer` using a [`TypedArray`]'s `.buffer`, it is
259
253
possible to use only a portion of the underlying [ ` ArrayBuffer ` ] by passing in
260
254
` byteOffset ` and ` length ` parameters.
261
255
262
- Example:
263
-
264
256
``` js
265
257
const arr = new Uint16Array (20 );
266
258
const buf = Buffer .from (arr .buffer , 0 , 16 );
@@ -289,8 +281,6 @@ function:
289
281
` Buffer ` instances can be iterated over using the [ ` ECMAScript 2015 ` ] (ES6) ` for..of `
290
282
syntax.
291
283
292
- Example:
293
-
294
284
``` js
295
285
const buf = Buffer .from ([1 , 2 , 3 ]);
296
286
@@ -329,8 +319,6 @@ changes:
329
319
330
320
Allocates a new ` Buffer ` using an ` array ` of octets.
331
321
332
- Example:
333
-
334
322
``` js
335
323
// Creates a new Buffer containing the UTF-8 bytes of the string 'buffer'
336
324
const buf = new Buffer ([0x62 , 0x75 , 0x66 , 0x66 , 0x65 , 0x72 ]);
@@ -370,8 +358,6 @@ share the same allocated memory as the [`TypedArray`].
370
358
The optional ` byteOffset ` and ` length ` arguments specify a memory range within
371
359
the ` arrayBuffer ` that will be shared by the ` Buffer ` .
372
360
373
- Example:
374
-
375
361
``` js
376
362
const arr = new Uint16Array (2 );
377
363
@@ -409,8 +395,6 @@ changes:
409
395
410
396
Copies the passed ` buffer ` data onto a new ` Buffer ` instance.
411
397
412
- Example:
413
-
414
398
``` js
415
399
const buf1 = new Buffer (' buffer' );
416
400
const buf2 = new Buffer (buf1);
@@ -453,8 +437,6 @@ created in this way is *not initialized*. The contents of a newly created
453
437
[ ` Buffer.alloc(size) ` ] [ `Buffer.alloc()` ] instead to initialize a ` Buffer `
454
438
to zeroes.
455
439
456
- Example:
457
-
458
440
``` js
459
441
const buf = new Buffer (10 );
460
442
@@ -522,8 +504,6 @@ changes:
522
504
Allocates a new ` Buffer ` of ` size ` bytes. If ` fill ` is ` undefined ` , the
523
505
` Buffer ` will be * zero-filled* .
524
506
525
- Example:
526
-
527
507
``` js
528
508
const buf = Buffer .alloc (5 );
529
509
@@ -538,8 +518,6 @@ thrown. A zero-length `Buffer` will be created if `size` is 0.
538
518
If ` fill ` is specified, the allocated ` Buffer ` will be initialized by calling
539
519
[ ` buf.fill(fill) ` ] [ `buf.fill()` ] .
540
520
541
- Example:
542
-
543
521
``` js
544
522
const buf = Buffer .alloc (5 , ' a' );
545
523
@@ -550,8 +528,6 @@ console.log(buf);
550
528
If both ` fill ` and ` encoding ` are specified, the allocated ` Buffer ` will be
551
529
initialized by calling [ ` buf.fill(fill, encoding) ` ] [ `buf.fill()` ] .
552
530
553
- Example:
554
-
555
531
``` js
556
532
const buf = Buffer .alloc (11 , ' aGVsbG8gd29ybGQ=' , ' base64' );
557
533
@@ -585,8 +561,6 @@ initialized*. The contents of the newly created `Buffer` are unknown and
585
561
* may contain sensitive data* . Use [ ` Buffer.alloc() ` ] instead to initialize
586
562
` Buffer ` instances to zeroes.
587
563
588
- Example:
589
-
590
564
``` js
591
565
const buf = Buffer .allocUnsafe (10 );
592
566
@@ -643,8 +617,6 @@ memory from a pool for an indeterminate amount of time, it may be appropriate
643
617
to create an un-pooled ` Buffer ` instance using ` Buffer.allocUnsafeSlow() ` then
644
618
copy out the relevant bits.
645
619
646
- Example:
647
-
648
620
``` js
649
621
// Need to keep around a few small chunks of memory
650
622
const store = [];
@@ -694,8 +666,6 @@ For `'base64'` and `'hex'`, this function assumes valid input. For strings that
694
666
contain non-Base64/Hex-encoded data (e.g. whitespace), the return value might be
695
667
greater than the length of a ` Buffer ` created from the string.
696
668
697
- Example:
698
-
699
669
``` js
700
670
const str = ' \u00bd + \u00bc = \u00be ' ;
701
671
@@ -724,8 +694,6 @@ Compares `buf1` to `buf2` typically for the purpose of sorting arrays of
724
694
` Buffer ` instances. This is equivalent to calling
725
695
[ ` buf1.compare(buf2) ` ] [ `buf.compare()` ] .
726
696
727
- Example:
728
-
729
697
``` js
730
698
const buf1 = Buffer .from (' 1234' );
731
699
const buf2 = Buffer .from (' 0123' );
@@ -765,9 +733,9 @@ If `totalLength` is provided, it is coerced to an unsigned integer. If the
765
733
combined length of the ` Buffer ` s in ` list ` exceeds ` totalLength ` , the result is
766
734
truncated to ` totalLength ` .
767
735
768
- Example: Create a single ` Buffer ` from a list of three ` Buffer ` instances
769
-
770
736
``` js
737
+ // Create a single `Buffer` from a list of three `Buffer` instances.
738
+
771
739
const buf1 = Buffer .alloc (10 );
772
740
const buf2 = Buffer .alloc (14 );
773
741
const buf3 = Buffer .alloc (18 );
@@ -793,8 +761,6 @@ added: v5.10.0
793
761
794
762
Allocates a new ` Buffer ` using an ` array ` of octets.
795
763
796
- Example:
797
-
798
764
``` js
799
765
// Creates a new Buffer containing UTF-8 bytes of the string 'buffer'
800
766
const buf = Buffer .from ([0x62 , 0x75 , 0x66 , 0x66 , 0x65 , 0x72 ]);
@@ -818,8 +784,6 @@ memory. For example, when passed a reference to the `.buffer` property of a
818
784
[ ` TypedArray ` ] instance, the newly created ` Buffer ` will share the same
819
785
allocated memory as the [ ` TypedArray ` ] .
820
786
821
- Example:
822
-
823
787
``` js
824
788
const arr = new Uint16Array (2 );
825
789
@@ -842,8 +806,6 @@ console.log(buf);
842
806
The optional ` byteOffset ` and ` length ` arguments specify a memory range within
843
807
the ` arrayBuffer ` that will be shared by the ` Buffer ` .
844
808
845
- Example:
846
-
847
809
``` js
848
810
const ab = new ArrayBuffer (10 );
849
811
const buf = Buffer .from (ab, 0 , 2 );
@@ -864,8 +826,6 @@ added: v5.10.0
864
826
865
827
Copies the passed ` buffer ` data onto a new ` Buffer ` instance.
866
828
867
- Example:
868
-
869
829
``` js
870
830
const buf1 = Buffer .from (' buffer' );
871
831
const buf2 = Buffer .from (buf1);
@@ -984,9 +944,9 @@ This operator is inherited from `Uint8Array`, so its behavior on out-of-bounds
984
944
access is the same as ` UInt8Array ` - that is, getting returns ` undefined ` and
985
945
setting does nothing.
986
946
987
- Example: Copy an ASCII string into a ` Buffer ` , one byte at a time
988
-
989
947
``` js
948
+ // Copy an ASCII string into a `Buffer` one byte at a time.
949
+
990
950
const str = ' Node.js' ;
991
951
const buf = Buffer .allocUnsafe (str .length );
992
952
@@ -1098,10 +1058,8 @@ added: v0.1.90
1098
1058
Copies data from a region of ` buf ` to a region in ` target ` even if the ` target `
1099
1059
memory region overlaps with ` buf ` .
1100
1060
1101
- Example: Create two ` Buffer ` instances, ` buf1 ` and ` buf2 ` , and copy ` buf1 ` from
1102
- byte 16 through byte 19 into ` buf2 ` , starting at the 8th byte in ` buf2 `
1103
-
1104
1061
``` js
1062
+ // Create two `Buffer` instances.
1105
1063
const buf1 = Buffer .allocUnsafe (26 );
1106
1064
const buf2 = Buffer .allocUnsafe (26 ).fill (' !' );
1107
1065
@@ -1110,16 +1068,17 @@ for (let i = 0; i < 26; i++) {
1110
1068
buf1[i] = i + 97 ;
1111
1069
}
1112
1070
1071
+ // Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`
1113
1072
buf1 .copy (buf2, 8 , 16 , 20 );
1114
1073
1115
1074
console .log (buf2 .toString (' ascii' , 0 , 25 ));
1116
1075
// Prints: !!!!!!!!qrst!!!!!!!!!!!!!
1117
1076
```
1118
1077
1119
- Example: Create a single ` Buffer ` and copy data from one region to an
1120
- overlapping region within the same ` Buffer `
1121
-
1122
1078
``` js
1079
+ // Create a `Buffer` and copy data from one region to an overlapping region
1080
+ // within the same `Buffer`.
1081
+
1123
1082
const buf = Buffer .allocUnsafe (26 );
1124
1083
1125
1084
for (let i = 0 ; i < 26 ; i++ ) {
@@ -1143,9 +1102,9 @@ added: v1.1.0
1143
1102
Creates and returns an [ iterator] of ` [index, byte] ` pairs from the contents of
1144
1103
` buf ` .
1145
1104
1146
- Example: Log the entire contents of a ` Buffer `
1147
-
1148
1105
``` js
1106
+ // Log the entire contents of a `Buffer`.
1107
+
1149
1108
const buf = Buffer .from (' buffer' );
1150
1109
1151
1110
for (const pair of buf .entries ()) {
@@ -1217,9 +1176,9 @@ Fills `buf` with the specified `value`. If the `offset` and `end` are not given,
1217
1176
the entire ` buf ` will be filled. This is meant to be a small simplification to
1218
1177
allow the creation and filling of a ` Buffer ` to be done on a single line.
1219
1178
1220
- Example: Fill a ` Buffer ` with the ASCII character ` 'h' `
1221
-
1222
1179
``` js
1180
+ // Fill a `Buffer` with the ASCII character 'h'.
1181
+
1223
1182
const b = Buffer .allocUnsafe (50 ).fill (' h' );
1224
1183
1225
1184
console .log (b .toString ());
@@ -1231,9 +1190,9 @@ console.log(b.toString());
1231
1190
If the final write of a ` fill() ` operation falls on a multi-byte character,
1232
1191
then only the first bytes of that character that fit into ` buf ` are written.
1233
1192
1234
- Example: Fill a ` Buffer ` with a two-byte character
1235
-
1236
1193
``` js
1194
+ // Fill a `Buffer` with a two-byte character.
1195
+
1237
1196
console .log (Buffer .allocUnsafe (3 ).fill (' \u0222 ' ));
1238
1197
// Prints: <Buffer c8 a2 c8>
1239
1198
```
@@ -1374,8 +1333,6 @@ added: v1.1.0
1374
1333
1375
1334
Creates and returns an [ iterator] of ` buf ` keys (indices).
1376
1335
1377
- Example:
1378
-
1379
1336
``` js
1380
1337
const buf = Buffer .from (' buffer' );
1381
1338
@@ -1476,9 +1433,9 @@ added: v0.1.90
1476
1433
Returns the amount of memory allocated for ` buf ` in bytes. Note that this
1477
1434
does not necessarily reflect the amount of "usable" data within ` buf ` .
1478
1435
1479
- Example: Create a ` Buffer ` and write a shorter ASCII string to it
1480
-
1481
1436
``` js
1437
+ // Create a `Buffer` and write a shorter ASCII string to it.
1438
+
1482
1439
const buf = Buffer .alloc (1234 );
1483
1440
1484
1441
console .log (buf .length );
@@ -1850,10 +1807,10 @@ that of `end` equal to [`buf.length`].
1850
1807
Modifying the new ` Buffer ` slice will modify the memory in the original ` Buffer `
1851
1808
because the allocated memory of the two objects overlap.
1852
1809
1853
- Example: Create a ` Buffer ` with the ASCII alphabet, take a slice, and then modify
1854
- one byte from the original ` Buffer `
1855
-
1856
1810
``` js
1811
+ // Create a `Buffer` with the ASCII alphabet, take a slice, and modify one byte
1812
+ // from the original `Buffer`.
1813
+
1857
1814
const buf1 = Buffer .allocUnsafe (26 );
1858
1815
1859
1816
for (let i = 0 ; i < 26 ; i++ ) {
@@ -1985,8 +1942,6 @@ added: v0.9.2
1985
1942
Returns a JSON representation of ` buf ` . [ ` JSON.stringify() ` ] implicitly calls
1986
1943
this function when stringifying a ` Buffer ` instance.
1987
1944
1988
- Example:
1989
-
1990
1945
``` js
1991
1946
const buf = Buffer .from ([0x1 , 0x2 , 0x3 , 0x4 , 0x5 ]);
1992
1947
const json = JSON .stringify (buf);
@@ -2096,8 +2051,6 @@ The `length` parameter is the number of bytes to write. If `buf` did not contain
2096
2051
enough space to fit the entire string, only a partial amount of ` string ` will
2097
2052
be written. However, partially encoded characters will not be written.
2098
2053
2099
- Example:
2100
-
2101
2054
``` js
2102
2055
const buf = Buffer .allocUnsafe (256 );
2103
2056
@@ -2517,8 +2470,6 @@ In the case where a developer may need to retain a small chunk of memory from a
2517
2470
pool for an indeterminate amount of time, it may be appropriate to create an
2518
2471
un-pooled ` Buffer ` instance using ` SlowBuffer ` then copy out the relevant bits.
2519
2472
2520
- Example:
2521
-
2522
2473
``` js
2523
2474
// Need to keep around a few small chunks of memory
2524
2475
const store = [];
@@ -2553,10 +2504,8 @@ Allocates a new `Buffer` of `size` bytes. If the `size` is larger than
2553
2504
thrown. A zero-length ` Buffer ` will be created if ` size ` is 0.
2554
2505
2555
2506
The underlying memory for ` SlowBuffer ` instances is * not initialized* . The
2556
- contents of a newly created ` SlowBuffer ` are unknown and may contain
2557
- sensitive data. Use [ ` buf.fill(0) ` ] [ `buf.fill()` ] to initialize a ` SlowBuffer ` to zeroes.
2558
-
2559
- Example:
2507
+ contents of a newly created ` SlowBuffer ` are unknown and may contain sensitive
2508
+ data. Use [ ` buf.fill(0) ` ] [ `buf.fill()` ] to initialize a ` SlowBuffer ` to zeroes.
2560
2509
2561
2510
``` js
2562
2511
const { SlowBuffer } = require (' buffer' );
0 commit comments