@@ -230,9 +230,9 @@ const arr = new Uint16Array(2);
230
230
arr[0 ] = 5000 ;
231
231
arr[1 ] = 4000 ;
232
232
233
- // Copies the contents of `arr`
233
+ // Copies the contents of `arr`.
234
234
const buf1 = Buffer .from (arr);
235
- // Shares memory with `arr`
235
+ // Shares memory with `arr`.
236
236
const buf2 = Buffer .from (arr .buffer );
237
237
238
238
console .log (buf1);
@@ -322,7 +322,7 @@ changes:
322
322
Allocates a new ` Buffer ` using an ` array ` of octets.
323
323
324
324
``` js
325
- // Creates a new Buffer containing the UTF-8 bytes of the string 'buffer'
325
+ // Creates a new Buffer containing the UTF-8 bytes of the string 'buffer'.
326
326
const buf = new Buffer ([0x62 , 0x75 , 0x66 , 0x66 , 0x65 , 0x72 ]);
327
327
```
328
328
@@ -370,13 +370,13 @@ const arr = new Uint16Array(2);
370
370
arr[0 ] = 5000 ;
371
371
arr[1 ] = 4000 ;
372
372
373
- // Shares memory with `arr`
373
+ // Shares memory with `arr`.
374
374
const buf = new Buffer (arr .buffer );
375
375
376
376
console .log (buf);
377
377
// Prints: <Buffer 88 13 a0 0f>
378
378
379
- // Changing the original Uint16Array changes the Buffer also
379
+ // Changing the original Uint16Array changes the Buffer also.
380
380
arr[1 ] = 6000 ;
381
381
382
382
console .log (buf);
@@ -585,7 +585,7 @@ initialized*. The contents of the newly created `Buffer` are unknown and
585
585
const buf = Buffer .allocUnsafe (10 );
586
586
587
587
console .log (buf);
588
- // Prints: (contents may vary): <Buffer a0 8b 28 3f 01 00 00 00 50 32>
588
+ // Prints (contents may vary): <Buffer a0 8b 28 3f 01 00 00 00 50 32>
589
589
590
590
buf .fill (0 );
591
591
@@ -638,16 +638,16 @@ to create an un-pooled `Buffer` instance using `Buffer.allocUnsafeSlow()` and
638
638
then copying out the relevant bits.
639
639
640
640
``` js
641
- // Need to keep around a few small chunks of memory
641
+ // Need to keep around a few small chunks of memory.
642
642
const store = [];
643
643
644
644
socket .on (' readable' , () => {
645
645
let data;
646
646
while (null !== (data = readable .read ())) {
647
- // Allocate for retained data
647
+ // Allocate for retained data.
648
648
const sb = Buffer .allocUnsafeSlow (10 );
649
649
650
- // Copy the data into the new allocation
650
+ // Copy the data into the new allocation.
651
651
data .copy (sb, 0 , 0 , 10 );
652
652
653
653
store .push (sb);
@@ -722,7 +722,7 @@ const arr = [buf1, buf2];
722
722
723
723
console .log (arr .sort (Buffer .compare ));
724
724
// Prints: [ <Buffer 30 31 32 33>, <Buffer 31 32 33 34> ]
725
- // (This result is equal to: [buf2, buf1])
725
+ // (This result is equal to: [buf2, buf1]. )
726
726
```
727
727
728
728
### Class Method: Buffer.concat(list[ , totalLength] )
@@ -784,7 +784,7 @@ added: v5.10.0
784
784
Allocates a new ` Buffer ` using an ` array ` of octets.
785
785
786
786
``` js
787
- // Creates a new Buffer containing UTF-8 bytes of the string 'buffer'
787
+ // Creates a new Buffer containing UTF-8 bytes of the string 'buffer'.
788
788
const buf = Buffer .from ([0x62 , 0x75 , 0x66 , 0x66 , 0x65 , 0x72 ]);
789
789
```
790
790
@@ -813,13 +813,13 @@ const arr = new Uint16Array(2);
813
813
arr[0 ] = 5000 ;
814
814
arr[1 ] = 4000 ;
815
815
816
- // Shares memory with `arr`
816
+ // Shares memory with `arr`.
817
817
const buf = Buffer .from (arr .buffer );
818
818
819
819
console .log (buf);
820
820
// Prints: <Buffer 88 13 a0 0f>
821
821
822
- // Changing the original Uint16Array changes the Buffer also
822
+ // Changing the original Uint16Array changes the Buffer also.
823
823
arr[1 ] = 6000 ;
824
824
825
825
console .log (buf);
@@ -1076,7 +1076,7 @@ console.log(buf2.compare(buf3));
1076
1076
// Prints: 1
1077
1077
console .log ([buf1, buf2, buf3].sort (Buffer .compare ));
1078
1078
// Prints: [ <Buffer 41 42 43>, <Buffer 41 42 43 44>, <Buffer 42 43 44> ]
1079
- // (This result is equal to: [buf1, buf3, buf2])
1079
+ // (This result is equal to: [buf1, buf3, buf2]. )
1080
1080
```
1081
1081
1082
1082
The optional ` targetStart ` , ` targetEnd ` , ` sourceStart ` , and ` sourceEnd `
@@ -1121,11 +1121,11 @@ const buf1 = Buffer.allocUnsafe(26);
1121
1121
const buf2 = Buffer .allocUnsafe (26 ).fill (' !' );
1122
1122
1123
1123
for (let i = 0 ; i < 26 ; i++ ) {
1124
- // 97 is the decimal ASCII value for 'a'
1124
+ // 97 is the decimal ASCII value for 'a'.
1125
1125
buf1[i] = i + 97 ;
1126
1126
}
1127
1127
1128
- // Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`
1128
+ // Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`.
1129
1129
buf1 .copy (buf2, 8 , 16 , 20 );
1130
1130
1131
1131
console .log (buf2 .toString (' ascii' , 0 , 25 ));
@@ -1139,7 +1139,7 @@ console.log(buf2.toString('ascii', 0, 25));
1139
1139
const buf = Buffer .allocUnsafe (26 );
1140
1140
1141
1141
for (let i = 0 ; i < 26 ; i++ ) {
1142
- // 97 is the decimal ASCII value for 'a'
1142
+ // 97 is the decimal ASCII value for 'a'.
1143
1143
buf[i] = i + 97 ;
1144
1144
}
1145
1145
@@ -1374,13 +1374,13 @@ behavior matches [`String#indexOf()`].
1374
1374
``` js
1375
1375
const b = Buffer .from (' abcdef' );
1376
1376
1377
- // Passing a value that's a number, but not a valid byte
1378
- // Prints: 2, equivalent to searching for 99 or 'c'
1377
+ // Passing a value that's a number, but not a valid byte.
1378
+ // Prints: 2, equivalent to searching for 99 or 'c'.
1379
1379
console .log (b .indexOf (99.9 ));
1380
1380
console .log (b .indexOf (256 + 99 ));
1381
1381
1382
- // Passing a byteOffset that coerces to NaN or 0
1383
- // Prints: 1, searching the whole buffer
1382
+ // Passing a byteOffset that coerces to NaN or 0.
1383
+ // Prints: 1, searching the whole buffer.
1384
1384
console .log (b .indexOf (' b' , undefined ));
1385
1385
console .log (b .indexOf (' b' , {}));
1386
1386
console .log (b .indexOf (' b' , null ));
@@ -1474,18 +1474,18 @@ This behavior matches [`String#lastIndexOf()`].
1474
1474
``` js
1475
1475
const b = Buffer .from (' abcdef' );
1476
1476
1477
- // Passing a value that's a number, but not a valid byte
1478
- // Prints: 2, equivalent to searching for 99 or 'c'
1477
+ // Passing a value that's a number, but not a valid byte.
1478
+ // Prints: 2, equivalent to searching for 99 or 'c'.
1479
1479
console .log (b .lastIndexOf (99.9 ));
1480
1480
console .log (b .lastIndexOf (256 + 99 ));
1481
1481
1482
- // Passing a byteOffset that coerces to NaN
1483
- // Prints: 1, searching the whole buffer
1482
+ // Passing a byteOffset that coerces to NaN.
1483
+ // Prints: 1, searching the whole buffer.
1484
1484
console .log (b .lastIndexOf (' b' , undefined ));
1485
1485
console .log (b .lastIndexOf (' b' , {}));
1486
1486
1487
- // Passing a byteOffset that coerces to 0
1488
- // Prints: -1, equivalent to passing 0
1487
+ // Passing a byteOffset that coerces to 0.
1488
+ // Prints: -1, equivalent to passing 0.
1489
1489
console .log (b .lastIndexOf (' b' , null ));
1490
1490
console .log (b .lastIndexOf (' b' , []));
1491
1491
```
@@ -1571,7 +1571,7 @@ console.log(buf.readDoubleBE(0));
1571
1571
console .log (buf .readDoubleLE (0 ));
1572
1572
// Prints: 5.447603722011605e-270
1573
1573
console .log (buf .readDoubleLE (1 ));
1574
- // Throws ERR_OUT_OF_RANGE
1574
+ // Throws ERR_OUT_OF_RANGE.
1575
1575
```
1576
1576
1577
1577
### buf.readFloatBE([ offset] )
@@ -1601,7 +1601,7 @@ console.log(buf.readFloatBE(0));
1601
1601
console .log (buf .readFloatLE (0 ));
1602
1602
// Prints: 1.539989614439558e-36
1603
1603
console .log (buf .readFloatLE (1 ));
1604
- // Throws ERR_OUT_OF_RANGE
1604
+ // Throws ERR_OUT_OF_RANGE.
1605
1605
```
1606
1606
1607
1607
### buf.readInt8([ offset] )
@@ -1630,7 +1630,7 @@ console.log(buf.readInt8(0));
1630
1630
console .log (buf .readInt8 (1 ));
1631
1631
// Prints: 5
1632
1632
console .log (buf .readInt8 (2 ));
1633
- // Throws ERR_OUT_OF_RANGE
1633
+ // Throws ERR_OUT_OF_RANGE.
1634
1634
```
1635
1635
1636
1636
### buf.readInt16BE([ offset] )
@@ -1662,7 +1662,7 @@ console.log(buf.readInt16BE(0));
1662
1662
console .log (buf .readInt16LE (0 ));
1663
1663
// Prints: 1280
1664
1664
console .log (buf .readInt16LE (1 ));
1665
- // Throws ERR_OUT_OF_RANGE
1665
+ // Throws ERR_OUT_OF_RANGE.
1666
1666
```
1667
1667
1668
1668
### buf.readInt32BE([ offset] )
@@ -1694,7 +1694,7 @@ console.log(buf.readInt32BE(0));
1694
1694
console .log (buf .readInt32LE (0 ));
1695
1695
// Prints: 83886080
1696
1696
console .log (buf .readInt32LE (1 ));
1697
- // Throws ERR_OUT_OF_RANGE
1697
+ // Throws ERR_OUT_OF_RANGE.
1698
1698
```
1699
1699
1700
1700
### buf.readIntBE(offset, byteLength)
@@ -1726,9 +1726,9 @@ console.log(buf.readIntLE(0, 6).toString(16));
1726
1726
console .log (buf .readIntBE (0 , 6 ).toString (16 ));
1727
1727
// Prints: 1234567890ab
1728
1728
console .log (buf .readIntBE (1 , 6 ).toString (16 ));
1729
- // Throws ERR_OUT_OF_RANGE
1729
+ // Throws ERR_OUT_OF_RANGE.
1730
1730
console .log (buf .readIntBE (1 , 0 ).toString (16 ));
1731
- // Throws ERR_OUT_OF_RANGE
1731
+ // Throws ERR_OUT_OF_RANGE.
1732
1732
```
1733
1733
1734
1734
### buf.readUInt8([ offset] )
@@ -1755,7 +1755,7 @@ console.log(buf.readUInt8(0));
1755
1755
console .log (buf .readUInt8 (1 ));
1756
1756
// Prints: 254
1757
1757
console .log (buf .readUInt8 (2 ));
1758
- // Throws ERR_OUT_OF_RANGE
1758
+ // Throws ERR_OUT_OF_RANGE.
1759
1759
```
1760
1760
1761
1761
### buf.readUInt16BE([ offset] )
@@ -1789,7 +1789,7 @@ console.log(buf.readUInt16BE(1).toString(16));
1789
1789
console .log (buf .readUInt16LE (1 ).toString (16 ));
1790
1790
// Prints: 5634
1791
1791
console .log (buf .readUInt16LE (2 ).toString (16 ));
1792
- // Throws ERR_OUT_OF_RANGE
1792
+ // Throws ERR_OUT_OF_RANGE.
1793
1793
```
1794
1794
1795
1795
### buf.readUInt32BE([ offset] )
@@ -1819,7 +1819,7 @@ console.log(buf.readUInt32BE(0).toString(16));
1819
1819
console .log (buf .readUInt32LE (0 ).toString (16 ));
1820
1820
// Prints: 78563412
1821
1821
console .log (buf .readUInt32LE (1 ).toString (16 ));
1822
- // Throws ERR_OUT_OF_RANGE
1822
+ // Throws ERR_OUT_OF_RANGE.
1823
1823
```
1824
1824
1825
1825
### buf.readUIntBE(offset, byteLength)
@@ -1851,7 +1851,7 @@ console.log(buf.readUIntBE(0, 6).toString(16));
1851
1851
console .log (buf .readUIntLE (0 , 6 ).toString (16 ));
1852
1852
// Prints: ab9078563412
1853
1853
console .log (buf .readUIntBE (1 , 6 ).toString (16 ));
1854
- // Throws ERR_OUT_OF_RANGE
1854
+ // Throws ERR_OUT_OF_RANGE.
1855
1855
```
1856
1856
1857
1857
### buf.slice([ start[ , end]] )
@@ -1889,7 +1889,7 @@ because the allocated memory of the two objects overlap.
1889
1889
const buf1 = Buffer .allocUnsafe (26 );
1890
1890
1891
1891
for (let i = 0 ; i < 26 ; i++ ) {
1892
- // 97 is the decimal ASCII value for 'a'
1892
+ // 97 is the decimal ASCII value for 'a'.
1893
1893
buf1[i] = i + 97 ;
1894
1894
}
1895
1895
@@ -1912,15 +1912,15 @@ const buf = Buffer.from('buffer');
1912
1912
1913
1913
console .log (buf .slice (- 6 , - 1 ).toString ());
1914
1914
// Prints: buffe
1915
- // (Equivalent to buf.slice(0, 5))
1915
+ // (Equivalent to buf.slice(0, 5). )
1916
1916
1917
1917
console .log (buf .slice (- 6 , - 2 ).toString ());
1918
1918
// Prints: buff
1919
- // (Equivalent to buf.slice(0, 4))
1919
+ // (Equivalent to buf.slice(0, 4). )
1920
1920
1921
1921
console .log (buf .slice (- 5 , - 2 ).toString ());
1922
1922
// Prints: uff
1923
- // (Equivalent to buf.slice(1, 4))
1923
+ // (Equivalent to buf.slice(1, 4). )
1924
1924
```
1925
1925
1926
1926
### buf.swap16()
@@ -1948,7 +1948,7 @@ console.log(buf1);
1948
1948
const buf2 = Buffer .from ([0x1 , 0x2 , 0x3 ]);
1949
1949
1950
1950
buf2 .swap16 ();
1951
- // Throws ERR_INVALID_BUFFER_SIZE
1951
+ // Throws ERR_INVALID_BUFFER_SIZE.
1952
1952
```
1953
1953
1954
1954
One convenient use of ` buf.swap16() ` is to perform a fast in-place conversion
@@ -1984,7 +1984,7 @@ console.log(buf1);
1984
1984
const buf2 = Buffer .from ([0x1 , 0x2 , 0x3 ]);
1985
1985
1986
1986
buf2 .swap32 ();
1987
- // Throws ERR_INVALID_BUFFER_SIZE
1987
+ // Throws ERR_INVALID_BUFFER_SIZE.
1988
1988
```
1989
1989
1990
1990
### buf.swap64()
@@ -2011,7 +2011,7 @@ console.log(buf1);
2011
2011
const buf2 = Buffer .from ([0x1 , 0x2 , 0x3 ]);
2012
2012
2013
2013
buf2 .swap64 ();
2014
- // Throws ERR_INVALID_BUFFER_SIZE
2014
+ // Throws ERR_INVALID_BUFFER_SIZE.
2015
2015
```
2016
2016
2017
2017
Note that JavaScript cannot encode 64-bit integers. This method is intended
@@ -2065,7 +2065,7 @@ as [`buffer.constants.MAX_STRING_LENGTH`][].
2065
2065
const buf1 = Buffer .allocUnsafe (26 );
2066
2066
2067
2067
for (let i = 0 ; i < 26 ; i++ ) {
2068
- // 97 is the decimal ASCII value for 'a'
2068
+ // 97 is the decimal ASCII value for 'a'.
2069
2069
buf1[i] = i + 97 ;
2070
2070
}
2071
2071
@@ -2573,16 +2573,16 @@ pool for an indeterminate amount of time, it may be appropriate to create an
2573
2573
un-pooled ` Buffer ` instance using ` SlowBuffer ` then copy out the relevant bits.
2574
2574
2575
2575
``` js
2576
- // Need to keep around a few small chunks of memory
2576
+ // Need to keep around a few small chunks of memory.
2577
2577
const store = [];
2578
2578
2579
2579
socket .on (' readable' , () => {
2580
2580
let data;
2581
2581
while (null !== (data = readable .read ())) {
2582
- // Allocate for retained data
2582
+ // Allocate for retained data.
2583
2583
const sb = SlowBuffer (10 );
2584
2584
2585
- // Copy the data into the new allocation
2585
+ // Copy the data into the new allocation.
2586
2586
data .copy (sb, 0 , 0 , 10 );
2587
2587
2588
2588
store .push (sb);
0 commit comments