首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Kotlin Native FileSeek,getFileLength和readIntoBuffer中实现?

如何在Kotlin Native FileSeek,getFileLength和readIntoBuffer中实现?
EN

Stack Overflow用户
提问于 2021-02-17 05:57:06
回答 1查看 165关注 0票数 0

如何正确地为Kotlin Native/iOS实现这些功能?我正在尝试实现DFU实用程序的文件读取,但它不工作。

iOS实现:

代码语言:javascript
复制
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 {
        val read = platformFile!!.readDataOfLength(buffer.size.toULong())
        buffer = read.toByteArray()
        return read.length.toInt()
    }
}

常见实现:

代码语言:javascript
复制
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实现(工作实现):

代码语言:javascript
复制
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)
}
EN

回答 1

Stack Overflow用户

发布于 2021-02-17 07:20:39

它到底是怎么不工作的呢?编译器错误,运行时错误,或者只是没有做你期望的事情?错误消息/堆栈跟踪会有所帮助。

另外,你有没有考虑过使用Okio?它部分是多平台的,包括文件系统部分。

https://square.github.io/okio

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66232760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档