Node.js Buffer.copy() Method (original) (raw)

Last Updated : 26 Aug, 2024

**Buffer is a temporary memory storage that stores the data when it is being moved from one place to another. It is like an array of integers. The **Buffer.copy() method simply copies all the values from the input buffer to another buffer.

**Syntax:

buffer.copy( target, targetStart, sourceStart, sourceEnd )

**Parameters: This method accepts two parameters as mentioned above and described below:

**Return Value: This method returns the number that indicates the number of bytes copied.

**Note: Copies the values from the input buffer and overwrites them to the output buffer even if the target memory regions (indexes) already exist. Copies data from a region of the input buffer to a region in the target buffer even if the target memory region overlaps with the input buffer.

**Example 1: The below example illustrates the use of **Buffer.copy() Method in Node.js

javascript `

// Node.js program to demonstrate the // Buffer.copy() Method

// Creating a buffer let buffer2 = Buffer.from('for');

let buffer1 = Buffer.from('GeeksandGeeks');

buffer2.copy(buffer1, 5, 0);

console.log(buffer1.toString());

`

**Output:

GeeksforGeeks

**Example 2: The below example illustrates the use of **Buffer.copy() Method in Node.js

javascript `

// Node.js program to demonstrate the // Buffer.copy() Method

let buffer2 = Buffer.allocUnsafe(5);

let buffer1 = Buffer.from('Geeks');

for (let i = 0; i < 5; i++) {

// Adds: 'a b c d e' as 97 98 99 100 101
// are their respective ASCII values
buffer2[i] = i + 97;

}

buffer2.copy(buffer1, 2);

// Prints 'Geabc' as the input buffer1 // carries 'Geeks' and we provided the // targetStart index as 2 // so elements will replace the values in // buffer1 starting from index 2

console.log(buffer1.toString());

`

**Output:

Geabc

**Note: The above program will compile and run by using the node index.js command.

**Reference: https://nodejs.org/api/buffer.html#buffer_buf_copy_target_targetstart_sourcestart_sourceend