@@ -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
@@ -514,8 +496,6 @@ changes:
514
496
Allocates a new ` Buffer ` of ` size ` bytes. If ` fill ` is ` undefined ` , the
515
497
` Buffer ` will be * zero-filled* .
516
498
517
- Example:
518
-
519
499
``` js
520
500
const buf = Buffer .alloc (5 );
521
501
@@ -530,8 +510,6 @@ thrown. A zero-length `Buffer` will be created if `size` is 0.
530
510
If ` fill ` is specified, the allocated ` Buffer ` will be initialized by calling
531
511
[ ` buf.fill(fill) ` ] [ `buf.fill()` ] .
532
512
533
- Example:
534
-
535
513
``` js
536
514
const buf = Buffer .alloc (5 , ' a' );
537
515
@@ -542,8 +520,6 @@ console.log(buf);
542
520
If both ` fill ` and ` encoding ` are specified, the allocated ` Buffer ` will be
543
521
initialized by calling [ ` buf.fill(fill, encoding) ` ] [ `buf.fill()` ] .
544
522
545
- Example:
546
-
547
523
``` js
548
524
const buf = Buffer .alloc (11 , ' aGVsbG8gd29ybGQ=' , ' base64' );
549
525
@@ -577,8 +553,6 @@ initialized*. The contents of the newly created `Buffer` are unknown and
577
553
* may contain sensitive data* . Use [ ` Buffer.alloc() ` ] instead to initialize
578
554
` Buffer ` instances to zeroes.
579
555
580
- Example:
581
-
582
556
``` js
583
557
const buf = Buffer .allocUnsafe (10 );
584
558
@@ -635,8 +609,6 @@ memory from a pool for an indeterminate amount of time, it may be appropriate
635
609
to create an un-pooled ` Buffer ` instance using ` Buffer.allocUnsafeSlow() ` then
636
610
copy out the relevant bits.
637
611
638
- Example:
639
-
640
612
``` js
641
613
// Need to keep around a few small chunks of memory
642
614
const store = [];
@@ -686,8 +658,6 @@ For `'base64'` and `'hex'`, this function assumes valid input. For strings that
686
658
contain non-Base64/Hex-encoded data (e.g. whitespace), the return value might be
687
659
greater than the length of a ` Buffer ` created from the string.
688
660
689
- Example:
690
-
691
661
``` js
692
662
const str = ' \u00bd + \u00bc = \u00be ' ;
693
663
@@ -716,8 +686,6 @@ Compares `buf1` to `buf2` typically for the purpose of sorting arrays of
716
686
` Buffer ` instances. This is equivalent to calling
717
687
[ ` buf1.compare(buf2) ` ] [ `buf.compare()` ] .
718
688
719
- Example:
720
-
721
689
``` js
722
690
const buf1 = Buffer .from (' 1234' );
723
691
const buf2 = Buffer .from (' 0123' );
@@ -757,9 +725,9 @@ If `totalLength` is provided, it is coerced to an unsigned integer. If the
757
725
combined length of the ` Buffer ` s in ` list ` exceeds ` totalLength ` , the result is
758
726
truncated to ` totalLength ` .
759
727
760
- Example: Create a single ` Buffer ` from a list of three ` Buffer ` instances
761
-
762
728
``` js
729
+ // Create a single `Buffer` from a list of three `Buffer` instances.
730
+
763
731
const buf1 = Buffer .alloc (10 );
764
732
const buf2 = Buffer .alloc (14 );
765
733
const buf3 = Buffer .alloc (18 );
@@ -785,8 +753,6 @@ added: v5.10.0
785
753
786
754
Allocates a new ` Buffer ` using an ` array ` of octets.
787
755
788
- Example:
789
-
790
756
``` js
791
757
// Creates a new Buffer containing UTF-8 bytes of the string 'buffer'
792
758
const buf = Buffer .from ([0x62 , 0x75 , 0x66 , 0x66 , 0x65 , 0x72 ]);
@@ -810,8 +776,6 @@ memory. For example, when passed a reference to the `.buffer` property of a
810
776
[ ` TypedArray ` ] instance, the newly created ` Buffer ` will share the same
811
777
allocated memory as the [ ` TypedArray ` ] .
812
778
813
- Example:
814
-
815
779
``` js
816
780
const arr = new Uint16Array (2 );
817
781
@@ -834,8 +798,6 @@ console.log(buf);
834
798
The optional ` byteOffset ` and ` length ` arguments specify a memory range within
835
799
the ` arrayBuffer ` that will be shared by the ` Buffer ` .
836
800
837
- Example:
838
-
839
801
``` js
840
802
const ab = new ArrayBuffer (10 );
841
803
const buf = Buffer .from (ab, 0 , 2 );
@@ -856,8 +818,6 @@ added: v5.10.0
856
818
857
819
Copies the passed ` buffer ` data onto a new ` Buffer ` instance.
858
820
859
- Example:
860
-
861
821
``` js
862
822
const buf1 = Buffer .from (' buffer' );
863
823
const buf2 = Buffer .from (buf1);
@@ -976,9 +936,9 @@ This operator is inherited from `Uint8Array`, so its behavior on out-of-bounds
976
936
access is the same as ` UInt8Array ` - that is, getting returns ` undefined ` and
977
937
setting does nothing.
978
938
979
- Example: Copy an ASCII string into a ` Buffer ` , one byte at a time
980
-
981
939
``` js
940
+ // Copy an ASCII string into a `Buffer` one byte at a time.
941
+
982
942
const str = ' Node.js' ;
983
943
const buf = Buffer .allocUnsafe (str .length );
984
944
@@ -1090,10 +1050,8 @@ added: v0.1.90
1090
1050
Copies data from a region of ` buf ` to a region in ` target ` even if the ` target `
1091
1051
memory region overlaps with ` buf ` .
1092
1052
1093
- Example: Create two ` Buffer ` instances, ` buf1 ` and ` buf2 ` , and copy ` buf1 ` from
1094
- byte 16 through byte 19 into ` buf2 ` , starting at the 8th byte in ` buf2 `
1095
-
1096
1053
``` js
1054
+ // Create two `Buffer` instances.
1097
1055
const buf1 = Buffer .allocUnsafe (26 );
1098
1056
const buf2 = Buffer .allocUnsafe (26 ).fill (' !' );
1099
1057
@@ -1102,16 +1060,17 @@ for (let i = 0; i < 26; i++) {
1102
1060
buf1[i] = i + 97 ;
1103
1061
}
1104
1062
1063
+ // Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`
1105
1064
buf1 .copy (buf2, 8 , 16 , 20 );
1106
1065
1107
1066
console .log (buf2 .toString (' ascii' , 0 , 25 ));
1108
1067
// Prints: !!!!!!!!qrst!!!!!!!!!!!!!
1109
1068
```
1110
1069
1111
- Example: Create a single ` Buffer ` and copy data from one region to an
1112
- overlapping region within the same ` Buffer `
1113
-
1114
1070
``` js
1071
+ // Create a `Buffer` and copy data from one region to an overlapping region
1072
+ // within the same `Buffer`.
1073
+
1115
1074
const buf = Buffer .allocUnsafe (26 );
1116
1075
1117
1076
for (let i = 0 ; i < 26 ; i++ ) {
@@ -1135,9 +1094,9 @@ added: v1.1.0
1135
1094
Creates and returns an [ iterator] of ` [index, byte] ` pairs from the contents of
1136
1095
` buf ` .
1137
1096
1138
- Example: Log the entire contents of a ` Buffer `
1139
-
1140
1097
``` js
1098
+ // Log the entire contents of a `Buffer`.
1099
+
1141
1100
const buf = Buffer .from (' buffer' );
1142
1101
1143
1102
for (const pair of buf .entries ()) {
@@ -1198,9 +1157,9 @@ Fills `buf` with the specified `value`. If the `offset` and `end` are not given,
1198
1157
the entire ` buf ` will be filled. This is meant to be a small simplification to
1199
1158
allow the creation and filling of a ` Buffer ` to be done on a single line.
1200
1159
1201
- Example: Fill a ` Buffer ` with the ASCII character ` 'h' `
1202
-
1203
1160
``` js
1161
+ // Fill a `Buffer` with the ASCII character 'h'.
1162
+
1204
1163
const b = Buffer .allocUnsafe (50 ).fill (' h' );
1205
1164
1206
1165
console .log (b .toString ());
@@ -1212,9 +1171,9 @@ console.log(b.toString());
1212
1171
If the final write of a ` fill() ` operation falls on a multi-byte character,
1213
1172
then only the first bytes of that character that fit into ` buf ` are written.
1214
1173
1215
- Example: Fill a ` Buffer ` with a two-byte character
1216
-
1217
1174
``` js
1175
+ // Fill a `Buffer` with a two-byte character.
1176
+
1218
1177
console .log (Buffer .allocUnsafe (3 ).fill (' \u0222 ' ));
1219
1178
// Prints: <Buffer c8 a2 c8>
1220
1179
```
@@ -1355,8 +1314,6 @@ added: v1.1.0
1355
1314
1356
1315
Creates and returns an [ iterator] of ` buf ` keys (indices).
1357
1316
1358
- Example:
1359
-
1360
1317
``` js
1361
1318
const buf = Buffer .from (' buffer' );
1362
1319
@@ -1457,9 +1414,9 @@ added: v0.1.90
1457
1414
Returns the amount of memory allocated for ` buf ` in bytes. Note that this
1458
1415
does not necessarily reflect the amount of "usable" data within ` buf ` .
1459
1416
1460
- Example: Create a ` Buffer ` and write a shorter ASCII string to it
1461
-
1462
1417
``` js
1418
+ // Create a `Buffer` and write a shorter ASCII string to it.
1419
+
1463
1420
const buf = Buffer .alloc (1234 );
1464
1421
1465
1422
console .log (buf .length );
@@ -1819,10 +1776,10 @@ that of `end` equal to [`buf.length`].
1819
1776
Modifying the new ` Buffer ` slice will modify the memory in the original ` Buffer `
1820
1777
because the allocated memory of the two objects overlap.
1821
1778
1822
- Example: Create a ` Buffer ` with the ASCII alphabet, take a slice, and then modify
1823
- one byte from the original ` Buffer `
1824
-
1825
1779
``` js
1780
+ // Create a `Buffer` with the ASCII alphabet, take a slice, and modify one byte
1781
+ // from the original `Buffer`.
1782
+
1826
1783
const buf1 = Buffer .allocUnsafe (26 );
1827
1784
1828
1785
for (let i = 0 ; i < 26 ; i++ ) {
@@ -1954,8 +1911,6 @@ added: v0.9.2
1954
1911
Returns a JSON representation of ` buf ` . [ ` JSON.stringify() ` ] implicitly calls
1955
1912
this function when stringifying a ` Buffer ` instance.
1956
1913
1957
- Example:
1958
-
1959
1914
``` js
1960
1915
const buf = Buffer .from ([0x1 , 0x2 , 0x3 , 0x4 , 0x5 ]);
1961
1916
const json = JSON .stringify (buf);
@@ -2065,8 +2020,6 @@ The `length` parameter is the number of bytes to write. If `buf` did not contain
2065
2020
enough space to fit the entire string, only a partial amount of ` string ` will
2066
2021
be written. However, partially encoded characters will not be written.
2067
2022
2068
- Example:
2069
-
2070
2023
``` js
2071
2024
const buf = Buffer .allocUnsafe (256 );
2072
2025
@@ -2477,8 +2430,6 @@ In the case where a developer may need to retain a small chunk of memory from a
2477
2430
pool for an indeterminate amount of time, it may be appropriate to create an
2478
2431
un-pooled ` Buffer ` instance using ` SlowBuffer ` then copy out the relevant bits.
2479
2432
2480
- Example:
2481
-
2482
2433
``` js
2483
2434
// Need to keep around a few small chunks of memory
2484
2435
const store = [];
@@ -2513,10 +2464,8 @@ Allocates a new `Buffer` of `size` bytes. If the `size` is larger than
2513
2464
thrown. A zero-length ` Buffer ` will be created if ` size ` is 0.
2514
2465
2515
2466
The underlying memory for ` SlowBuffer ` instances is * not initialized* . The
2516
- contents of a newly created ` SlowBuffer ` are unknown and may contain
2517
- sensitive data. Use [ ` buf.fill(0) ` ] [ `buf.fill()` ] to initialize a ` SlowBuffer ` to zeroes.
2518
-
2519
- Example:
2467
+ contents of a newly created ` SlowBuffer ` are unknown and may contain sensitive
2468
+ data. Use [ ` buf.fill(0) ` ] [ `buf.fill()` ] to initialize a ` SlowBuffer ` to zeroes.
2520
2469
2521
2470
``` js
2522
2471
const { SlowBuffer } = require (' buffer' );
0 commit comments