From d118a2551ee8bb90f15ab8773534693dabb506b9 Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 30 Dec 2021 21:35:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E4=B9=A6=E7=B1=8D=E6=97=A0?= =?UTF-8?q?=E6=9D=83=E9=99=90=E5=88=99=E4=BF=9D=E5=AD=98=E5=88=B0=E8=87=AA?= =?UTF-8?q?=E5=B7=B1=E9=80=89=E5=AE=9A=E7=9A=84=E6=96=87=E4=BB=B6=E5=A4=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/model/localBook/TextFile.kt | 48 +++++++------------ 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/localBook/TextFile.kt b/app/src/main/java/io/legado/app/model/localBook/TextFile.kt index 2be6c645f..935db781a 100644 --- a/app/src/main/java/io/legado/app/model/localBook/TextFile.kt +++ b/app/src/main/java/io/legado/app/model/localBook/TextFile.kt @@ -1,13 +1,15 @@ package io.legado.app.model.localBook import android.net.Uri -import androidx.documentfile.provider.DocumentFile import io.legado.app.data.appDb import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.TxtTocRule import io.legado.app.help.DefaultData -import io.legado.app.utils.* +import io.legado.app.utils.EncodingDetect +import io.legado.app.utils.MD5Utils +import io.legado.app.utils.StringUtils +import io.legado.app.utils.isContentScheme import splitties.init.appCtx import java.io.File import java.io.FileInputStream @@ -24,18 +26,19 @@ class TextFile(private val book: Book) { @Throws(FileNotFoundException::class) fun getChapterList(): ArrayList { - val bookFile = getBookFile(book) - if (book.charset == null) { - book.charset = EncodingDetect.getEncode(bookFile) - } - charset = book.fileCharset() - val rulePattern = if (book.tocUrl.isNotEmpty()) { - Pattern.compile(book.tocUrl, Pattern.MULTILINE) - } else { - tocRules.addAll(getTocRules()) - null - } return getBookInputStream(book).use { + val buffer = ByteArray(BUFFER_SIZE) + it.read(buffer, 0, buffer.size) + if (book.charset == null) { + book.charset = EncodingDetect.getEncode(buffer) + } + charset = book.fileCharset() + val rulePattern = if (book.tocUrl.isNotEmpty()) { + Pattern.compile(book.tocUrl, Pattern.MULTILINE) + } else { + tocRules.addAll(getTocRules()) + null + } analyze(it, book, rulePattern) } } @@ -276,25 +279,6 @@ class TextFile(private val book: Book) { return FileInputStream(File(book.bookUrl)) } - private fun getBookFile(book: Book): File { - if (book.bookUrl.isContentScheme()) { - val uri = Uri.parse(book.bookUrl) - val bookFile = LocalBook.cacheFolder.getFile(book.originName) - val doc = DocumentFile.fromSingleUri(appCtx, uri)!! - if (bookFile.exists() && bookFile.lastModified() >= doc.lastModified()) { - return bookFile - } - if (!bookFile.exists()) { - bookFile.createNewFile() - DocumentUtils.readBytes(appCtx, uri).let { - bookFile.writeBytes(it) - } - } - return bookFile - } - return File(book.bookUrl) - } - private fun getTocRules(): List { var rules = appDb.txtTocRuleDao.enabled if (rules.isEmpty()) {