package eu.friedcutlet.yokan.services.files import java.io.InputStream import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.currentCoroutineContext import kotlinx.coroutines.ensureActive import kotlinx.coroutines.runInterruptible import kotlinx.coroutines.withContext import kotlinx.coroutines.yield internal class JvmLocalMediaInput( override val relativePath: String, override val sizeBytes: Long?, private val open: () -> InputStream, ) : LocalMediaInput { override suspend fun readChunks(consume: suspend (ByteArray) -> Unit) = withContext(Dispatchers.IO) { open().use { input -> val buffer = ByteArray(LOCAL_IMPORT_CHUNK_BYTES) while (true) { currentCoroutineContext().ensureActive() val count = runInterruptible { input.read(buffer) } if (count < 0) break if (count > 0) consume(buffer.copyOf(count)) else yield() } } } }