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 ByteString conversions for JS binary types like ArrayBuffer, Int8Array, Blob, etc. #268

Open
joffrey-bion opened this issue Feb 26, 2024 · 0 comments
Labels
Design enhancement integration An issue related to the integration with other libraries or platform-specific APIs

Comments

@joffrey-bion
Copy link

joffrey-bion commented Feb 26, 2024

In the same vibe as #266, we could add conversions from JS binary types like ArrayBuffer, Blob, Int8Array...

Dealing with binary data in JS is a bit annoying. For example, the ArrayBuffer type is not directly usable and needs to be wrapped in a view. Providing such conversions out of the box in the kotlinx-io-bytestring artifact would be useful for a true multiplatform experience.

Examples:

/**
 * Creates a new [ArrayBuffer] containing the data copied from this [ByteString].
 */
fun ByteString.toArrayBuffer(): ArrayBuffer = toInt8Array().buffer

/**
 * Creates a new [Int8Array] containing the data copied from this [ByteString].
 */
fun ByteString.toInt8Array() = Int8Array(toArrayOfBytes())

private fun ByteString.toArrayOfBytes() = Array(size) { this[it] }

/**
 * Creates a new [ByteString] containing the data copied from this [ArrayBuffer].
 */
fun ArrayBuffer.toByteString(): ByteString = ByteString.wrap(toByteArray())

/**
 * Creates a new [ByteArray] containing the data copied from this [ArrayBuffer].
 */
fun ArrayBuffer.toByteArray(): ByteArray = Int8Array(this).toByteArray()

/**
 * Creates a new [ByteArray] containing the data copied from this [Int8Array].
 */
fun Int8Array.toByteArray() = ByteArray(length) { this[it] } // maybe there is more efficient here
@fzhinkin fzhinkin added enhancement Design integration An issue related to the integration with other libraries or platform-specific APIs labels Feb 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design enhancement integration An issue related to the integration with other libraries or platform-specific APIs
Projects
None yet
Development

No branches or pull requests

2 participants