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

Add Buffer.copyTo(ByteArray) extension #191

Open
fzhinkin opened this issue Jul 28, 2023 · 2 comments
Open

Add Buffer.copyTo(ByteArray) extension #191

fzhinkin opened this issue Jul 28, 2023 · 2 comments

Comments

@fzhinkin
Copy link
Collaborator

Sometimes, data from a Buffer need to be partially copied into a byte array without consuming it from the buffer. The only way it could be achieved now is by creating a peek source and consuming data from it. Such an approach is far from being efficient.
I'm proposing to add a Buffer.copyTo(dest: ByteArray, startIndex = 0, endIndex = dest.size) extension that will behave the same way other existing Buffer.copyTo methods currently do.

@lppedd
Copy link
Contributor

lppedd commented Sep 14, 2023

Hey @fzhinkin, currently what's the most performant way to copy (or write) a Buffer to a byte array?
I'm using readByteArray(count) currenty, is that the way to do it?

Same for transforming a byte array into a Buffer. Currently it seems the only way is

val buffer = Buffer()
buffer.write(byteArray)

Is this the way to do it?
It feels like a Buffer could be initialized with a byte array directly, as a constructor argument.

@fzhinkin
Copy link
Collaborator Author

@lppedd what you mentioned is the way to read/write byte array from/to Buffer.

Note that readByteArray(count) doesn't copy the buffer, it consumes the buffer (so it's a read, not a copy operation). At the moment, a buffer could be copied into a byte array by using a peek buffer: buffer.peek().readByteArray(), this issue is about providing a better alternative to using peek.

It feels like a Buffer could be initialized with a byte array directly, as a constructor argument.

There's an issue suggesting providing an API to wrap arrays in Buffers (#166), I'll take a look at what could be done there in the context of #135.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants