buf.byteOffset
- <integer>
Buffer的基础ArrayBuffer对象的byteOffset。
在 Buffer.from(ArrayBuffer, byteOffset, length) 中设置 byteOffset 时,或者有时在分配小于 Buffer.poolSize 的 Buffer 时,缓冲区不会从底层 ArrayBuffer 上的零偏移量开始。
当使用 buf.buffer 直接访问底层 ArrayBuffer 时,这可能会导致问题,因为 ArrayBuffer 的其他部分可能与 Buffer 对象本身无关。
创建与Buffer 共享其内存的TypedArray 对象时的一个常见问题是,在这种情况下,需要正确指定byteOffset:
import { Buffer } from 'node:buffer'; // Create a buffer smaller than `Buffer.poolSize`. const nodeBuffer = Buffer.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); // When casting the Node.js Buffer to an Int8Array, use the byteOffset // to refer only to the part of `nodeBuffer.buffer` that contains the memory // for `nodeBuffer`. new Int8Array(nodeBuffer.buffer, nodeBuffer.byteOffset, nodeBuffer.length);const { Buffer } = require('node:buffer'); // Create a buffer smaller than `Buffer.poolSize`. const nodeBuffer = Buffer.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); // When casting the Node.js Buffer to an Int8Array, use the byteOffset // to refer only to the part of `nodeBuffer.buffer` that contains the memory // for `nodeBuffer`. new Int8Array(nodeBuffer.buffer, nodeBuffer.byteOffset, nodeBuffer.length);
相关用法
- Node.js Buffer buf.buffer用法及代码示例
- 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.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()用法及代码示例
- Node.js Buffer buf.readUInt8([offset])用法及代码示例
注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 buf.byteOffset。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
