This article mainly introduces node. buffer in js. the usage of the copy method. This article introduces the buffer. copy method description, syntax, receive parameters, use instances, and implement Source Code. For more information, see
Method description:
Replace the buffer.
Copy data from the source buffer and replace it with the specified location of the target buffer.
Syntax:
The Code is as follows:
Buffer. copy (targetBuffer, [targetStart], [sourceStart], [sourceEnd])
Receiving parameters:
TargetBuffer: The target buffer, which is replaced by the buffer for replication.
The starting position of the target buffer data replacement by targetStart
Source buffer Data Replication start position
End position of sourceEnd source buffer Data Replication
Example:
In this example, the data between buf1 16 and 20 is extracted, the data is copied to buf2, and is replaced from position 8 of buf2.
The Code is as follows:
Buf1 = new Buffer (26 );
Buf2 = new Buffer (26 );
For (var I = 0; I <26; I ++ ){
Buf1 [I] = I + 97; // 97 is ASCII
Buf2 [I] = 33; // ASCII!
}
Buf1.copy (buf2, 8, 16, 20 );
Console. log (buf2.toString ('ascii ', 0, 25 ));