Instructions for the buffer.copy method in node.js

  • 2020-05-05 10:52:50
  • OfStack

method description:

Perform copy and replace operations between different buffer.

Copies the data from the source buffer and replaces it to the specified location of the target buffer.

syntax:


buffer.copy(targetBuffer, [targetStart], [sourceStart], [sourceEnd])

receive parameters:

targetBuffer               target buffer

targetStart                   buffer data replacement starting position

sourceStart                 source buffer data copy starting location

sourceEnd                 source buffer data copy end location

example:

In this example, data between buf1, 16 and 20 are extracted, copied into buf2, and replaced from buf2 position 8.


buf1 = new Buffer(26);
buf2 = new Buffer(26);
for (var i = 0 ; i < 26 ; i++) {
  buf1[i] = i + 97; // 97 is ASCII a
  buf2[i] = 33; // ASCII !
}
buf1.copy(buf2, 8, 16, 20);
console.log(buf2.toString('ascii', 0, 25));


Related articles: