Skip to content

Commit

Permalink
Various improvements in LcpService (readium#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
qnga authored Mar 29, 2024
1 parent af16ea9 commit 2786fb4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
2 changes: 2 additions & 0 deletions readium/lcp/src/main/java/org/readium/r2/lcp/LcpError.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -84,19 +85,22 @@ 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))
}
}
}

override suspend fun acquirePublication(
lcpl: File,
onProgress: (Double) -> Unit
): Try<LcpService.AcquiredPublication, LcpError> {
coroutineContext.ensureActive()
val bytes = try {
lcpl.readBytes()
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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

Expand Down

0 comments on commit 2786fb4

Please sign in to comment.