buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])
添加於:v0.1.90
參數
target<Buffer> | <Uint8Array> ABuffer或複製到。Uint8ArraytargetStart<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: efghijghijklmnopqrstuvwxyzconst { 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]]])。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
