• Node.js Video Tutorials

Node.js - Buffer.copy() Method



The NodeJS Buffer.copy() method will copy the contents of the source buffer object into the target buffer. The contents will be copied even if there is memory overlap between the two buffers.

Syntax

Following is the syntax of the NodeJs copy() method −

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

Parameters

The buffer.copy() method takes four parameters. The first parameter target is a mandatory one and the rest are optional.

  • target −This is a mandatory parameter. It is a target buffer object to which the source buffer object will be copied.

  • targetStart −It refers to the offset within the target buffer to start writing from. It is 0 by default.

  • sourceStart −It refers to the offset within the source buffer to start copying from. It is 0 by default.

  • sourceEnd −It refers to the offset within the source buffer to stop copying at. The default value is the length of the source buffer.

Return value

The buffer.copy() method returns the number of bytes copied.

Example

Using NodeJS buffer.copy() method to copy contents from one buffer to another.

var buffer1 = Buffer.from('testing');
var buffer2 = Buffer.from('HELLO');
buffer2.copy(buffer1);
console.log("After copying the string in buffer1 is "+ buffer1.toString());

Output

In this example we are copying the string HELLO inside buffer2 to buffer1. When you print the contents of buffer1 you will get following output −

After copying the string in buf1 is HELLOng

Example

In this example will make use of the targetStart parameter while copying.

var buffer1 = Buffer.from('testing');
var buffer2 = Buffer.from('HELLO');
buffer2.copy(buffer1, 3);
console.log("After copying the string in buffer1 is "+ buffer1.toString());

Output

In the above example we have used targetStart as 3. So in buffer1 the string is testing, so from offset 3 till the end of the string the contents from buffer2 will be copied.

After copying the string in buffer1 is tesHELL

Example

In this example will make use of targetStart and sourceStart to copy contents using buffer.copy()

var buffer1 = Buffer.from('testing');
var buffer2 = Buffer.from('HELLO');
buffer2.copy(buffer1, 2, 2);
console.log("After copying the string in buffer1 is "+ buffer1.toString());

Output

In the above example we have used targetStart as 2. So in buffer1 the string is testing, so from offset 2 till the end of the string the contents from buffer2 will be copied. We have made use of sourceStart, so the string in buffer2 is HELLO, the contents to copy will start from offset 2 i.e. LLO will be the contents that will be copied to buffer1.

After copying the string in buffer1 is teLLOng

Example

Let us now make use of all the parameters that come with the Buffer.copy() method.

var buffer1 = Buffer.from('testing');
var buffer2 = Buffer.from('HELLO');
buffer2.copy(buffer1, 2, 0, 3);
console.log("After copying the string in buffer1 is "+ buffer1.toString());

Output

In the example above we have used tartgetStart, sourceStart and sourceEnd parameters. So the string that will be picked from source buffers is HEL and the same will be copied to buffer1 from the offset 2.

After copying the string in buffer1 is teHELng

Example

In this example will create a buffer using the method Buffer.allocUnsafe() and later add characters to it using the Unicode values.

var buffer1 = Buffer.allocUnsafe(10);
var buffer2 = Buffer.from('HELLO');
for (let i = 0; i < 10; i++) {
   
   //109 is for character m, it will increment and n,o,p..will get added.
   buffer1[i] = i + 109;
}
   
console.log("The string in buffer1 is "+buffer1.toString());   
buffer2.copy(buffer1, 2);      
console.log("The string in buffer1 is "+buffer1.toString());

Output

We have allocated 10 bytes in buffer1 using Buffer.allocUnsafe(). Later we are looping using a for loop from 0 to 10 and adding characters to the buffer1. The contents of buffer2 is copied inside buffer1 from the offset starting at 2 in buffer1.

When you execute above code the output is as shown below −

The string in buffer1 is mnopqrstuv
The string in buffer1 is mnHELLOtuv

Example

It will throw an error when you try to use values in parameters targetStart and sourceStart less than zero.

Let us see an example when we use targetStart and sourceStart less than zero.

var buffer1 = Buffer.from('testing');
var buffer2 = Buffer.from('HELLO');
buffer2.copy(buffer1, -1, -1, 3);
console.log("After copying the string in buffer1 is "+ buffer1.toString());

Output

You will get an error for above example since we have use targetStart and sourceStart as -1. The default value is 0 and less than 0 will throw and error as shown in the output below

RangeError: Index out of range
nodejs_buffer_module.htm
Advertisements