Skip to content
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

Buffer.from Offset #7134

Closed
360disrupt opened this issue Jun 3, 2016 · 4 comments
Closed

Buffer.from Offset #7134

360disrupt opened this issue Jun 3, 2016 · 4 comments
Labels
buffer Issues and PRs related to the buffer subsystem.

Comments

@360disrupt
Copy link

  • Version: v.6.2.1
  • Platform: MAC OSX 10.11.5
  • Subsystem: Buffer

Offset is not applied in Buffer.from

console.log "Deducting #{msgSentBuffer.length} from #{msgReceivingBuffer.length}" #logs Deducting 5 from 13
msgAnswer = Buffer.from(msgReceivingBuffer, msgSentBuffer.length) #so 5 should be the offset
console.log "Length is now: #{msgAnswer.length}" #logs Length is now:13, but should be 13 - 5 = 8
console.log chalk.bgMagenta "Answer", toIntArray msgAnswer #Is a copy of msgReceivingBuffer there is nothing deducted

Or is The optional byteOffset and length arguments specify a memory range within the arrayBuffer that will be shared by the Buffer. this to be understood as:

  • that the new Buffer contains the same bytes as the old buffer
  • , but only the part before the offset is copied,
  • while the other part is pointing to the original buffer?
@mscdex mscdex added the buffer Issues and PRs related to the buffer subsystem. label Jun 3, 2016
@mscdex
Copy link
Contributor

mscdex commented Jun 3, 2016

/cc @nodejs/buffer

@trevnorris
Copy link
Contributor

trevnorris commented Jun 3, 2016

Documentation is a bit confusing. Passing Buffer falls under the API of Buffer.from(array). Which copies the whole contents of the Buffer into a new Buffer.

What you're likely expecting is Buffer.from(arrayBuffer[, byteOffset[, length]]), but to do this you'll need to change your code to the following:

msgAnswer = Buffer.from(msgReceivingBuffer.buffer,
                        msgReceivingBuffer.byteOffset + msgSentBuffer.length);

Can you try that out and let me know if it works for you?

Side note, reason the API is setup like this is because if a dev simply wants to slice the buffer then they can run msgAnswer.slice(msgSentBuffer.length); Buffer.from() accepts ArrayBuffer simply as a way to work with typed arrays.

@360disrupt
Copy link
Author

Thanks, your solution nearly worked. Without the third length param, the new buffer was very long and full of random numbers. Finally this worked:

console.log chalk.bgMagenta "Original", toIntArray msgReceivingBuffer #Original 80,0,1,242,189,1,3,80,0,136,65,5,222
console.log chalk.bgRed "Offset", msgReceivingBuffer.byteOffset, "Offset2 ", msgSentBuffer.length, "Length ", msgReceivingBuffer.length #Offset 4016 Offset2  5 Length  13

msgAnswer = Buffer.from msgReceivingBuffer.buffer, msgReceivingBuffer.byteOffset + msgSentBuffer.length, 8

console.log chalk.bgMagenta "Sent", toIntArray msgSentBuffer #Sent 80,0,1,242,189
console.log chalk.bgMagenta "Answer", toIntArray msgAnswer #Answer 1,3,80,0,136,65,5,222

msgAnswer = msgReceivingBuffer.slice(5)works also in looks more intuitive and shorter.

Just to understand, why is msgReceivingBuffer.byteOffset 4016, so all my Buffer objects are save within the same space?

Instances of the Buffer class are similar to arrays of integers but correspond to fixed-sized, raw memory allocations outside the V8 heap.

So every single Buffer object is just allocated space in that global Buffer object?

@trevnorris
Copy link
Contributor

In certain cases we optimize new Buffer allocations by using a preallocated larger Buffer and take a slice from that. It's just a performance optimization for certain cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
buffer Issues and PRs related to the buffer subsystem.
Projects
None yet
Development

No branches or pull requests

3 participants