@ -1,10 +1,29 @@
package org.jetbrains.compose.resources
package org.jetbrains.compose.resources
import kotlinx.cinterop.BetaInteropApi
import kotlinx.cinterop.ObjCObjectVar
import kotlinx.cinterop.addressOf
import kotlinx.cinterop.addressOf
import kotlinx.cinterop.alloc
import kotlinx.cinterop.memScoped
import kotlinx.cinterop.ptr
import kotlinx.cinterop.usePinned
import kotlinx.cinterop.usePinned
import platform.Foundation.*
import kotlinx.cinterop.value
import org.jetbrains.skiko.OS
import org.jetbrains.skiko.OSVersion
import org.jetbrains.skiko.available
import platform.Foundation.NSBundle
import platform.Foundation.NSData
import platform.Foundation.NSError
import platform.Foundation.NSFileHandle
import platform.Foundation.NSFileManager
import platform.Foundation.NSURL
import platform.Foundation.closeFile
import platform.Foundation.fileHandleForReadingAtPath
import platform.Foundation.readDataOfLength
import platform.Foundation.seekToFileOffset
import platform.posix.memcpy
import platform.posix.memcpy
@OptIn ( BetaInteropApi :: class )
internal actual fun getPlatformResourceReader ( ) : ResourceReader = object : ResourceReader {
internal actual fun getPlatformResourceReader ( ) : ResourceReader = object : ResourceReader {
override suspend fun read ( path : String ) : ByteArray {
override suspend fun read ( path : String ) : ByteArray {
val data = readData ( getPathInBundle ( path ) )
val data = readData ( getPathInBundle ( path ) )
@ -30,7 +49,15 @@ internal actual fun getPlatformResourceReader(): ResourceReader = object : Resou
private fun readData ( path : String , offset : Long , size : Long ) : NSData {
private fun readData ( path : String , offset : Long , size : Long ) : NSData {
val fileHandle = NSFileHandle . fileHandleForReadingAtPath ( path ) ?: throw MissingResourceException ( path )
val fileHandle = NSFileHandle . fileHandleForReadingAtPath ( path ) ?: throw MissingResourceException ( path )
fileHandle . seekToOffset ( offset . toULong ( ) , null )
if ( available ( OS . Ios to OSVersion ( major = 13 ) ) ) {
memScoped {
val error = alloc < ObjCObjectVar < NSError ? > > ( )
fileHandle . seekToOffset ( offset . toULong ( ) , error . ptr )
error . value ?. let { err -> error ( err . localizedDescription ) }
}
} else {
fileHandle . seekToFileOffset ( offset . toULong ( ) )
}
val result = fileHandle . readDataOfLength ( size . toULong ( ) )
val result = fileHandle . readDataOfLength ( size . toULong ( ) )
fileHandle . closeFile ( )
fileHandle . closeFile ( )
return result
return result