From ff82acb95ab85c2e9dccc720481826c5ad3ac494 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Mon, 12 Feb 2018 23:24:54 +0100 Subject: [PATCH] doc: update buffer examples Move the print statements below a console.log call. Backport-PR-URL: https://github.com/nodejs/node/pull/19244 PR-URL: https://github.com/nodejs/node/pull/18758 Reviewed-By: Benjamin Gruenbaum Reviewed-By: James M Snell Reviewed-By: Vse Mozhet Byt --- doc/api/buffer.md | 391 +++++++++++++++++++--------------------------- 1 file changed, 161 insertions(+), 230 deletions(-) diff --git a/doc/api/buffer.md b/doc/api/buffer.md index 8925f6d895572e..da46fa7c2e4341 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -161,11 +161,10 @@ Example: ```js const buf = Buffer.from('hello world', 'ascii'); -// Prints: 68656c6c6f20776f726c64 console.log(buf.toString('hex')); - -// Prints: aGVsbG8gd29ybGQ= +// Prints: 68656c6c6f20776f726c64 console.log(buf.toString('base64')); +// Prints: aGVsbG8gd29ybGQ= ``` The character encodings currently supported by Node.js include: @@ -239,23 +238,20 @@ arr[1] = 4000; // Copies the contents of `arr` const buf1 = Buffer.from(arr); - // Shares memory with `arr` const buf2 = Buffer.from(arr.buffer); -// Prints: console.log(buf1); - -// Prints: +// Prints: console.log(buf2); +// Prints: arr[1] = 6000; -// Prints: console.log(buf1); - -// Prints: +// Prints: console.log(buf2); +// Prints: ``` Note that when creating a `Buffer` using a [`TypedArray`]'s `.buffer`, it is @@ -268,8 +264,8 @@ Example: const arr = new Uint16Array(20); const buf = Buffer.from(arr.buffer, 0, 16); -// Prints: 16 console.log(buf.length); +// Prints: 16 ``` The `Buffer.from()` and [`TypedArray.from()`] have different signatures and @@ -384,14 +380,14 @@ arr[1] = 4000; // Shares memory with `arr` const buf = new Buffer(arr.buffer); -// Prints: console.log(buf); +// Prints: // Changing the original Uint16Array changes the Buffer also arr[1] = 6000; -// Prints: console.log(buf); +// Prints: ``` ### new Buffer(buffer) @@ -420,11 +416,10 @@ const buf2 = new Buffer(buf1); buf1[0] = 0x61; -// Prints: auffer console.log(buf1.toString()); - -// Prints: buffer +// Prints: auffer console.log(buf2.toString()); +// Prints: buffer ``` ### new Buffer(size) @@ -462,8 +457,8 @@ Example: ```js const buf = new Buffer(10); -// Prints: console.log(buf); +// Prints: ``` ### new Buffer(string[, encoding]) @@ -489,17 +484,14 @@ provided, the `encoding` parameter identifies the character encoding of `string` ```js const buf1 = new Buffer('this is a tést'); - -// Prints: this is a tést -console.log(buf1.toString()); - -// Prints: this is a tC)st -console.log(buf1.toString('ascii')); - const buf2 = new Buffer('7468697320697320612074c3a97374', 'hex'); +console.log(buf1.toString()); // Prints: this is a tést console.log(buf2.toString()); +// Prints: this is a tést +console.log(buf1.toString('ascii')); +// Prints: this is a tC)st ``` ### Class Method: Buffer.alloc(size[, fill[, encoding]]) @@ -526,8 +518,8 @@ Example: ```js const buf = Buffer.alloc(5); -// Prints: console.log(buf); +// Prints: ``` Allocates a new `Buffer` of `size` bytes. If the `size` is larger than @@ -542,8 +534,8 @@ Example: ```js const buf = Buffer.alloc(5, 'a'); -// Prints: console.log(buf); +// Prints: ``` If both `fill` and `encoding` are specified, the allocated `Buffer` will be @@ -554,8 +546,8 @@ Example: ```js const buf = Buffer.alloc(11, 'aGVsbG8gd29ybGQ=', 'base64'); -// Prints: console.log(buf); +// Prints: ``` Calling [`Buffer.alloc()`] can be significantly slower than the alternative @@ -589,13 +581,13 @@ Example: ```js const buf = Buffer.allocUnsafe(10); -// Prints: (contents may vary): console.log(buf); +// Prints: (contents may vary): buf.fill(0); -// Prints: console.log(buf); +// Prints: ``` A `TypeError` will be thrown if `size` is not a number. @@ -698,9 +690,9 @@ Example: ```js const str = '\u00bd + \u00bc = \u00be'; -// Prints: ½ + ¼ = ¾: 9 characters, 12 bytes console.log(`${str}: ${str.length} characters, ` + `${Buffer.byteLength(str, 'utf8')} bytes`); +// Prints: ½ + ¼ = ¾: 9 characters, 12 bytes ``` When `string` is a `Buffer`/[`DataView`]/[`TypedArray`]/[`ArrayBuffer`]/ @@ -730,9 +722,9 @@ const buf1 = Buffer.from('1234'); const buf2 = Buffer.from('0123'); const arr = [buf1, buf2]; +console.log(arr.sort(Buffer.compare)); // Prints: [ , ] // (This result is equal to: [buf2, buf1]) -console.log(arr.sort(Buffer.compare)); ``` ### Class Method: Buffer.concat(list[, totalLength]) @@ -772,16 +764,15 @@ const buf2 = Buffer.alloc(14); const buf3 = Buffer.alloc(18); const totalLength = buf1.length + buf2.length + buf3.length; -// Prints: 42 console.log(totalLength); +// Prints: 42 const bufA = Buffer.concat([buf1, buf2, buf3], totalLength); -// Prints: console.log(bufA); - -// Prints: 42 +// Prints: console.log(bufA.length); +// Prints: 42 ``` ### Class Method: Buffer.from(array) @@ -829,14 +820,14 @@ arr[1] = 4000; // Shares memory with `arr` const buf = Buffer.from(arr.buffer); -// Prints: console.log(buf); +// Prints: // Changing the original Uint16Array changes the Buffer also arr[1] = 6000; -// Prints: console.log(buf); +// Prints: ``` The optional `byteOffset` and `length` arguments specify a memory range within @@ -848,8 +839,8 @@ Example: const ab = new ArrayBuffer(10); const buf = Buffer.from(ab, 0, 2); -// Prints: 2 console.log(buf.length); +// Prints: 2 ``` A `TypeError` will be thrown if `arrayBuffer` is not an [`ArrayBuffer`] or a @@ -872,11 +863,10 @@ const buf2 = Buffer.from(buf1); buf1[0] = 0x61; -// Prints: auffer console.log(buf1.toString()); - -// Prints: buffer +// Prints: auffer console.log(buf2.toString()); +// Prints: buffer ``` A `TypeError` will be thrown if `buffer` is not a `Buffer`. @@ -894,17 +884,14 @@ provided, the `encoding` parameter identifies the character encoding of `string` ```js const buf1 = Buffer.from('this is a tést'); - -// Prints: this is a tést -console.log(buf1.toString()); - -// Prints: this is a tC)st -console.log(buf1.toString('ascii')); - const buf2 = Buffer.from('7468697320697320612074c3a97374', 'hex'); +console.log(buf1.toString()); // Prints: this is a tést console.log(buf2.toString()); +// Prints: this is a tést +console.log(buf1.toString('ascii')); +// Prints: this is a tC)st ``` A `TypeError` will be thrown if `string` is not a string. @@ -926,7 +913,7 @@ For objects whose `valueOf()` function returns a value not strictly equal to ```js const buf = Buffer.from(new String('this is a test')); -// +// Prints: ``` For objects that support `Symbol.toPrimitive`, returns @@ -940,7 +927,7 @@ class Foo { } const buf = Buffer.from(new Foo(), 'utf8'); -// +// Prints: ``` ### Class Method: Buffer.isBuffer(obj) @@ -998,8 +985,8 @@ for (let i = 0; i < str.length; i++) { buf[i] = str.charCodeAt(i); } -// Prints: Node.js console.log(buf.toString('ascii')); +// Prints: Node.js ``` ### buf.buffer @@ -1051,24 +1038,19 @@ const buf1 = Buffer.from('ABC'); const buf2 = Buffer.from('BCD'); const buf3 = Buffer.from('ABCD'); -// Prints: 0 console.log(buf1.compare(buf1)); - -// Prints: -1 +// Prints: 0 console.log(buf1.compare(buf2)); - // Prints: -1 console.log(buf1.compare(buf3)); - -// Prints: 1 +// Prints: -1 console.log(buf2.compare(buf1)); - // Prints: 1 console.log(buf2.compare(buf3)); - +// Prints: 1 +console.log([buf1, buf2, buf3].sort(Buffer.compare)); // Prints: [ , , ] // (This result is equal to: [buf1, buf3, buf2]) -console.log([buf1, buf2, buf3].sort(Buffer.compare)); ``` The optional `targetStart`, `targetEnd`, `sourceStart`, and `sourceEnd` @@ -1079,14 +1061,12 @@ and `buf` respectively. const buf1 = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9]); const buf2 = Buffer.from([5, 6, 7, 8, 9, 1, 2, 3, 4]); -// Prints: 0 console.log(buf1.compare(buf2, 5, 9, 0, 4)); - -// Prints: -1 +// Prints: 0 console.log(buf1.compare(buf2, 0, 6, 4)); - -// Prints: 1 +// Prints: -1 console.log(buf1.compare(buf2, 5, 6, 5)); +// Prints: 1 ``` A `RangeError` will be thrown if: `targetStart < 0`, `sourceStart < 0`, @@ -1123,8 +1103,8 @@ for (let i = 0; i < 26; i++) { buf1.copy(buf2, 8, 16, 20); -// Prints: !!!!!!!!qrst!!!!!!!!!!!!! console.log(buf2.toString('ascii', 0, 25)); +// Prints: !!!!!!!!qrst!!!!!!!!!!!!! ``` Example: Create a single `Buffer` and copy data from one region to an @@ -1140,8 +1120,8 @@ for (let i = 0; i < 26; i++) { buf.copy(buf, 0, 4, 10); -// Prints: efghijghijklmnopqrstuvwxyz console.log(buf.toString()); +// Prints: efghijghijklmnopqrstuvwxyz ``` ### buf.entries() @@ -1159,6 +1139,9 @@ Example: Log the entire contents of a `Buffer` ```js const buf = Buffer.from('buffer'); +for (const pair of buf.entries()) { + console.log(pair); +} // Prints: // [0, 98] // [1, 117] @@ -1166,9 +1149,6 @@ const buf = Buffer.from('buffer'); // [3, 102] // [4, 101] // [5, 114] -for (const pair of buf.entries()) { - console.log(pair); -} ``` ### buf.equals(otherBuffer) @@ -1191,11 +1171,10 @@ const buf1 = Buffer.from('ABC'); const buf2 = Buffer.from('414243', 'hex'); const buf3 = Buffer.from('ABCD'); -// Prints: true console.log(buf1.equals(buf2)); - -// Prints: false +// Prints: true console.log(buf1.equals(buf3)); +// Prints: false ``` ### buf.fill(value[, offset[, end]][, encoding]) @@ -1223,8 +1202,8 @@ Example: Fill a `Buffer` with the ASCII character `'h'` ```js const b = Buffer.allocUnsafe(50).fill('h'); -// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh console.log(b.toString()); +// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh ``` `value` is coerced to a `uint32` value if it is not a String or Integer. @@ -1235,8 +1214,8 @@ then only the first bytes of that character that fit into `buf` are written. Example: Fill a `Buffer` with a two-byte character ```js -// Prints: console.log(Buffer.allocUnsafe(3).fill('\u0222')); +// Prints: ``` If `value` contains invalid characters, it is truncated; if no valid @@ -1244,12 +1223,13 @@ fill data remains, no filling is performed: ```js const buf = Buffer.allocUnsafe(5); -// Prints: + console.log(buf.fill('a')); -// Prints: +// Prints: console.log(buf.fill('aazz', 'hex')); // Prints: console.log(buf.fill('zz', 'hex')); +// Throws an exception. ``` ### buf.includes(value[, byteOffset][, encoding]) @@ -1268,27 +1248,20 @@ Equivalent to [`buf.indexOf() !== -1`][`buf.indexOf()`]. ```js const buf = Buffer.from('this is a buffer'); -// Prints: true console.log(buf.includes('this')); - // Prints: true console.log(buf.includes('is')); - // Prints: true console.log(buf.includes(Buffer.from('a buffer'))); - // Prints: true -// (97 is the decimal ASCII value for 'a') console.log(buf.includes(97)); - -// Prints: false +// Prints: true (97 is the decimal ASCII value for 'a') console.log(buf.includes(Buffer.from('a buffer example'))); - -// Prints: true -console.log(buf.includes(Buffer.from('a buffer example').slice(0, 8))); - // Prints: false +console.log(buf.includes(Buffer.from('a buffer example').slice(0, 8))); +// Prints: true console.log(buf.includes('this', 4)); +// Prints: false ``` ### buf.indexOf(value[, byteOffset][, encoding]) @@ -1323,32 +1296,25 @@ If `value` is: ```js const buf = Buffer.from('this is a buffer'); -// Prints: 0 console.log(buf.indexOf('this')); - -// Prints: 2 +// Prints: 0 console.log(buf.indexOf('is')); - -// Prints: 8 +// Prints: 2 console.log(buf.indexOf(Buffer.from('a buffer'))); - // Prints: 8 -// (97 is the decimal ASCII value for 'a') console.log(buf.indexOf(97)); - -// Prints: -1 +// Prints: 8 (97 is the decimal ASCII value for 'a') console.log(buf.indexOf(Buffer.from('a buffer example'))); - -// Prints: 8 +// Prints: -1 console.log(buf.indexOf(Buffer.from('a buffer example').slice(0, 8))); +// Prints: 8 const utf16Buffer = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2'); -// Prints: 4 console.log(utf16Buffer.indexOf('\u03a3', 0, 'ucs2')); - -// Prints: 6 +// Prints: 4 console.log(utf16Buffer.indexOf('\u03a3', -4, 'ucs2')); +// Prints: 6 ``` If `value` is not a string, number, or `Buffer`, this method will throw a @@ -1393,6 +1359,9 @@ Example: ```js const buf = Buffer.from('buffer'); +for (const key of buf.keys()) { + console.log(key); +} // Prints: // 0 // 1 @@ -1400,9 +1369,6 @@ const buf = Buffer.from('buffer'); // 3 // 4 // 5 -for (const key of buf.keys()) { - console.log(key); -} ``` ### buf.lastIndexOf(value[, byteOffset][, encoding]) @@ -1428,35 +1394,27 @@ instead of front to back. ```js const buf = Buffer.from('this buffer is a buffer'); -// Prints: 0 console.log(buf.lastIndexOf('this')); - -// Prints: 17 +// Prints: 0 console.log(buf.lastIndexOf('buffer')); - // Prints: 17 console.log(buf.lastIndexOf(Buffer.from('buffer'))); - -// Prints: 15 -// (97 is the decimal ASCII value for 'a') +// Prints: 17 console.log(buf.lastIndexOf(97)); - -// Prints: -1 +// Prints: 15 (97 is the decimal ASCII value for 'a') console.log(buf.lastIndexOf(Buffer.from('yolo'))); - -// Prints: 5 -console.log(buf.lastIndexOf('buffer', 5)); - // Prints: -1 +console.log(buf.lastIndexOf('buffer', 5)); +// Prints: 5 console.log(buf.lastIndexOf('buffer', 4)); +// Prints: -1 const utf16Buffer = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2'); -// Prints: 6 console.log(utf16Buffer.lastIndexOf('\u03a3', undefined, 'ucs2')); - -// Prints: 4 +// Prints: 6 console.log(utf16Buffer.lastIndexOf('\u03a3', -5, 'ucs2')); +// Prints: 4 ``` If `value` is not a string, number, or `Buffer`, this method will throw a @@ -1503,13 +1461,13 @@ Example: Create a `Buffer` and write a shorter ASCII string to it ```js const buf = Buffer.alloc(1234); -// Prints: 1234 console.log(buf.length); +// Prints: 1234 buf.write('some string', 0, 'ascii'); -// Prints: 1234 console.log(buf.length); +// Prints: 1234 ``` While the `length` property is not immutable, changing the value of `length` @@ -1522,13 +1480,13 @@ let buf = Buffer.allocUnsafe(10); buf.write('abcdefghj', 0, 'ascii'); -// Prints: 10 console.log(buf.length); +// Prints: 10 buf = buf.slice(0, 5); -// Prints: 5 console.log(buf.length); +// Prints: 5 ``` ### buf.parent @@ -1560,18 +1518,15 @@ the resulting behavior is undefined. ```js const buf = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); -// Prints: 8.20788039913184e-304 console.log(buf.readDoubleBE()); - -// Prints: 5.447603722011605e-270 +// Prints: 8.20788039913184e-304 console.log(buf.readDoubleLE()); - -// Throws an exception: RangeError: Index out of range +// Prints: 5.447603722011605e-270 console.log(buf.readDoubleLE(1)); - +// Throws an exception: RangeError: Index out of range +console.log(buf.readDoubleLE(1, true)); // Warning: reads passed end of buffer! // This will result in a segmentation fault! Don't do this! -console.log(buf.readDoubleLE(1, true)); ``` ### buf.readFloatBE(offset[, noAssert]) @@ -1594,18 +1549,15 @@ the resulting behavior is undefined. ```js const buf = Buffer.from([1, 2, 3, 4]); -// Prints: 2.387939260590663e-38 console.log(buf.readFloatBE()); - -// Prints: 1.539989614439558e-36 +// Prints: 2.387939260590663e-38 console.log(buf.readFloatLE()); - -// Throws an exception: RangeError: Index out of range +// Prints: 1.539989614439558e-36 console.log(buf.readFloatLE(1)); - +// Throws an exception: RangeError: Index out of range +console.log(buf.readFloatLE(1, true)); // Warning: reads passed end of buffer! // This will result in a segmentation fault! Don't do this! -console.log(buf.readFloatLE(1, true)); ``` ### buf.readInt8(offset[, noAssert]) @@ -1627,14 +1579,12 @@ Integers read from a `Buffer` are interpreted as two's complement signed values. ```js const buf = Buffer.from([-1, 5]); -// Prints: -1 console.log(buf.readInt8(0)); - -// Prints: 5 +// Prints: -1 console.log(buf.readInt8(1)); - -// Throws an exception: RangeError: Index out of range +// Prints: 5 console.log(buf.readInt8(2)); +// Throws an exception: RangeError: Index out of range ``` ### buf.readInt16BE(offset[, noAssert]) @@ -1659,14 +1609,12 @@ Integers read from a `Buffer` are interpreted as two's complement signed values. ```js const buf = Buffer.from([0, 5]); -// Prints: 5 console.log(buf.readInt16BE()); - -// Prints: 1280 +// Prints: 5 console.log(buf.readInt16LE()); - -// Throws an exception: RangeError: Index out of range +// Prints: 1280 console.log(buf.readInt16LE(1)); +// Throws an exception: RangeError: Index out of range ``` ### buf.readInt32BE(offset[, noAssert]) @@ -1691,14 +1639,12 @@ Integers read from a `Buffer` are interpreted as two's complement signed values. ```js const buf = Buffer.from([0, 0, 0, 5]); -// Prints: 5 console.log(buf.readInt32BE()); - -// Prints: 83886080 +// Prints: 5 console.log(buf.readInt32LE()); - -// Throws an exception: RangeError: Index out of range +// Prints: 83886080 console.log(buf.readInt32LE(1)); +// Throws an exception: RangeError: Index out of range ``` ### buf.readIntBE(offset, byteLength[, noAssert]) @@ -1722,14 +1668,12 @@ the resulting behavior is undefined. ```js const buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]); -// Prints: -546f87a9cbee console.log(buf.readIntLE(0, 6).toString(16)); - -// Prints: 1234567890ab +// Prints: -546f87a9cbee console.log(buf.readIntBE(0, 6).toString(16)); - -// Throws an exception: RangeError: Index out of range +// Prints: 1234567890ab console.log(buf.readIntBE(1, 6).toString(16)); +// Throws: RangeError [ERR_INDEX_OUT_OF_RANGE]: Index out of range ``` ### buf.readUInt8(offset[, noAssert]) @@ -1749,14 +1693,12 @@ the resulting behavior is undefined. ```js const buf = Buffer.from([1, -2]); -// Prints: 1 console.log(buf.readUInt8(0)); - -// Prints: 254 +// Prints: 1 console.log(buf.readUInt8(1)); - -// Throws an exception: RangeError: Index out of range +// Prints: 254 console.log(buf.readUInt8(2)); +// Throws an exception: RangeError: Index out of range ``` ### buf.readUInt16BE(offset[, noAssert]) @@ -1779,20 +1721,16 @@ the resulting behavior is undefined. ```js const buf = Buffer.from([0x12, 0x34, 0x56]); -// Prints: 1234 console.log(buf.readUInt16BE(0).toString(16)); - -// Prints: 3412 +// Prints: 1234 console.log(buf.readUInt16LE(0).toString(16)); - -// Prints: 3456 +// Prints: 3412 console.log(buf.readUInt16BE(1).toString(16)); - -// Prints: 5634 +// Prints: 3456 console.log(buf.readUInt16LE(1).toString(16)); - -// Throws an exception: RangeError: Index out of range +// Prints: 5634 console.log(buf.readUInt16LE(2).toString(16)); +// Throws an exception: RangeError: Index out of range ``` ### buf.readUInt32BE(offset[, noAssert]) @@ -1815,14 +1753,12 @@ the resulting behavior is undefined. ```js const buf = Buffer.from([0x12, 0x34, 0x56, 0x78]); -// Prints: 12345678 console.log(buf.readUInt32BE(0).toString(16)); - -// Prints: 78563412 +// Prints: 12345678 console.log(buf.readUInt32LE(0).toString(16)); - -// Throws an exception: RangeError: Index out of range +// Prints: 78563412 console.log(buf.readUInt32LE(1).toString(16)); +// Throws an exception: RangeError: Index out of range ``` ### buf.readUIntBE(offset, byteLength[, noAssert]) @@ -1846,14 +1782,12 @@ the resulting behavior is undefined. ```js const buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]); -// Prints: 1234567890ab console.log(buf.readUIntBE(0, 6).toString(16)); - -// Prints: ab9078563412 +// Prints: 1234567890ab console.log(buf.readUIntLE(0, 6).toString(16)); - -// Throws an exception: RangeError: Index out of range +// Prints: ab9078563412 console.log(buf.readUIntBE(1, 6).toString(16)); +// Throws an exception: RangeError: Index out of range ``` ### buf.slice([start[, end]]) @@ -1897,13 +1831,13 @@ for (let i = 0; i < 26; i++) { const buf2 = buf1.slice(0, 3); -// Prints: abc console.log(buf2.toString('ascii', 0, buf2.length)); +// Prints: abc buf1[0] = 33; -// Prints: !bc console.log(buf2.toString('ascii', 0, buf2.length)); +// Prints: !bc ``` Specifying negative indexes causes the slice to be generated relative to the @@ -1912,17 +1846,17 @@ end of `buf` rather than the beginning. ```js const buf = Buffer.from('buffer'); +console.log(buf.slice(-6, -1).toString()); // Prints: buffe // (Equivalent to buf.slice(0, 5)) -console.log(buf.slice(-6, -1).toString()); +console.log(buf.slice(-6, -2).toString()); // Prints: buff // (Equivalent to buf.slice(0, 4)) -console.log(buf.slice(-6, -2).toString()); +console.log(buf.slice(-5, -2).toString()); // Prints: uff // (Equivalent to buf.slice(1, 4)) -console.log(buf.slice(-5, -2).toString()); ``` ### buf.swap16() @@ -1938,18 +1872,18 @@ Interprets `buf` as an array of unsigned 16-bit integers and swaps the byte-orde ```js const buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]); -// Prints: console.log(buf1); +// Prints: buf1.swap16(); -// Prints: console.log(buf1); +// Prints: const buf2 = Buffer.from([0x1, 0x2, 0x3]); -// Throws an exception: RangeError: Buffer size must be a multiple of 16-bits buf2.swap16(); +// Throws an exception: RangeError: Buffer size must be a multiple of 16-bits ``` ### buf.swap32() @@ -1965,18 +1899,18 @@ Interprets `buf` as an array of unsigned 32-bit integers and swaps the byte-orde ```js const buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]); -// Prints: console.log(buf1); +// Prints: buf1.swap32(); -// Prints: console.log(buf1); +// Prints: const buf2 = Buffer.from([0x1, 0x2, 0x3]); -// Throws an exception: RangeError: Buffer size must be a multiple of 32-bits buf2.swap32(); +// Throws an exception: RangeError: Buffer size must be a multiple of 32-bits ``` ### buf.swap64() @@ -1992,18 +1926,18 @@ Throws a `RangeError` if [`buf.length`] is not a multiple of 8. ```js const buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]); -// Prints: console.log(buf1); +// Prints: buf1.swap64(); -// Prints: console.log(buf1); +// Prints: const buf2 = Buffer.from([0x1, 0x2, 0x3]); -// Throws an exception: RangeError: Buffer size must be a multiple of 64-bits buf2.swap64(); +// Throws an exception: RangeError: Buffer size must be a multiple of 64-bits ``` Note that JavaScript cannot encode 64-bit integers. This method is intended @@ -2025,8 +1959,8 @@ Example: const buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5]); const json = JSON.stringify(buf); -// Prints: {"type":"Buffer","data":[1,2,3,4,5]} console.log(json); +// Prints: {"type":"Buffer","data":[1,2,3,4,5]} const copy = JSON.parse(json, (key, value) => { return value && value.type === 'Buffer' ? @@ -2034,8 +1968,8 @@ const copy = JSON.parse(json, (key, value) => { value; }); -// Prints: console.log(copy); +// Prints: ``` ### buf.toString([encoding[, start[, end]]]) @@ -2063,22 +1997,19 @@ for (let i = 0; i < 26; i++) { buf1[i] = i + 97; } -// Prints: abcdefghijklmnopqrstuvwxyz console.log(buf1.toString('ascii')); - -// Prints: abcde +// Prints: abcdefghijklmnopqrstuvwxyz console.log(buf1.toString('ascii', 0, 5)); +// Prints: abcde const buf2 = Buffer.from('tést'); -// Prints: 74c3a97374 console.log(buf2.toString('hex')); - -// Prints: té +// Prints: 74c3a97374 console.log(buf2.toString('utf8', 0, 3)); - // Prints: té console.log(buf2.toString(undefined, 0, 3)); +// Prints: té ``` ### buf.values() @@ -2094,6 +2025,9 @@ called automatically when a `Buffer` is used in a `for..of` statement. ```js const buf = Buffer.from('buffer'); +for (const value of buf.values()) { + console.log(value); +} // Prints: // 98 // 117 @@ -2101,10 +2035,10 @@ const buf = Buffer.from('buffer'); // 102 // 101 // 114 -for (const value of buf.values()) { + +for (const value of buf) { console.log(value); } - // Prints: // 98 // 117 @@ -2112,9 +2046,6 @@ for (const value of buf.values()) { // 102 // 101 // 114 -for (const value of buf) { - console.log(value); -} ``` ### buf.write(string[, offset[, length]][, encoding]) @@ -2140,8 +2071,8 @@ const buf = Buffer.allocUnsafe(256); const len = buf.write('\u00bd + \u00bc = \u00be', 0); -// Prints: 12 bytes: ½ + ¼ = ¾ console.log(`${len} bytes: ${buf.toString('utf8', 0, len)}`); +// Prints: 12 bytes: ½ + ¼ = ¾ ``` ### buf.writeDoubleBE(value, offset[, noAssert]) @@ -2168,13 +2099,13 @@ const buf = Buffer.allocUnsafe(8); buf.writeDoubleBE(0xdeadbeefcafebabe, 0); -// Prints: console.log(buf); +// Prints: buf.writeDoubleLE(0xdeadbeefcafebabe, 0); -// Prints: console.log(buf); +// Prints: ``` ### buf.writeFloatBE(value, offset[, noAssert]) @@ -2201,13 +2132,13 @@ const buf = Buffer.allocUnsafe(4); buf.writeFloatBE(0xcafebabe, 0); -// Prints: console.log(buf); +// Prints: buf.writeFloatLE(0xcafebabe, 0); -// Prints: console.log(buf); +// Prints: ``` ### buf.writeInt8(value, offset[, noAssert]) @@ -2235,8 +2166,8 @@ const buf = Buffer.allocUnsafe(2); buf.writeInt8(2, 0); buf.writeInt8(-2, 1); -// Prints: console.log(buf); +// Prints: ``` ### buf.writeInt16BE(value, offset[, noAssert]) @@ -2266,8 +2197,8 @@ const buf = Buffer.allocUnsafe(4); buf.writeInt16BE(0x0102, 0); buf.writeInt16LE(0x0304, 2); -// Prints: console.log(buf); +// Prints: ``` ### buf.writeInt32BE(value, offset[, noAssert]) @@ -2297,8 +2228,8 @@ const buf = Buffer.allocUnsafe(8); buf.writeInt32BE(0x01020304, 0); buf.writeInt32LE(0x05060708, 4); -// Prints: console.log(buf); +// Prints: ``` ### buf.writeIntBE(value, offset, byteLength[, noAssert]) @@ -2326,13 +2257,13 @@ const buf = Buffer.allocUnsafe(6); buf.writeIntBE(0x1234567890ab, 0, 6); -// Prints: console.log(buf); +// Prints: buf.writeIntLE(0x1234567890ab, 0, 6); -// Prints: console.log(buf); +// Prints: ``` ### buf.writeUInt8(value, offset[, noAssert]) @@ -2360,8 +2291,8 @@ buf.writeUInt8(0x4, 1); buf.writeUInt8(0x23, 2); buf.writeUInt8(0x42, 3); -// Prints: console.log(buf); +// Prints: ``` ### buf.writeUInt16BE(value, offset[, noAssert]) @@ -2389,14 +2320,14 @@ const buf = Buffer.allocUnsafe(4); buf.writeUInt16BE(0xdead, 0); buf.writeUInt16BE(0xbeef, 2); -// Prints: console.log(buf); +// Prints: buf.writeUInt16LE(0xdead, 0); buf.writeUInt16LE(0xbeef, 2); -// Prints: console.log(buf); +// Prints: ``` ### buf.writeUInt32BE(value, offset[, noAssert]) @@ -2423,13 +2354,13 @@ const buf = Buffer.allocUnsafe(4); buf.writeUInt32BE(0xfeedface, 0); -// Prints: console.log(buf); +// Prints: buf.writeUInt32LE(0xfeedface, 0); -// Prints: console.log(buf); +// Prints: ``` ### buf.writeUIntBE(value, offset, byteLength[, noAssert]) @@ -2457,13 +2388,13 @@ const buf = Buffer.allocUnsafe(6); buf.writeUIntBE(0x1234567890ab, 0, 6); -// Prints: console.log(buf); +// Prints: buf.writeUIntLE(0x1234567890ab, 0, 6); -// Prints: console.log(buf); +// Prints: ``` ## buffer.INSPECT_MAX_BYTES @@ -2591,13 +2522,13 @@ const { SlowBuffer } = require('buffer'); const buf = new SlowBuffer(5); -// Prints: (contents may vary): console.log(buf); +// Prints: (contents may vary): buf.fill(0); -// Prints: console.log(buf); +// Prints: ``` ## Buffer Constants