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

Issues with PlatformInputStream on real apple device #157

Open
phcannesson opened this issue Nov 19, 2024 · 1 comment
Open

Issues with PlatformInputStream on real apple device #157

phcannesson opened this issue Nov 19, 2024 · 1 comment

Comments

@phcannesson
Copy link

phcannesson commented Nov 19, 2024

In PlatformFile apple implementation, I've noticed that only the readBytes method is using a security scope to access the data, through nsUrl.startAccessingSecurityScopedResource() and nsUrl.stopAccessingSecurityScopedResource().

It seems to cause issues with our app on real apple devices, as we're not able to retrieve the size (getSize()) and the input stream (getStream()) has no bytes available.
On emulators, it's working fine.

Are we doing something wrong ?

@vinceglb
Copy link
Owner

Hi @phcannesson, thanks for creating this issue.

Here is an example in sample-core of how we use PlatformInputStream:

bytes = if (file.supportsStreams()) {
    val size = file.getSize()
    if (size != null && size > 0L) {
        val buffer = ByteArray(size.toInt())
        val tmpBuffer = ByteArray(1000)
        var totalBytesRead = 0
        file.getStream().use {
            while (it.hasBytesAvailable()) {
                val numRead = it.readInto(tmpBuffer, 1000)
                tmpBuffer.copyInto(
                    buffer,
                    destinationOffset = totalBytesRead,
                    endIndex = numRead,
                )
                totalBytesRead += numRead
            }
        }
        buffer
    } else {
        file.readBytes()
    }
} else {
    file.readBytes()
}

I tried to launch this code on my real iPhone this morning, and it seems to work in that case.

That said, we are working on a new version of FileKit bringing a lot of changes. Typically:

  • PlatformInputStream will be deprecated / removed?
  • PlatformFile will integrate kotlinx-io operations on Android, Apple targets and JVM

It means that it will be possible to use kotlinx-io Sink and Source easily from a PlatformFile. This way, it will be possible to read and write files like you want. For example, we will use Source to read data from a stream. Also, operations will become much stable since they are maintained by the kotlinx-io library.

Soon, I'm going to publish this new version (0.10) of FileKit. If you are interested in trying this new version now, it's possible!
You can follow the steps in this comment: #143 (comment)

If you have any questions or feedback, don't hesitate!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants