1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| const allocUnsafe = Buffer ? Buffer.allocUnsafe : undefined; const buf = Buffer.from("laoyuan");
function cloneBuffer(buffer, isDeep) { if (!isDeep) { return buffer.slice(); } const length = buffer.length, result = allocUnsafe ? allocUnsafe(length) : new buffer.constructor(length);
return result; }
const buf2 = cloneBuffer(buf, true);
buf2.write("nodejs"); buf2.write("22");
console.log("buf", buf.toString("utf-8")); console.log("buf2", buf2.toString("utf-8"));
|