File tree Expand file tree Collapse file tree 3 files changed +15
-6
lines changed
readium/lcp/src/main/java/org/readium/r2/lcp Expand file tree Collapse file tree 3 files changed +15
-6
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ package org.readium.r2.lcp
8
8
9
9
import java.net.SocketTimeoutException
10
10
import java.util.*
11
+ import kotlinx.coroutines.CancellationException
11
12
import org.readium.r2.lcp.service.NetworkException
12
13
import org.readium.r2.shared.util.DebugError
13
14
import org.readium.r2.shared.util.Error
@@ -240,6 +241,7 @@ public sealed class LcpError(
240
241
is LcpException -> e.error
241
242
is NetworkException -> Network (e)
242
243
is SocketTimeoutException -> Network (e)
244
+ is CancellationException -> throw e
243
245
else -> Unknown (e)
244
246
}
245
247
}
Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ import kotlinx.coroutines.CancellationException
18
18
import kotlinx.coroutines.CoroutineScope
19
19
import kotlinx.coroutines.Dispatchers
20
20
import kotlinx.coroutines.MainScope
21
+ import kotlinx.coroutines.ensureActive
21
22
import kotlinx.coroutines.launch
22
23
import kotlinx.coroutines.suspendCancellableCoroutine
23
24
import kotlinx.coroutines.withContext
@@ -84,19 +85,22 @@ internal class LicensesService(
84
85
)
85
86
}
86
87
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
+ }
93
96
}
94
97
}
95
98
96
99
override suspend fun acquirePublication (
97
100
lcpl : File ,
98
101
onProgress : (Double ) -> Unit
99
102
): Try <LcpService .AcquiredPublication , LcpError > {
103
+ coroutineContext.ensureActive()
100
104
val bytes = try {
101
105
lcpl.readBytes()
102
106
} catch (e: Exception ) {
Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ import java.net.URL
18
18
import kotlin.math.round
19
19
import kotlin.time.Duration
20
20
import kotlinx.coroutines.Dispatchers
21
+ import kotlinx.coroutines.ensureActive
21
22
import kotlinx.coroutines.withContext
22
23
import org.readium.r2.lcp.LcpError
23
24
import org.readium.r2.lcp.LcpException
@@ -95,6 +96,7 @@ internal class NetworkService {
95
96
mediaType : MediaType ? = null,
96
97
onProgress : (Double ) -> Unit
97
98
): MediaType ? = withContext(Dispatchers .IO ) {
99
+ coroutineContext.ensureActive()
98
100
try {
99
101
val connection = URL (url.toString()).openConnection() as HttpURLConnection
100
102
if (connection.responseCode >= 400 ) {
@@ -115,6 +117,7 @@ internal class NetworkService {
115
117
val buf = ByteArray (2048 )
116
118
var n: Int
117
119
while (- 1 != input.read(buf).also { n = it }) {
120
+ coroutineContext.ensureActive()
118
121
output.write(buf, 0 , n)
119
122
readLength + = n
120
123
You can’t perform that action at this time.
0 commit comments