From 80b7d14af195147b52e38765fdb3adf4b9030dd4 Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 6 Jan 2024 17:09:38 -0500 Subject: [PATCH] Fix RAR loading Closes #10302 --- .../tachiyomi/ui/reader/loader/RarPageLoader.kt | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/RarPageLoader.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/RarPageLoader.kt index 056319d4e6..ad513ec93b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/RarPageLoader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/RarPageLoader.kt @@ -10,6 +10,7 @@ import java.io.File import java.io.InputStream import java.io.PipedInputStream import java.io.PipedOutputStream +import java.util.concurrent.Executors /** * Loader used to load a chapter from a .rar or .cbr file. @@ -20,13 +21,18 @@ internal class RarPageLoader(file: File) : PageLoader() { override var isLocal: Boolean = true + /** + * Pool for copying compressed files to an input stream. + */ + private val pool = Executors.newFixedThreadPool(1) + override suspend fun getPages(): List { return rar.fileHeaders.asSequence() .filter { !it.isDirectory && ImageUtil.isImage(it.fileName) { rar.getInputStream(it) } } .sortedWith { f1, f2 -> f1.fileName.compareToCaseInsensitiveNaturalOrder(f2.fileName) } .mapIndexed { i, header -> ReaderPage(i).apply { - stream = { getStream(rar, header) } + stream = { getStream(header) } status = Page.State.READY } } @@ -40,15 +46,16 @@ internal class RarPageLoader(file: File) : PageLoader() { override fun recycle() { super.recycle() rar.close() + pool.shutdown() } /** * Returns an input stream for the given [header]. */ - private fun getStream(rar: Archive, header: FileHeader): InputStream { + private fun getStream(header: FileHeader): InputStream { val pipeIn = PipedInputStream() val pipeOut = PipedOutputStream(pipeIn) - synchronized(this) { + pool.execute { try { pipeOut.use { rar.extractFile(header, it)