buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])
添加于:v0.1.90
参数
target
<Buffer> | <Uint8Array> ABuffer
或Uint8Array
targetStart
<integer>target
中开始写入的偏移量。 默认:0
。sourceStart
<integer>buf
内开始复制的偏移量。 默认:0
。sourceEnd
<integer>buf
内停止复制的偏移量(不包括在内)。 默认:buf.length
- 返回: <integer> 复制的字节数。
将数据从 buf
的区域复制到 target
的区域,即使 target
内存区域与 buf
重叠。
执行相同的操作,并且可用于所有 TypedArrays,包括 Node.js TypedArray.prototype.set()
Buffer
s,尽管它采用不同的函数参数。
import { Buffer } from 'node:buffer'; // Create two `Buffer` instances. const buf1 = Buffer.allocUnsafe(26); const buf2 = Buffer.allocUnsafe(26).fill('!'); for (let i = 0; i < 26; i++) { // 97 is the decimal ASCII value for 'a'. buf1[i] = i + 97; } // Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`. buf1.copy(buf2, 8, 16, 20); // This is equivalent to: // buf2.set(buf1.subarray(16, 20), 8); console.log(buf2.toString('ascii', 0, 25)); // Prints: !!!!!!!!qrst!!!!!!!!!!!!!
const { Buffer } = require('node:buffer'); // Create two `Buffer` instances. const buf1 = Buffer.allocUnsafe(26); const buf2 = Buffer.allocUnsafe(26).fill('!'); for (let i = 0; i < 26; i++) { // 97 is the decimal ASCII value for 'a'. buf1[i] = i + 97; } // Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`. buf1.copy(buf2, 8, 16, 20); // This is equivalent to: // buf2.set(buf1.subarray(16, 20), 8); console.log(buf2.toString('ascii', 0, 25)); // Prints: !!!!!!!!qrst!!!!!!!!!!!!!
import { Buffer } from 'node:buffer'; // Create a `Buffer` and copy data from one region to an overlapping region // within the same `Buffer`. const buf = Buffer.allocUnsafe(26); for (let i = 0; i < 26; i++) { // 97 is the decimal ASCII value for 'a'. buf[i] = i + 97; } buf.copy(buf, 0, 4, 10); console.log(buf.toString()); // Prints: efghijghijklmnopqrstuvwxyz
const { Buffer } = require('node:buffer'); // Create a `Buffer` and copy data from one region to an overlapping region // within the same `Buffer`. const buf = Buffer.allocUnsafe(26); for (let i = 0; i < 26; i++) { // 97 is the decimal ASCII value for 'a'. buf[i] = i + 97; } buf.copy(buf, 0, 4, 10); console.log(buf.toString()); // Prints: efghijghijklmnopqrstuvwxyz
相关用法
- Node.js Buffer buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])用法及代码示例
- Node.js Buffer buf.writeBigUInt64BE(value[, offset])用法及代码示例
- Node.js Buffer buf.toString([encoding[, start[, end]]])用法及代码示例
- Node.js Buffer buf.writeDoubleLE(value[, offset])用法及代码示例
- Node.js Buffer buf.writeBigInt64LE(value[, offset])用法及代码示例
- Node.js Buffer buf.keys()用法及代码示例
- Node.js Buffer buf.writeFloatLE(value[, offset])用法及代码示例
- Node.js Buffer buf.indexOf(value[, byteOffset][, encoding])用法及代码示例
- Node.js Buffer buf.readFloatLE([offset])用法及代码示例
- Node.js Buffer buf.readInt32LE([offset])用法及代码示例
- Node.js Buffer buf.writeInt8(value[, offset])用法及代码示例
- Node.js Buffer buf.swap32()用法及代码示例
- Node.js Buffer buf.writeInt32LE(value[, offset])用法及代码示例
- Node.js Buffer buf.writeIntLE(value, offset, byteLength)用法及代码示例
- Node.js Buffer buf.values()用法及代码示例
- Node.js Buffer buf.writeDoubleBE(value[, offset])用法及代码示例
- Node.js Buffer buf.length用法及代码示例
- Node.js Buffer buf.byteOffset用法及代码示例
- Node.js Buffer buf.readUInt32BE([offset])用法及代码示例
- Node.js Buffer buf.readDoubleLE([offset])用法及代码示例
- Node.js Buffer buf.writeFloatBE(value[, offset])用法及代码示例
- Node.js Buffer buf.readBigUInt64LE([offset])用法及代码示例
- Node.js Buffer buf.readUInt16BE([offset])用法及代码示例
- Node.js Buffer buf.writeInt16BE(value[, offset])用法及代码示例
- Node.js Buffer buf.swap64()用法及代码示例
注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。