-
Notifications
You must be signed in to change notification settings - Fork 30.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
doc: add documentation for buffer.byteOffset #21718
The head ref may contain hidden characters: "doc\u2013buffer-byteOffset"
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -992,6 +992,31 @@ console.log(buffer.buffer === arrayBuffer); | |
// Prints: true | ||
``` | ||
|
||
### buf.byteOffset | ||
|
||
* {integer} The byteOffset on the underlying `ArrayBuffer` object based on | ||
which this `Buffer` object is created. | ||
|
||
When setting `byteOffset` in `Buffer.from(ArrayBuffer, byteOffset, length)` | ||
or sometimes when allocating a buffer smaller than `Buffer.poolSize` the | ||
buffer doesn't start from a zero offset on the underlying `ArrayBuffer`. | ||
|
||
This can cause problems when accessing the underlying `ArrayBuffer` directly | ||
using `buf.buffer`, as the first bytes in this `ArrayBuffer` may be unrelated | ||
to the `buf` object itself. | ||
|
||
A common issue is when casting a `Buffer` object to an `TypedArray` object, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
in this case one needs to specify the `byteOffset` correctly: | ||
|
||
```js | ||
// create a buffer smaller than `Buffer.poolSize` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
const nodeBuffer = new Buffer.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); | ||
|
||
// When casting the node.js Buffer to a Int8 TypedArray remember to use the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// byteOffset. | ||
new Int8Array(nodeBuffer.buffer, nodeBuffer.byteOffset, nodeBuffer.length); | ||
``` | ||
|
||
### buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]]) | ||
<!-- YAML | ||
added: v0.11.13 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
byteOffset
->`byteOffset`
orbyte offset
?