diff --git a/readium/lcp/src/main/java/org/readium/r2/lcp/LcpError.kt b/readium/lcp/src/main/java/org/readium/r2/lcp/LcpError.kt index 85d2ee611d..7b89aded17 100644 --- a/readium/lcp/src/main/java/org/readium/r2/lcp/LcpError.kt +++ b/readium/lcp/src/main/java/org/readium/r2/lcp/LcpError.kt @@ -8,6 +8,7 @@ package org.readium.r2.lcp import java.net.SocketTimeoutException import java.util.* +import kotlinx.coroutines.CancellationException import org.readium.r2.lcp.service.NetworkException import org.readium.r2.shared.util.DebugError import org.readium.r2.shared.util.Error @@ -240,6 +241,7 @@ public sealed class LcpError( is LcpException -> e.error is NetworkException -> Network(e) is SocketTimeoutException -> Network(e) + is CancellationException -> throw e else -> Unknown(e) } } diff --git a/readium/lcp/src/main/java/org/readium/r2/lcp/service/LicensesService.kt b/readium/lcp/src/main/java/org/readium/r2/lcp/service/LicensesService.kt index 064615de94..39c89edbca 100644 --- a/readium/lcp/src/main/java/org/readium/r2/lcp/service/LicensesService.kt +++ b/readium/lcp/src/main/java/org/readium/r2/lcp/service/LicensesService.kt @@ -18,6 +18,7 @@ import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.MainScope +import kotlinx.coroutines.ensureActive import kotlinx.coroutines.launch import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.withContext @@ -84,12 +85,14 @@ internal class LicensesService( ) } - return try { - val container = createLicenseContainer(publicationFile, format.specification) - container.write(licenseDocument) - Try.success(Unit) - } catch (e: Exception) { - Try.failure(LcpError.wrap(e)) + return withContext(Dispatchers.IO) { + try { + val container = createLicenseContainer(publicationFile, format.specification) + container.write(licenseDocument) + Try.success(Unit) + } catch (e: Exception) { + Try.failure(LcpError.wrap(e)) + } } } @@ -97,6 +100,7 @@ internal class LicensesService( lcpl: File, onProgress: (Double) -> Unit ): Try { + coroutineContext.ensureActive() val bytes = try { lcpl.readBytes() } catch (e: Exception) { diff --git a/readium/lcp/src/main/java/org/readium/r2/lcp/service/NetworkService.kt b/readium/lcp/src/main/java/org/readium/r2/lcp/service/NetworkService.kt index a41116f05c..dd6f50e0bf 100644 --- a/readium/lcp/src/main/java/org/readium/r2/lcp/service/NetworkService.kt +++ b/readium/lcp/src/main/java/org/readium/r2/lcp/service/NetworkService.kt @@ -18,6 +18,7 @@ import java.net.URL import kotlin.math.round import kotlin.time.Duration import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ensureActive import kotlinx.coroutines.withContext import org.readium.r2.lcp.LcpError import org.readium.r2.lcp.LcpException @@ -95,6 +96,7 @@ internal class NetworkService { mediaType: MediaType? = null, onProgress: (Double) -> Unit ): MediaType? = withContext(Dispatchers.IO) { + coroutineContext.ensureActive() try { val connection = URL(url.toString()).openConnection() as HttpURLConnection if (connection.responseCode >= 400) { @@ -115,6 +117,7 @@ internal class NetworkService { val buf = ByteArray(2048) var n: Int while (-1 != input.read(buf).also { n = it }) { + coroutineContext.ensureActive() output.write(buf, 0, n) readLength += n