Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various improvements in LcpService #483

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading