LocalMediaInput.jvm.kt
composeApp/src/jvmCommonMain/kotlin/eu/friedcutlet/yokan/services/files/LocalMediaInput.jvm.kt
1package eu.friedcutlet.yokan.services.files23import java.io.InputStream4import kotlinx.coroutines.Dispatchers5import kotlinx.coroutines.currentCoroutineContext6import kotlinx.coroutines.ensureActive7import kotlinx.coroutines.runInterruptible8import kotlinx.coroutines.withContext9import kotlinx.coroutines.yield1011internal class JvmLocalMediaInput(12 override val relativePath: String,13 override val sizeBytes: Long?,14 private val open: () -> InputStream,15) : LocalMediaInput {16 override suspend fun readChunks(consume: suspend (ByteArray) -> Unit) = withContext(Dispatchers.IO) {17 open().use { input ->18 val buffer = ByteArray(LOCAL_IMPORT_CHUNK_BYTES)19 while (true) {20 currentCoroutineContext().ensureActive()21 val count = runInterruptible { input.read(buffer) }22 if (count < 0) break23 if (count > 0) consume(buffer.copyOf(count))24 else yield()25 }26 }27 }28}29