Skip to content

Commit 2786fb4

Browse files
authored
Various improvements in LcpService (readium#483)
1 parent af16ea9 commit 2786fb4

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

readium/lcp/src/main/java/org/readium/r2/lcp/LcpError.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package org.readium.r2.lcp
88

99
import java.net.SocketTimeoutException
1010
import java.util.*
11+
import kotlinx.coroutines.CancellationException
1112
import org.readium.r2.lcp.service.NetworkException
1213
import org.readium.r2.shared.util.DebugError
1314
import org.readium.r2.shared.util.Error
@@ -240,6 +241,7 @@ public sealed class LcpError(
240241
is LcpException -> e.error
241242
is NetworkException -> Network(e)
242243
is SocketTimeoutException -> Network(e)
244+
is CancellationException -> throw e
243245
else -> Unknown(e)
244246
}
245247
}

readium/lcp/src/main/java/org/readium/r2/lcp/service/LicensesService.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import kotlinx.coroutines.CancellationException
1818
import kotlinx.coroutines.CoroutineScope
1919
import kotlinx.coroutines.Dispatchers
2020
import kotlinx.coroutines.MainScope
21+
import kotlinx.coroutines.ensureActive
2122
import kotlinx.coroutines.launch
2223
import kotlinx.coroutines.suspendCancellableCoroutine
2324
import kotlinx.coroutines.withContext
@@ -84,19 +85,22 @@ internal class LicensesService(
8485
)
8586
}
8687

87-
return try {
88-
val container = createLicenseContainer(publicationFile, format.specification)
89-
container.write(licenseDocument)
90-
Try.success(Unit)
91-
} catch (e: Exception) {
92-
Try.failure(LcpError.wrap(e))
88+
return withContext(Dispatchers.IO) {
89+
try {
90+
val container = createLicenseContainer(publicationFile, format.specification)
91+
container.write(licenseDocument)
92+
Try.success(Unit)
93+
} catch (e: Exception) {
94+
Try.failure(LcpError.wrap(e))
95+
}
9396
}
9497
}
9598

9699
override suspend fun acquirePublication(
97100
lcpl: File,
98101
onProgress: (Double) -> Unit
99102
): Try<LcpService.AcquiredPublication, LcpError> {
103+
coroutineContext.ensureActive()
100104
val bytes = try {
101105
lcpl.readBytes()
102106
} catch (e: Exception) {

readium/lcp/src/main/java/org/readium/r2/lcp/service/NetworkService.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import java.net.URL
1818
import kotlin.math.round
1919
import kotlin.time.Duration
2020
import kotlinx.coroutines.Dispatchers
21+
import kotlinx.coroutines.ensureActive
2122
import kotlinx.coroutines.withContext
2223
import org.readium.r2.lcp.LcpError
2324
import org.readium.r2.lcp.LcpException
@@ -95,6 +96,7 @@ internal class NetworkService {
9596
mediaType: MediaType? = null,
9697
onProgress: (Double) -> Unit
9798
): MediaType? = withContext(Dispatchers.IO) {
99+
coroutineContext.ensureActive()
98100
try {
99101
val connection = URL(url.toString()).openConnection() as HttpURLConnection
100102
if (connection.responseCode >= 400) {
@@ -115,6 +117,7 @@ internal class NetworkService {
115117
val buf = ByteArray(2048)
116118
var n: Int
117119
while (-1 != input.read(buf).also { n = it }) {
120+
coroutineContext.ensureActive()
118121
output.write(buf, 0, n)
119122
readLength += n
120123

0 commit comments

Comments
 (0)