The Buffer.from() function creates a new buffer and can be used in multiple ways.
The above prototype of the Buffer.from() function accepts an integer array that contains bytes in the range 0-255 and uses the bytes to create a buffer.
If the byte elements of the array lie outside the range 0-255, they are truncated to fit into the range.
The following example demonstrates how to use an array to create a buffer with the Buffer.form(array) function.
//create an arrayconst array = [0x12, 0x23, 0x13, 0x45, 0x31]//creat the buffer using the arrayconst buf = Buffer.from(array)console.log(buf)
The above prototype of the Buffer.from() function accepts the following parameters:
arrayBuffer: An ArrayBuffer or a SharedArrayBuffer with a .Buffer property.byteOffset: The byte from where the function starts to create the buffer. byteOffset is set to 0 by default.length: The number of bytes to include in the buffer. length is set to arrayBuffer.byteLength - byteOffset by default.Note: The underlying array and the buffer share the same memory.
The following example demonstrates how to use ArrayBuffer to create a buffer though the Buffer.from(arrayBuffer[, byteOffset[, length]]) function.
The program below shares memory with the underlying array. Therefore, when the array changes, the buffer changes as well. buf2 employs the byteoffset and length parameters to ensure that only the third element of the array is created in a buffer.
// create an ArrayBufferconst array = new Uint8Array(4);array[0] = 23;array[1] = 97;array[2] = 33;array[3] = 40;//create a buffer having a shared memory with the arrayconst buf = Buffer.from(array.buffer);console.log(buf);// change the arrayarray[1] = 145;// the buffer changes as wellconsole.log(buf);const buf2 = Buffer.from(array.buffer, 2,1);console.log(buf2);
This prototype of the Buffer.from() function accepts buffer of type Buffer or Uint8Array as a single input. Buffer.from() copies the data from the input to the created buffer.
The following example demonstrates how to create a buffer using an existing buffer with the Buffer.from( buffer) function.
//create an arraconst array = [22, 33, 44, 55, 66]//create a bufferconst buf1 = Buffer.from(array)console.log(buf1)//create another buffer using an existing bufferconst buf2 = Buffer. from(buf1)console.log(buf2)
This prototype of the Buffer.from() function accepts the following parameters:
object: An object that supports the Symbol.toPrimitive() or ValueOf() method.offsetOrEncoding: The byte offset of type integer, or an encoding of type string.length: The number of bytes to include in the buffer.The following example demonstrates how to create a buffer from objects that contain the Symbols.toPrimitive() or valueOf() method.
// create an object with Symbol.toPrimitive methodclass example {[Symbol.toPrimitive]() {return 'The example class has a Symbol.toPrimitive() method';}}const ex = new example()// create a buffer using the objectconst buf = Buffer.from( ex, 'utf8');console.log(buf)const str = "string has valuOf method"console.log(str.valueOf())// create a buffer using the object with valueOf methodconst buf1 = Buffer.from( ex, 'utf8');console.log(buf1)
This prototype of the Buffer.from() function accepts the following parameters:
string: The string that is encoded and made into a buffer.encoding: The encoding type, set to utf8 by default.The following code demonstrates how to create a buffer from strings with the Buffer.from(string [, encoding]) function.
//encode with utf8const buf1 = Buffer.from('a buffer created from a string');//encode with hexconst buf2 = Buffer.from('2074c3a97374468697', 'hex');console.log(buf1)console.log(buf2)
Free Resources