Mateusz Krawczuk
02/16/2021, 9:38 PMimport platform.Foundation.*
actual class PlatformFile actual constructor(val filePath: String) {
private val platformFile = NSFileHandle.fileHandleForReadingAtPath(filePath)
actual fun setPosition(start: Long) {
platformFile!!.seekToFileOffset(start.toULong())
}
actual fun getLength(): Long {
val fileSize = NSFileManager.defaultManager.attributesOfItemAtPath(filePath,null) as NSDictionary
return fileSize.fileSize().toLong()
}
actual fun readIntoBuffer(buffer: ByteArray): Int {
platformFile!!.readDataUpToLength(buffer.size.toULong(),null)
}
}
Common Implementation:
expect class PlatformFile(filePath: String) {
/**
* Sets the file-pointer offset, measured from the beginning of this
* file, at which the next read or write occurs.
*/
fun setPosition(start: Long)
fun getLength(): Long
/**
* Reads up to {@code buffer.length} bytes of data from this file
* into an array of bytes.
* @param buffer the buffer into which the data is read.
* @return the total number of bytes read into the buffer, or
* {@code -1} if there is no more data because the end of
* this file has been reached.
*/
fun readIntoBuffer(buffer: ByteArray): Int
}
Android Implementation:
import java.io.RandomAccessFile
actual class PlatformFile actual constructor(filePath: String) {
private val platformFile = RandomAccessFile(filePath, "r")
actual fun setPosition(start: Long) = platformFile.seek(start)
actual fun getLength(): Long = platformFile.length()
actual fun readIntoBuffer(buffer: ByteArray): Int = platformFile.read(buffer)
}