静态方法:Buffer.from(arrayBuffer[, byteOffset[, length]])
【Static method: Buffer.from(arrayBuffer[, byteOffset[, length]])】
arrayBuffer<ArrayBuffer> | <SharedArrayBuffer> 一个ArrayBuffer、SharedArrayBuffer,例如一个TypedArray的.buffer属性。byteOffset<integer> 要暴露的第一个字节的索引。默认值:0。length<integer> 要显示的字节数。 默认值:arrayBuffer.byteLength - byteOffset。- 返回:<Buffer>
这会创建 ArrayBuffer 的一个视图,而不会复制底层内存。例如,当传入 TypedArray 实例的 .buffer 属性的引用时,新创建的 Buffer 将与 TypedArray 的底层 ArrayBuffer 共享相同的分配内存。
【This creates a view of the ArrayBuffer without copying the underlying
memory. For example, when passed a reference to the .buffer property of a
TypedArray instance, the newly created Buffer will share the same
allocated memory as the TypedArray's underlying ArrayBuffer.】
import { Buffer } from 'node:buffer';
const arr = new Uint16Array(2);
arr[0] = 5000;
arr[1] = 4000;
// Shares memory with `arr`.
const buf = Buffer.from(arr.buffer);
console.log(buf);
// Prints: <Buffer 88 13 a0 0f>
// Changing the original Uint16Array changes the Buffer also.
arr[1] = 6000;
console.log(buf);
// Prints: <Buffer 88 13 70 17>const { Buffer } = require('node:buffer');
const arr = new Uint16Array(2);
arr[0] = 5000;
arr[1] = 4000;
// Shares memory with `arr`.
const buf = Buffer.from(arr.buffer);
console.log(buf);
// Prints: <Buffer 88 13 a0 0f>
// Changing the original Uint16Array changes the Buffer also.
arr[1] = 6000;
console.log(buf);
// Prints: <Buffer 88 13 70 17>可选的 byteOffset 和 length 参数指定 Buffer 将共享的 arrayBuffer 内的内存范围。
【The optional byteOffset and length arguments specify a memory range within
the arrayBuffer that will be shared by the Buffer.】
import { Buffer } from 'node:buffer';
const ab = new ArrayBuffer(10);
const buf = Buffer.from(ab, 0, 2);
console.log(buf.length);
// Prints: 2const { Buffer } = require('node:buffer');
const ab = new ArrayBuffer(10);
const buf = Buffer.from(ab, 0, 2);
console.log(buf.length);
// Prints: 2如果 arrayBuffer 不是 ArrayBuffer、SharedArrayBuffer 或适用于 Buffer.from() 的其他类型,将会抛出 TypeError。
【A TypeError will be thrown if arrayBuffer is not an ArrayBuffer or a
SharedArrayBuffer or another type appropriate for Buffer.from()
variants.】
重要的是要记住,作为后盾的 ArrayBuffer 可以覆盖超出 TypedArray 视图边界的内存范围。使用 TypedArray 的 buffer 属性创建的新 Buffer 可能会超出 TypedArray 的范围:
【It is important to remember that a backing ArrayBuffer can cover a range
of memory that extends beyond the bounds of a TypedArray view. A new
Buffer created using the buffer property of a TypedArray may extend
beyond the range of the TypedArray:】
import { Buffer } from 'node:buffer';
const arrA = Uint8Array.from([0x63, 0x64, 0x65, 0x66]); // 4 elements
const arrB = new Uint8Array(arrA.buffer, 1, 2); // 2 elements
console.log(arrA.buffer === arrB.buffer); // true
const buf = Buffer.from(arrB.buffer);
console.log(buf);
// Prints: <Buffer 63 64 65 66>const { Buffer } = require('node:buffer');
const arrA = Uint8Array.from([0x63, 0x64, 0x65, 0x66]); // 4 elements
const arrB = new Uint8Array(arrA.buffer, 1, 2); // 2 elements
console.log(arrA.buffer === arrB.buffer); // true
const buf = Buffer.from(arrB.buffer);
console.log(buf);
// Prints: <Buffer 63 64 65 66>