The Buffer.copy
function is used to copy the contents from one buffer object to another.
buffer1.copy(buffer2, paste_si, copy_si, copy_ei);
The illustration below describes four parameters that the Buffer.copy
function takes:
The Buffer.copy
function returns the number of bytes copied from buffer1 to buffer2.
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());