• Node.js Video Tutorials

Node.js - Buffer.from() Method



The NodeJS Buffer.from() method will create a copy of the ArrayBuffer passed without copying the underlying memory. When the .buffer property of TypedArray instance is passed to Buffer.from(), the buffer that is created will share same memory as TypedArray.

A TypedArray is nothing but an object with an array like view.Example of TypedArray is : new Int8Array(), new Uint16Array().

Syntax

Following is the syntax of the Node.JS Buffer.from(arrayBuffer[, byteOffset[, length]]) Method

Buffer.from(arrayBuffer[, byteOffset[, length]])
Buffer.from(buffer)
Buffer.from(str[, encoding])

Parameters

This method accepts three parameters. The same is explained below.

  • arrayBuffer − (required) An ArrayBuffer, SharedArrayBuffer, for example the .buffer property of a TypedArray.

  • byteOffset − (optional) An offset value. The default is 0.

  • length − (optional) The bytes to be exposed.Default value is arrayBuffer.length - byteOffset.

  • buffer − (required) A buffer object.

  • str − (required) A string value.

  • encoding −(optional) Encoding to be used. By default, it is utf-8.

Return value

The method Buffer.from(arrayBuffer[, byteOffset[, length]]) returns a new buffer with .buffer property from TypedArray.

Example

In this example will make use of NodeJS Int8Array() and create a buffer from it.

const arr = new Int8Array(5);
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
const buf = Buffer.from(arr.buffer);
console.log(buf);

Output

<Buffer 01 02 03 00 00>

Example

This example will make use of Int8Array() and byteOffset parameters and create a buffer from it.

const arr = new Int8Array(5);
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
const buf = Buffer.from(arr.buffer, 1);
console.log(buf);

Output

<Buffer 02 03 00 00>

Example

This example will make use of Int8Array() and byteOffset and length parameters and create a buffer from it.

const arr = new Int8Array(5);
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
const buf = Buffer.from(arr.buffer, 1, 3);
console.log(buf);

Output

<Buffer 02 03 00>

Example

The example will create a new buffer using buffer.from(buffer) method.

const buffer1 = Buffer.from('Hello World');
const buffer2 = Buffer.from(buffer1);
console.log(buffer1.toString());
console.log(buffer2.toString());

Output

Hello World
Hello World

Example

The method Buffer.from() will give type error if the input given is not a buffer.

const buffer1 = 1;
const buffer2 = Buffer.from(buffer1);
console.log(buffer2);

Output

TypeError [ERR_INVALID_ARG_TYPE]: The "value" argument must not be of type number. Received type number
   at Function.from (buffer.js:208:11)
   at Object.<anonymous> (C:\nodejsProject\src\testbuffer.js:2:24)
   at Module._compile (internal/modules/cjs/loader.js:816:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
   at Module.load (internal/modules/cjs/loader.js:685:32)
   at Function.Module._load (internal/modules/cjs/loader.js:620:12)
   at Function.Module.runMain (internal/modules/cjs/loader.js:877:12)
   at internal/main/run_main_module.js:21:11

Example

The example will create a new buffer using NodeJS buffer.from() method.

const obj = new String('Welcome to Tutorialspoint!');
console.log(obj.valueOf());
const newbuffer = Buffer.from(obj);
console.log(newbuffer);

Output

Welcome to Tutorialspoint!
<Buffer 57 65 6c 63 6f 6d 65 20 74 6f 20 54 75 74 6f 72 69 61 6c 73 70 6f 69 6e 74 21>

Example

The example will create a new buffer using buffer.from() method.

const object1 = {
   [Symbol.toPrimitive]() {
      return 'testing buffer';
   }
};  
const newBuffer = Buffer.from(object1, 'utf8');
console.log(newBuffer);

Output

<Buffer 74 65 73 74 69 6e 67 20 62 75 66 66 65 72>

Example

The example will create a new buffer using buffer.from() method using object, encoding and length.

const object1 = {
   [Symbol.toPrimitive]() {
      return 'testing buffer';
   }
};  
const newBuffer = Buffer.from(object1, 'utf-8', 10);
console.log(newBuffer);

Output

<Buffer 74 65 73 74 69 6e 67 20 62 75 66 66 65 72>
nodejs_buffer_module.htm
Advertisements