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

Access to underlying Buffer primitive #7

Open
whyoleg opened this issue Jun 23, 2022 · 0 comments
Open

Access to underlying Buffer primitive #7

whyoleg opened this issue Jun 23, 2022 · 0 comments

Comments

@whyoleg
Copy link
Collaborator

whyoleg commented Jun 23, 2022

As stated in design/BUFFER.md

The main purpose of IO is read and write arrays of bytes. There is a ByteArray - common Kotlin array of bytes abstraction, but it usually is not the efficient way to represent array of bytes on the platform(ByteBuffer on JVM, NSData on iOS, CPointer on Native, ArrayBuffer on Js and so on).

In multiplatform code, it will be ok to use just Buffer, but in platform specific code, near interop with already written Java or Native code, we need to have access to underlying primitive, like ByteBuffer on JVM and CPointer or ByteArray (which can be converted to CPointer) on Native.

My use case is using JDK and OpenSSL for cryptography API, where JDK API need ByteArray or ByteBuffer and OpenSSL native API has CPointer. Of course, we may read ByteArray from Buffer and use it, and also we can create Buffer from ByteArray - but this will be not efficient as copies will be created.

I would think, that such a functionality should be hidden under some UnsafeBufferApi (similar to DelicateCoroutinesApi) opt-in annotation, as such code will be to easy to misuse.

Possible designs:

  1. Make specific Buffer implementation public with public property/function to access underlying primitive
  2. Make expect/actual to access ONLY platform specific representations
  3. Create some other way to safely access underlying primitive, but not tight to specific implementation, so different implementations be it wrapping ByteBuffer or ByteArray or other primitive can be accessed both as ByteArray and ByteBuffer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant