Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

node.js

What is Buffer.copy in Node.js?

Rukhshan Haroon

The Buffer.copy function is used to copy the contents from one buffer object to another.

Syntax

buffer1.copy(buffer2, paste_si, copy_si, copy_ei);

Parameters

The illustration below describes four parameters that the Buffer.copy function takes:

Parameters illustration.

Return value

The Buffer.copy function returns the number of bytes copied from buffer1 to buffer2.

Example

The code snippet below demonstrates how data elements can be copied from one buffer to the other. This program copies data from the 3rd to 4th index of one_buffer and pastes it at the 0th index of two_buffer:

Notice that data at the 5th index in one_buffer was excluded.

var one_buffer = Buffer.from('000000');
var two_buffer = Buffer.from('111111111');

console.log("The buffer value from which we copy  is:", one_buffer.toString())
console.log("The buffer value to which we copy is:", two_buffer.toString())

one_buffer.copy(two_buffer, 0, 3, 5);

console.log("The new buffer is:", two_buffer.toString());

RELATED TAGS

node.js

CONTRIBUTOR

Rukhshan Haroon
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring