All Downloads are FREE. Search and download functionalities are using the official Maven repository.

iosMain.net.iriscan.sdk.utils.NSDataExt.kt Maven / Gradle / Ivy

There is a newer version: 0.6.0
Show newest version
package net.iriscan.sdk.utils

import kotlinx.cinterop.*
import platform.Foundation.NSData
import platform.Foundation.dataWithBytesNoCopy
import platform.posix.memcpy

/**
 * @author Slava Gornostal
 */
fun NSData.toByteArray(): ByteArray = memScoped {
    val buffer = ByteArray(length.toInt())
    val dataPtr = bytes?.reinterpret()
    buffer.usePinned { pinned -> memcpy(pinned.addressOf(0), dataPtr, length) }
    return buffer
}

fun ByteArray.toNSData(): NSData = usePinned { pinned ->
    NSData.dataWithBytesNoCopy(pinned.addressOf(0), this.size.toULong(), false)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy