File tree 28 files changed +92
-78
lines changed
pdfium/document/src/main/java/org/readium/adapter/pdfium/document
pspdfkit/document/src/main/java/org/readium/adapter/pspdfkit/document
main/java/org/readium/r2/shared
test/java/org/readium/r2/shared/util/format
streamer/src/test/java/org/readium/r2/streamer/parser/epub
28 files changed +92
-78
lines changed Original file line number Diff line number Diff line change @@ -72,7 +72,7 @@ public class PdfiumDocument(
72
72
core.getTableOfContents(document).map { it.toOutlineNode() }
73
73
}
74
74
75
- override suspend fun close () {
75
+ override fun close () {
76
76
tryOrLog {
77
77
core.closeDocument(document)
78
78
}
Original file line number Diff line number Diff line change @@ -115,7 +115,7 @@ public class PsPdfKitDocument(
115
115
document.outline.toOutlineNodes()
116
116
}
117
117
118
- override suspend fun close () {}
118
+ override fun close () {}
119
119
}
120
120
121
121
private fun List<OutlineElement>.toOutlineNodes (): List <PdfDocument .OutlineNode > =
Original file line number Diff line number Diff line change @@ -199,7 +199,7 @@ public class Publication(
199
199
/* *
200
200
* Closes any opened resource associated with the [Publication], including services.
201
201
*/
202
- override suspend fun close () {
202
+ override fun close () {
203
203
container.close()
204
204
services.close()
205
205
}
Original file line number Diff line number Diff line change @@ -11,12 +11,12 @@ package org.readium.r2.shared.publication
11
11
import kotlin.reflect.KClass
12
12
import org.readium.r2.shared.InternalReadiumApi
13
13
import org.readium.r2.shared.extensions.tryOrLog
14
- import org.readium.r2.shared.util.SuspendingCloseable
14
+ import org.readium.r2.shared.util.Closeable
15
15
16
16
/* *
17
17
* Holds [Publication.Service] instances for a [Publication].
18
18
*/
19
- public interface PublicationServicesHolder : SuspendingCloseable {
19
+ public interface PublicationServicesHolder : Closeable {
20
20
/* *
21
21
* Returns the first publication service that is an instance of [serviceType].
22
22
*/
@@ -37,7 +37,7 @@ internal class ListPublicationServicesHolder(
37
37
override fun <T : Publication .Service > findServices (serviceType : KClass <T >): List <T > =
38
38
services.filterIsInstance(serviceType.java)
39
39
40
- override suspend fun close () {
40
+ override fun close () {
41
41
for (service in services) {
42
42
tryOrLog { service.close() }
43
43
}
Original file line number Diff line number Diff line change @@ -12,8 +12,8 @@ import org.readium.r2.shared.ExperimentalReadiumApi
12
12
import org.readium.r2.shared.publication.LocatorCollection
13
13
import org.readium.r2.shared.publication.Publication
14
14
import org.readium.r2.shared.publication.ServiceFactory
15
+ import org.readium.r2.shared.util.Closeable
15
16
import org.readium.r2.shared.util.Error
16
- import org.readium.r2.shared.util.SuspendingCloseable
17
17
import org.readium.r2.shared.util.Try
18
18
import org.readium.r2.shared.util.data.ReadError
19
19
@@ -134,7 +134,7 @@ public var Publication.ServicesBuilder.searchServiceFactory: ServiceFactory?
134
134
* Iterates through search results.
135
135
*/
136
136
@ExperimentalReadiumApi
137
- public interface SearchIterator : SuspendingCloseable {
137
+ public interface SearchIterator : Closeable {
138
138
139
139
/* *
140
140
* Number of matches for this search, if known.
@@ -157,7 +157,7 @@ public interface SearchIterator : SuspendingCloseable {
157
157
* Closes any resources allocated for the search query, such as a cursor.
158
158
* To be called when the user dismisses the search.
159
159
*/
160
- override suspend fun close () {}
160
+ override fun close () {}
161
161
162
162
/* *
163
163
* Performs the given operation on each result page of this [SearchIterator].
Original file line number Diff line number Diff line change @@ -17,23 +17,11 @@ public interface Closeable {
17
17
public fun close ()
18
18
}
19
19
20
- /* *
21
- * A [SuspendingCloseable] is an object holding closeable resources, such as open files or streams.
22
- */
23
- public interface SuspendingCloseable {
24
-
25
- /* *
26
- * Closes this object and releases any resources associated with it.
27
- * If the object is already closed then invoking this method has no effect.
28
- */
29
- public suspend fun close ()
30
- }
31
-
32
20
/* *
33
21
* Executes the given block function on this resource and then closes it down correctly whether
34
22
* an exception is thrown or not.
35
23
*/
36
- public suspend inline fun <T : SuspendingCloseable ?, R > T.use (block : (T ) -> R ): R {
24
+ public inline fun <T : Closeable ?, R > T.use (block : (T ) -> R ): R {
37
25
var exception: Throwable ? = null
38
26
try {
39
27
return block(this )
Original file line number Diff line number Diff line change 6
6
7
7
package org.readium.r2.shared.util.asset
8
8
9
- import org.readium.r2.shared.util.SuspendingCloseable
9
+ import org.readium.r2.shared.util.Closeable
10
10
import org.readium.r2.shared.util.data.Container
11
11
import org.readium.r2.shared.util.format.Format
12
12
import org.readium.r2.shared.util.resource.Resource
13
13
14
14
/* *
15
15
* An asset which is either a single resource or a container that holds multiple resources.
16
16
*/
17
- public sealed class Asset : SuspendingCloseable {
17
+ public sealed class Asset : Closeable {
18
18
19
19
/* *
20
20
* Format of the asset.
@@ -33,7 +33,7 @@ public class ContainerAsset(
33
33
public val container : Container <Resource >
34
34
) : Asset() {
35
35
36
- override suspend fun close () {
36
+ override fun close () {
37
37
container.close()
38
38
}
39
39
}
@@ -49,7 +49,7 @@ public class ResourceAsset(
49
49
public val resource : Resource
50
50
) : Asset() {
51
51
52
- override suspend fun close () {
52
+ override fun close () {
53
53
resource.close()
54
54
}
55
55
}
Original file line number Diff line number Diff line change @@ -16,7 +16,6 @@ import kotlinx.coroutines.sync.withLock
16
16
import org.readium.r2.shared.InternalReadiumApi
17
17
import org.readium.r2.shared.util.Closeable
18
18
import org.readium.r2.shared.util.MemoryObserver
19
- import org.readium.r2.shared.util.SuspendingCloseable
20
19
import org.readium.r2.shared.util.Try
21
20
22
21
/* *
@@ -25,7 +24,7 @@ import org.readium.r2.shared.util.Try
25
24
* It implements [MemoryObserver] to flush unused in-memory objects when necessary.
26
25
*/
27
26
@InternalReadiumApi
28
- public interface Cache <V > : SuspendingCloseable , MemoryObserver {
27
+ public interface Cache <V > : Closeable , MemoryObserver {
29
28
/* *
30
29
* Performs an atomic [block] transaction on this cache.
31
30
*/
@@ -111,11 +110,13 @@ public class InMemoryCache<V> : Cache<V> {
111
110
}
112
111
}
113
112
114
- override suspend fun close () {
115
- transaction {
116
- for ((_, value) in values) {
117
- (value as ? Closeable )?.close()
118
- (value as ? SuspendingCloseable )?.close()
113
+ @OptIn(DelicateCoroutinesApi ::class )
114
+ override fun close () {
115
+ GlobalScope .launch {
116
+ transaction {
117
+ for ((_, value) in values) {
118
+ (value as ? Closeable )?.close()
119
+ }
119
120
}
120
121
}
121
122
}
Original file line number Diff line number Diff line change @@ -47,7 +47,7 @@ public class ContentResource(
47
47
48
48
override val sourceUrl: AbsoluteUrl ? = uri.toUrl() as ? AbsoluteUrl
49
49
50
- override suspend fun close () {
50
+ override fun close () {
51
51
}
52
52
53
53
override suspend fun properties (): Try <Resource .Properties , ReadError > {
Original file line number Diff line number Diff line change @@ -58,7 +58,7 @@ internal class CachingReadable(
58
58
}
59
59
}
60
60
61
- override suspend fun close () {}
61
+ override fun close () {}
62
62
}
63
63
64
64
internal class CachingContainer (
@@ -81,7 +81,7 @@ internal class CachingContainer(
81
81
return blobContext
82
82
}
83
83
84
- override suspend fun close () {
84
+ override fun close () {
85
85
cache.forEach { it.value.close() }
86
86
cache.clear()
87
87
}
Original file line number Diff line number Diff line change 6
6
7
7
package org.readium.r2.shared.util.data
8
8
9
+ import kotlin.io.use
9
10
import org.readium.r2.shared.InternalReadiumApi
10
11
import org.readium.r2.shared.util.AbsoluteUrl
11
- import org.readium.r2.shared.util.SuspendingCloseable
12
+ import org.readium.r2.shared.util.Closeable
12
13
import org.readium.r2.shared.util.Try
13
14
import org.readium.r2.shared.util.Url
14
15
import org.readium.r2.shared.util.use
15
16
16
17
/* *
17
18
* A container provides access to a list of [Readable] entries.
18
19
*/
19
- public interface Container <out E : Readable > : Iterable <Url >, SuspendingCloseable {
20
+ public interface Container <out E : Readable > : Iterable <Url >, Closeable {
20
21
21
22
/* *
22
23
* Direct source to this container, when available.
@@ -45,7 +46,7 @@ public class EmptyContainer<E : Readable> :
45
46
46
47
override fun get (url : Url ): E ? = null
47
48
48
- override suspend fun close () {}
49
+ override fun close () {}
49
50
}
50
51
51
52
/* *
@@ -69,7 +70,7 @@ public class CompositeContainer<E : Readable>(
69
70
override fun get (url : Url ): E ? =
70
71
containers.firstNotNullOfOrNull { it[url] }
71
72
72
- override suspend fun close () {
73
+ override fun close () {
73
74
containers.forEach { it.close() }
74
75
}
75
76
}
Original file line number Diff line number Diff line change @@ -8,18 +8,18 @@ package org.readium.r2.shared.util.data
8
8
9
9
import java.io.IOException
10
10
import org.readium.r2.shared.InternalReadiumApi
11
+ import org.readium.r2.shared.util.Closeable
11
12
import org.readium.r2.shared.util.DebugError
12
13
import org.readium.r2.shared.util.Error
13
14
import org.readium.r2.shared.util.ErrorException
14
- import org.readium.r2.shared.util.SuspendingCloseable
15
15
import org.readium.r2.shared.util.ThrowableError
16
16
import org.readium.r2.shared.util.Try
17
17
import org.readium.r2.shared.util.getOrElse
18
18
19
19
/* *
20
20
* Acts as a proxy to an actual data source by handling read access.
21
21
*/
22
- public interface Readable : SuspendingCloseable {
22
+ public interface Readable : Closeable {
23
23
24
24
/* *
25
25
* Returns data length from metadata if available, or calculated from reading the bytes otherwise.
@@ -120,7 +120,7 @@ private class BorrowedReadable(
120
120
private val readable : Readable
121
121
) : Readable by readable {
122
122
123
- override suspend fun close () {
123
+ override fun close () {
124
124
// Do nothing
125
125
}
126
126
}
Original file line number Diff line number Diff line change @@ -30,7 +30,7 @@ public class DirectoryContainer(
30
30
?.let { File (root, it) }
31
31
?.let { FileResource (it) }
32
32
33
- override suspend fun close () {}
33
+ override fun close () {}
34
34
35
35
public companion object {
36
36
Original file line number Diff line number Diff line change @@ -13,7 +13,10 @@ import java.io.FileNotFoundException
13
13
import java.io.IOException
14
14
import java.io.RandomAccessFile
15
15
import java.nio.channels.Channels
16
+ import kotlinx.coroutines.DelicateCoroutinesApi
16
17
import kotlinx.coroutines.Dispatchers
18
+ import kotlinx.coroutines.GlobalScope
19
+ import kotlinx.coroutines.launch
17
20
import kotlinx.coroutines.withContext
18
21
import org.readium.r2.shared.InternalReadiumApi
19
22
import org.readium.r2.shared.extensions.*
@@ -58,11 +61,14 @@ public class FileResource(
58
61
return Try .success(properties)
59
62
}
60
63
61
- override suspend fun close () {
62
- withContext(Dispatchers .IO ) {
63
- if (::randomAccessFile.isLazyInitialized) {
64
- randomAccessFile.onSuccess {
65
- tryOrLog { it.close() }
64
+ @OptIn(DelicateCoroutinesApi ::class )
65
+ override fun close () {
66
+ if (::randomAccessFile.isLazyInitialized) {
67
+ GlobalScope .launch {
68
+ withContext(Dispatchers .IO ) {
69
+ randomAccessFile.onSuccess {
70
+ tryOrLog { it.close() }
71
+ }
66
72
}
67
73
}
68
74
}
Original file line number Diff line number Diff line change @@ -39,5 +39,5 @@ public class HttpContainer(
39
39
}
40
40
}
41
41
42
- override suspend fun close () {}
42
+ override fun close () {}
43
43
}
Original file line number Diff line number Diff line change @@ -61,7 +61,7 @@ public class HttpResource(
61
61
}
62
62
}
63
63
64
- override suspend fun close () {}
64
+ override fun close () {}
65
65
66
66
override suspend fun read (range : LongRange ? ): Try <ByteArray , ReadError > = withContext(
67
67
Dispatchers .IO
Original file line number Diff line number Diff line change @@ -21,7 +21,7 @@ import org.readium.r2.shared.publication.Publication
21
21
import org.readium.r2.shared.publication.PublicationServicesHolder
22
22
import org.readium.r2.shared.publication.ReadingProgression
23
23
import org.readium.r2.shared.publication.services.cacheService
24
- import org.readium.r2.shared.util.SuspendingCloseable
24
+ import org.readium.r2.shared.util.Closeable
25
25
import org.readium.r2.shared.util.Url
26
26
import org.readium.r2.shared.util.cache.Cache
27
27
import org.readium.r2.shared.util.cache.getOrTryPut
@@ -72,7 +72,7 @@ private class CachingPdfDocumentFactory<T : PdfDocument>(
72
72
/* *
73
73
* Represents a PDF document.
74
74
*/
75
- public interface PdfDocument : SuspendingCloseable {
75
+ public interface PdfDocument : Closeable {
76
76
77
77
/* *
78
78
* Permanent identifier based on the contents of the file at the time it was originally
Original file line number Diff line number Diff line change @@ -29,7 +29,7 @@ public class FallbackResource(
29
29
override suspend fun read (range : LongRange ? ): Try <ByteArray , ReadError > =
30
30
withResource { read(range) }
31
31
32
- override suspend fun close () {
32
+ override fun close () {
33
33
if (::_resource .isInitialized) {
34
34
_resource .close()
35
35
}
Original file line number Diff line number Diff line change @@ -60,7 +60,7 @@ public class InMemoryResource(
60
60
return _bytes .map { it.read(range) }
61
61
}
62
62
63
- override suspend fun close () {}
63
+ override fun close () {}
64
64
65
65
override fun toString (): String =
66
66
" ${javaClass.simpleName} (${runBlocking { length() }} bytes)"
Original file line number Diff line number Diff line change @@ -37,7 +37,7 @@ public open class LazyResource(
37
37
override suspend fun read (range : LongRange ? ): Try <ByteArray , ReadError > =
38
38
resource().read(range)
39
39
40
- override suspend fun close () {
40
+ override fun close () {
41
41
if (::_resource .isInitialized) {
42
42
_resource .close()
43
43
}
Original file line number Diff line number Diff line change @@ -62,7 +62,7 @@ public class FailureResource(
62
62
override suspend fun properties (): Try <Resource .Properties , ReadError > = Try .failure(error)
63
63
override suspend fun length (): Try <Long , ReadError > = Try .failure(error)
64
64
override suspend fun read (range : LongRange ? ): Try <ByteArray , ReadError > = Try .failure(error)
65
- override suspend fun close () {}
65
+ override fun close () {}
66
66
67
67
override fun toString (): String =
68
68
" ${javaClass.simpleName} ($error )"
@@ -81,7 +81,7 @@ private class BorrowedResource(
81
81
private val resource : Resource
82
82
) : Resource by resource {
83
83
84
- override suspend fun close () {
84
+ override fun close () {
85
85
// Do nothing
86
86
}
87
87
}
Original file line number Diff line number Diff line change @@ -25,7 +25,7 @@ public class SingleResourceContainer(
25
25
return resource.borrow()
26
26
}
27
27
28
- override suspend fun close () {
28
+ override fun close () {
29
29
resource.close()
30
30
}
31
31
}
You can’t perform that action at this time.
0 commit comments