11# Buffer
22
33<!-- introduced_in=v0.1.90-->
4- <!-- lint disable maximum-line-length-->
54
65> Stability: 2 - Stable
76
87Prior to the introduction of [ ` TypedArray ` ] , the JavaScript language had no
98mechanism for reading or manipulating streams of binary data. The ` Buffer ` class
10- was introduced as part of the Node.js API to make it possible to interact with
11- octet streams in the context of things like TCP streams and file system
12- operations.
9+ was introduced as part of the Node.js API to enable interaction with octet
10+ streams in TCP streams, file system operations, and other contexts.
1311
1412With [ ` TypedArray ` ] now available, the ` Buffer ` class implements the
1513[ ` Uint8Array ` ] API in a manner that is more optimized and suitable for Node.js.
@@ -81,7 +79,7 @@ to one of these new APIs.*
8179
8280* [ ` Buffer.from(array) ` ] returns a new ` Buffer ` that * contains a copy* of the
8381 provided octets.
84- * [ ` Buffer.from(arrayBuffer[, byteOffset [, length]]) ` ] [ `Buffer.from(arrayBuffer )` ]
82+ * [ ` Buffer.from(arrayBuffer[, byteOffset[, length]]) ` ] [ `Buffer.from(arrayBuf )` ]
8583 returns a new ` Buffer ` that * shares the same allocated memory* as the given
8684 [ ` ArrayBuffer ` ] .
8785* [ ` Buffer.from(buffer) ` ] returns a new ` Buffer ` that * contains a copy* of the
@@ -194,8 +192,8 @@ Modern Web browsers follow the [WHATWG Encoding Standard][] which aliases
194192both ` 'latin1' ` and ` 'ISO-8859-1' ` to ` 'win-1252' ` . This means that while doing
195193something like ` http.get() ` , if the returned charset is one of those listed in
196194the WHATWG specification it is possible that the server actually returned
197- win-1252-encoded data, and using ` 'latin1' ` encoding may incorrectly decode the
198- characters.
195+ ` ' win-1252' ` -encoded data, and using ` 'latin1' ` encoding may incorrectly decode
196+ the characters.
199197
200198## Buffers and TypedArray
201199<!-- YAML
@@ -273,7 +271,7 @@ function:
273271
274272* [ ` Buffer.from(array) ` ]
275273* [ ` Buffer.from(buffer) ` ]
276- * [ ` Buffer.from(arrayBuffer[, byteOffset [, length]]) ` ] [ `Buffer.from(arrayBuffer )` ]
274+ * [ ` Buffer.from(arrayBuffer[, byteOffset[, length]]) ` ] [ `Buffer.from(arrayBuf )` ]
277275* [ ` Buffer.from(string[, encoding]) ` ] [ `Buffer.from(string)` ]
278276
279277## Buffers and iteration
@@ -327,7 +325,7 @@ Allocates a new `Buffer` using an `array` of octets.
327325const buf = new Buffer ([0x62 , 0x75 , 0x66 , 0x66 , 0x65 , 0x72 ]);
328326```
329327
330- ### new Buffer(arrayBuffer[ , byteOffset [ , length]] )
328+ ### new Buffer(arrayBuffer[ , byteOffset[ , length]] )
331329<!-- YAML
332330added: v3.0.0
333331deprecated: v6.0.0
@@ -348,7 +346,7 @@ changes:
348346-->
349347
350348> Stability: 0 - Deprecated: Use
351- > [ ` Buffer.from(arrayBuffer[, byteOffset [, length]]) ` ] [ `Buffer.from(arrayBuffer )` ]
349+ > [ ` Buffer.from(arrayBuffer[, byteOffset[, length]]) ` ] [ `Buffer.from(arrayBuf )` ]
352350> instead.
353351
354352* ` arrayBuffer ` {ArrayBuffer|SharedArrayBuffer} An [ ` ArrayBuffer ` ] ,
@@ -402,7 +400,8 @@ changes:
402400
403401> Stability: 0 - Deprecated: Use [ ` Buffer.from(buffer) ` ] instead.
404402
405- * ` buffer ` {Buffer} An existing ` Buffer ` to copy data from.
403+ * ` buffer ` {Buffer|Uint8Array} An existing ` Buffer ` or [ ` Uint8Array ` ] from which
404+ to copy data.
406405
407406Copies the passed ` buffer ` data onto a new ` Buffer ` instance.
408407
@@ -842,7 +841,8 @@ A `TypeError` will be thrown if `arrayBuffer` is not an [`ArrayBuffer`] or a
842841added: v5.10.0
843842-->
844843
845- * ` buffer ` {Buffer} An existing ` Buffer ` to copy data from.
844+ * ` buffer ` {Buffer|Uint8Array} An existing ` Buffer ` or [ ` Uint8Array ` ] from which
845+ to copy data.
846846
847847Copies the passed ` buffer ` data onto a new ` Buffer ` instance.
848848
@@ -1887,7 +1887,7 @@ added: v5.10.0
18871887* Returns: {Buffer} A reference to ` buf ` .
18881888
18891889Interprets ` buf ` as an array of unsigned 16-bit integers and swaps the
1890- byte- order * in-place* . Throws [ ` ERR_INVALID_BUFFER_SIZE ` ] if [ ` buf.length ` ] is
1890+ byte order * in-place* . Throws [ ` ERR_INVALID_BUFFER_SIZE ` ] if [ ` buf.length ` ] is
18911891not a multiple of 2.
18921892
18931893``` js
@@ -1915,7 +1915,7 @@ added: v5.10.0
19151915* Returns: {Buffer} A reference to ` buf ` .
19161916
19171917Interprets ` buf ` as an array of unsigned 32-bit integers and swaps the
1918- byte- order * in-place* . Throws [ ` ERR_INVALID_BUFFER_SIZE ` ] if [ ` buf.length ` ] is
1918+ byte order * in-place* . Throws [ ` ERR_INVALID_BUFFER_SIZE ` ] if [ ` buf.length ` ] is
19191919not a multiple of 4.
19201920
19211921``` js
@@ -1942,9 +1942,8 @@ added: v6.3.0
19421942
19431943* Returns: {Buffer} A reference to ` buf ` .
19441944
1945- Interprets ` buf ` as an array of 64-bit numbers and swaps the byte-order
1946- * in-place* . Throws [ ` ERR_INVALID_BUFFER_SIZE ` ] if [ ` buf.length ` ] is not a
1947- multiple of 8.
1945+ Interprets ` buf ` as an array of 64-bit numbers and swaps byte order * in-place* .
1946+ Throws [ ` ERR_INVALID_BUFFER_SIZE ` ] if [ ` buf.length ` ] is not a multiple of 8.
19481947
19491948``` js
19501949const buf1 = Buffer .from ([0x1 , 0x2 , 0x3 , 0x4 , 0x5 , 0x6 , 0x7 , 0x8 ]);
@@ -2074,19 +2073,21 @@ for (const value of buf) {
20742073added: v0.1.90
20752074-->
20762075
2077- * ` string ` {string} String to be written to ` buf ` .
2078- * ` offset ` {integer} Number of bytes to skip before starting to write ` string ` . ** Default:** ` 0 ` .
2079- * ` length ` {integer} Number of bytes to write. ** Default:** ` buf.length - offset ` .
2076+ * ` string ` {string} String to write to ` buf ` .
2077+ * ` offset ` {integer} Number of bytes to skip before starting to write ` string ` .
2078+ ** Default:** ` 0 ` .
2079+ * ` length ` {integer} Number of bytes to write. ** Default:**
2080+ ` buf.length - offset ` .
20802081* ` encoding ` {string} The character encoding of ` string ` . ** Default:** ` 'utf8' ` .
20812082* Returns: {integer} Number of bytes written.
20822083
2083- Writes ` string ` to ` buf ` at ` offset ` according to the character encoding in ` encoding ` .
2084- The ` length ` parameter is the number of bytes to write. If ` buf ` did not contain
2085- enough space to fit the entire string, only a partial amount of ` string ` will
2086- be written. However, partially encoded characters will not be written.
2084+ Writes ` string ` to ` buf ` at ` offset ` according to the character encoding in
2085+ ` encoding ` . The ` length ` parameter is the number of bytes to write. If ` buf ` did
2086+ not contain enough space to fit the entire string, only part of ` string ` will be
2087+ written. However, partially encoded characters will not be written.
20872088
20882089``` js
2089- const buf = Buffer .allocUnsafe (256 );
2090+ const buf = Buffer .alloc (256 );
20902091
20912092const len = buf .write (' \u00bd + \u00bc = \u00be ' , 0 );
20922093
@@ -2213,8 +2214,8 @@ changes:
22132214
22142215Writes ` value ` to ` buf ` at the specified ` offset ` with specified endian
22152216format (` writeInt16BE() ` writes big endian, ` writeInt16LE() ` writes little
2216- endian). ` value ` * should* be a valid signed 16-bit integer. Behavior is undefined
2217- when ` value ` is anything other than a signed 16-bit integer.
2217+ endian). ` value ` * should* be a valid signed 16-bit integer. Behavior is
2218+ undefined when ` value ` is anything other than a signed 16-bit integer.
22182219
22192220` value ` is interpreted and written as a two's complement signed integer.
22202221
@@ -2246,8 +2247,8 @@ changes:
22462247
22472248Writes ` value ` to ` buf ` at the specified ` offset ` with specified endian
22482249format (` writeInt32BE() ` writes big endian, ` writeInt32LE() ` writes little
2249- endian). ` value ` * should* be a valid signed 32-bit integer. Behavior is undefined
2250- when ` value ` is anything other than a signed 32-bit integer.
2250+ endian). ` value ` * should* be a valid signed 32-bit integer. Behavior is
2251+ undefined when ` value ` is anything other than a signed 32-bit integer.
22512252
22522253` value ` is interpreted and written as a two's complement signed integer.
22532254
@@ -2605,7 +2606,7 @@ This value may depend on the JS engine that is being used.
26052606[ `Buffer.allocUnsafe()` ] : #buffer_class_method_buffer_allocunsafe_size
26062607[ `Buffer.allocUnsafeSlow()` ] : #buffer_class_method_buffer_allocunsafeslow_size
26072608[ `Buffer.from(array)` ] : #buffer_class_method_buffer_from_array
2608- [ `Buffer.from(arrayBuffer )` ] : #buffer_class_method_buffer_from_arraybuffer_byteoffset_length
2609+ [ `Buffer.from(arrayBuf )` ] : #buffer_class_method_buffer_from_arraybuffer_byteoffset_length
26092610[ `Buffer.from(buffer)` ] : #buffer_class_method_buffer_from_buffer
26102611[ `Buffer.from(string)` ] : #buffer_class_method_buffer_from_string_encoding
26112612[ `Buffer.poolSize` ] : #buffer_class_property_buffer_poolsize
0 commit comments