Introduce runInterruptibleOkio

This commit is contained in:
tarsin committed 2023-05-29 16:03:41 +08:00
1 parent 51a292e5d0
commit 684d25780e
2 files changed
+26 -2

No files matched your search

@@ -34,7 +34,7 @@ import com.hippo.ehviewer.util.sendTo
import com.hippo.ehviewer.yorozuya.FileUtils
import com.hippo.unifile.UniFile
import com.hippo.unifile.openOutputStream
import kotlinx.coroutines.runInterruptible
import moe.tarsin.coroutines.runInterruptibleOkio
import moe.tarsin.coroutines.runSuspendCatching
import okhttp3.Response
import okio.buffer
@@ -145,7 +145,7 @@ class SpiderDen(private val mGalleryInfo: GalleryInfo) {
suspend fun doSave(outFile: UniFile): Long {
var ret = 0L
runInterruptible {
runInterruptibleOkio {
outFile.openOutputStream().sink().buffer().use { sink ->
response.body.source().use { source ->
while (true) {
@@ -0,0 +1,24 @@
package moe.tarsin.coroutines
import kotlinx.coroutines.runInterruptible
import java.io.InterruptedIOException
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
// See https://github.com/Kotlin/kotlinx.coroutines/issues/3551
suspend inline fun <T> runInterruptibleOkio(
context: CoroutineContext = EmptyCoroutineContext,
crossinline block: () -> T,
): T = runInterruptible(context) {
try {
block()
} catch (e: InterruptedIOException) {
if (Thread.currentThread().isInterrupted) {
// Coroutine cancelled
throw InterruptedException().initCause(e)
} else {
// AsyncTimeout reached
throw e
}
}
}