Nodejs Buffer
The JavaScript language itself only has a string data type and no binary data type.\\n\\nThe Buffer class in Node.js is the core tool for handling binary data, providing efficient operations on binary data.\\n\\nThe Buffer class is particularly useful in scenarios such as file operations, network communication, and image processing.\\n\\n**Features:**\\n\\n* **Binary Data**: A `Buffer` object is a fixed-size array containing raw binary data. Each element occupies one byte (8 bits), so `Buffer` is suitable for handling binary data, such as file contents, network packets, etc.\\n* **Immutability**: Although the contents of a `Buffer` object can be modified after creation, its length is fixed and cannot be dynamically changed.\\n\\n* * *\\n\\n## Buffer and Character Encoding\\n\\nBuffer instances are generally used to represent sequences of encoded characters, such as UTF-8, UCS2, Base64, or hexadecimal encoded data. By using explicit character encoding, you can convert between Buffer instances and ordinary JavaScript strings.\\n\\n## Example\\n\\nconst buf = Buffer.from('tutorial','ascii');\\n\\n// Output 72756e6f6f62\\n\\n console.log(buf.toString('hex'));\\n\\n// Output cnVub29i\\n\\n console.log(buf.toString('base64'));\\n\\n**Character encodings currently supported by Node.js include:**\\n\\n* **ascii** - Only supports 7-bit ASCII data. This encoding is very fast if the high bit is set to be removed.\\n\\n* **utf8** - Multibyte encoded Unicode characters. Many web pages and other document formats use UTF-8.\\n\\n* **utf16le** - 2 or 4 bytes, little-endian encoded Unicode characters. Supports surrogate pairs (U+10000 to U+10FFFF).\\n\\n* **ucs2** - Alias for **utf16le**.\\n\\n* **base64** - Base64 encoding.\\n\\n* **latin1** - A way to encode a **Buffer** into a one-byte encoded string.\\n\\n* **binary** - Alias for **latin1**.\\n\\n* **hex** - Encodes each byte as two hexadecimal characters.\\n\\n* * *\\n\\n## Creating a Buffer Class\\n\\nBuffer provides the following APIs to create a Buffer class:\\n\\n* **Buffer.alloc(size[, fill[, encoding]])οΌ** Creates a Buffer with a length of size bytes, which is equivalent to applying for size bytes of memory space, with the value of each byte being 0.\\n* **Buffer.allocUnsafe(size)οΌ** Creates a Buffer with a length of size bytes, but the Buffer may contain old data, which may affect execution results, hence it is called unsafe.\\n* **Buffer.allocUnsafeSlow(size)οΌ** Used to allocate a new Buffer instance of the given size, but does not initialize it.\\n* **Buffer.from(array)οΌ** Returns a new Buffer instance initialized with the values of the array (the elements of the passed-in array can only be numbers, otherwise they will automatically be overwritten with 0)\\n* **Buffer.from(arrayBuffer[, byteOffset[, length]])οΌ** Returns a newly created Buffer that shares the same memory as the given ArrayBuffer.\\n* **Buffer.from(buffer)οΌ** Copies the data of the passed-in Buffer instance and returns a new Buffer instance\\n* **Buffer.from(string[, encoding])οΌ** Creates a Buffer from a string, you can specify the encoding, which defaults to UTF-8.\\n\\n## Example\\n\\n// Create a Buffer of length 10, filled with 0's Bufferγ\\n\\nconst buf1 = Buffer.alloc(10);\\n\\n// Create a Buffer of length 10, filled with 0x1's Bufferγ \\n\\nconst buf2 = Buffer.alloc(10,1);\\n\\n// Create an uninitialized Buffer of length 10's Bufferγ\\n\\n// This method is faster than calling Buffer.alloc() faster,\\n\\n// but returns's Buffer The instance may contain old data,\\n\\n// Therefore, it must be overwritten using fill() or write().\\n\\nconst buf3 = Buffer.allocUnsafe(10);\\n\\n// Create a Buffer containing [0x1, 0x2, 0x3] 's Bufferγ\\n\\nconst buf4 = Buffer.from([1,2,3]);\\n\\n// Create a Buffer containing UTF-8 bytes [0x74, 0xc3, 0xa9, 0x73, 0x74] 's Bufferγ\\n\\nconst buf5 = Buffer.from('tΓ©st');\\n\\n// Create a Buffer containing Latin-1 bytes [0x74, 0xe9, 0x73, 0x74] 's Bufferγ\\n\\nconst buf6 = Buffer.from('tΓ©st','latin1');\\n\\n* * *\\n\\n## Writing to the Buffer\\n\\n### Syntax\\n\\nThe syntax for writing to a Node buffer is as follows:\\n\\nbuf.write(string[, offset[, length]][, encoding])\\n### Parameters\\n\\nThe parameter descriptions are as follows:\\n\\n* **string** - The string to write to the buffer.\\n\\n* **offset** - The index value of the buffer to start writing at, defaults to 0.\\n\\n* **length** - The number of bytes to write, defaults to buffer.length\\n\\n* **encoding** - The encoding to use. Defaults to 'utf8'.\\n\\nWrites string to buf at offset position according to the character encoding of encoding. The length parameter is the number of bytes to write. If buf does not have enough space to hold the entire string, only a part of the string will be written. Partially decoded characters will not be written.\\n\\n### Return Value\\n\\nReturns the actual size written. If the buffer space is insufficient, only a partial string will be written.\\n\\n### Example\\n\\nbuf = Buffer.alloc(256); len = buf.write("example.com"); console.log("Number of bytes written : "+ len);\\nExecuting the above code, the output result is:\\n\\n$node main.js Number of bytes written : 14\\n\\n* * *\\n\\n## Reading Data from the Buffer\\n\\n### Syntax\\n\\nThe syntax for reading data from a Node buffer is as follows:\\n\\nbuf.toString([encoding[, start[, end]]])\\n### Parameters\\n\\nThe parameter descriptions are as follows:\\n\\n* **encoding** - The encoding to use. Defaults to 'utf8'.\\n\\n* **start** - Specifies the index position to start reading, defaults to 0.\\n\\n* **end** - The ending position, defaults to the end of the buffer.\\n\\n### Return Value\\n\\nDecodes the buffer data and returns a string using the specified encoding.\\n\\n### Example\\n\\nbuf = Buffer.alloc(26);for (var i = 0 ; i { return value && value.type === 'Buffer' ? Buffer.from(value.data) : value;});// Output: console.log(copy);\\nExecuting the above code, the output result is:\\n\\n{"type":"Buffer","data":[1,2,3,4,5]}\\n\\n* * *\\n\\n## Buffer Concatenation\\n\\n### Syntax\\n\\nThe syntax for Node buffer concatenation is as follows:\\n\\nBuffer.concat(list[, totalLength])\\n### Parameters\\n\\nThe parameter descriptions are as follows:\\n\\n* **list** - The array list of Buffer objects to concatenate.\\n\\n* **totalLength** - Specifies the total length of the concatenated Buffer object.\\n\\n### Return Value\\n\\nReturns a new Buffer object concatenated from multiple members.\\n\\n### Example\\n\\nvar buffer1 = Buffer.from((''));var buffer2 = Buffer.from(('example.com'));var buffer3 = Buffer.concat([buffer1,buffer2]); console.log("buffer3 Content: " + buffer3.toString());\\nExecuting the above code, the output result is:\\n\\nbuffer3 Content: example.com\\n\\n* * *\\n\\n## Buffer Comparison\\n\\n### Syntax\\n\\nThe function syntax for Node Buffer comparison is as follows, this method was introduced in Node.js v0.12.2:\\n\\nbuf.compare(otherBuffer);\\n### Parameters\\n\\nThe parameter descriptions are as follows:\\n\\n* **otherBuffer** - Another Buffer object to compare with the **buf** object.\\n\\n### Return Value\\n\\nReturns a number, indicating whether **buf** comes before, after, or is the same as **otherBuffer**.\\n\\n### Example\\n\\nvar buffer1 = Buffer.from('ABC');var buffer2 = Buffer.from('ABCD');var result = buffer1.compare(buffer2);if(result < 0) { console.log(buffer1 + " in " + buffer2 + "before");}else if(result == 0){ console.log(buffer1 + " and " + buffer2 + "same
YouTip