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

getting bytes as a stream/flow in common code #143

Open
nxoim opened this issue Oct 20, 2024 · 2 comments
Open

getting bytes as a stream/flow in common code #143

nxoim opened this issue Oct 20, 2024 · 2 comments
Labels
next Scheduled for the next version

Comments

@nxoim
Copy link

nxoim commented Oct 20, 2024

it would be nice to have an alternative to file.readBytes() in common code that returns a Flow<ByteArray> , in which each emmission is a chunk of data

@vinceglb
Copy link
Owner

Hi @nxoim! Thanks for creating this issue.

I'm currently working on a big update of FileKit. In the next version, PlatformFile on Android, Apple targets and JVM will be fully compatible with kotlinx-io. This way, it will be possible to read and write files like you want.

Here is an extension that we will soon be able to do:

public fun PlatformFile.asByteArrayFlow(chunkSize: Long): Flow<ByteArray> = flow {
    val source = source() ?: return@flow

    source.use { bufferedSource ->
        val buffer = Buffer()
        while (true) {
            // Read up to chunkSize bytes into the buffer
            bufferedSource.readAtMostTo(sink = buffer, byteCount = chunkSize)
            if (buffer.size == 0L) break // End of stream

            // Emit the contents of the buffer
            emit(buffer.readByteArray())

            // Clear the buffer for the next iteration
            buffer.clear()
        }
    }
}

If you want to try the next version of FileKit now, it's possible. I'm still working on it and moving things around. Also, there are some breaking changes with previous versions.

In your settings.gradle.kts

dependencyResolutionManagement {
    repositories {
        // ...
        maven("https://s01.oss.sonatype.org/content/repositories/snapshots/") // Add this line
    }
}

In your build.gradle.kts, the package names have been changed. Also, update the version to 0.10.0-SNAPSHOT.

// io.github.vinceglb:filekit-core is now:
implementation("io.github.vinceglb:filekit-dialog:0.10.0-SNAPSHOT")

// io.github.vinceglb:filekit-compose is now:
implementation("io.github.vinceglb:filekit-dialog-compose:0.10.0-SNAPSHOT")

In your code, when you call FileKit classes, you'll probably need to fix the imports. Things are called almost as before.

Here are some interesting files you can look at to understand how you can integrate with:

The samples are updated if you want to see some examples:

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

@nxoim
Copy link
Author

nxoim commented Oct 20, 2024

Here is an extension that we will soon be able to do

this was INSANELY helpful, thank you so much. works great on android, have not tested on other platforms yet

@vinceglb vinceglb added the next Scheduled for the next version label Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
next Scheduled for the next version
Projects
None yet
Development

No branches or pull requests

2 participants