From 65f7f919bac4e60f462107184ec471b70cd570bf Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 31 Dec 2021 20:06:45 +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 | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 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 b875dafa9..e5476979d 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 @@ -24,18 +24,12 @@ import java.util.regex.Pattern class TextFile(private val book: Book) { private val tocRules = arrayListOf() - private lateinit var charset: Charset + private var charset: Charset = book.fileCharset() @Throws(FileNotFoundException::class) fun getChapterList(): ArrayList { return getBookFD(book).let { fd -> try { - val buffer = ByteArray(BUFFER_SIZE) - Os.read(fd, 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 { @@ -59,11 +53,20 @@ class TextFile(private val book: Book) { val toc = arrayListOf() var tocRule: TxtTocRule? = null val buffer = ByteArray(BUFFER_SIZE) - val rulePattern = pattern ?: let { - Os.lseek(bookFd, 0, SEEK_SET) + var blockContent = "" + if (book.charset == null) { val length = Os.read(bookFd, buffer, 0, BUFFER_SIZE) - val content = String(buffer, 0, length, charset) - tocRule = getTocRule(content) + book.charset = EncodingDetect.getEncode(buffer) + blockContent = String(buffer, 0, length, charset) + charset = book.fileCharset() + } + val rulePattern = pattern ?: let { + if (blockContent.isEmpty()) { + Os.lseek(bookFd, 0, SEEK_SET) + val length = Os.read(bookFd, buffer, 0, BUFFER_SIZE) + blockContent = String(buffer, 0, length, charset) + } + tocRule = getTocRule(blockContent) tocRule?.let { Pattern.compile(it.rule, Pattern.MULTILINE) } @@ -84,7 +87,7 @@ class TextFile(private val book: Book) { //如果存在Chapter if (rulePattern != null) { //将数据转换成String, 不能超过length - var blockContent = String(buffer, 0, length, charset) + blockContent = String(buffer, 0, length, charset) val lastN = blockContent.lastIndexOf("\n") if (lastN > 0) { blockContent = blockContent.substring(0, lastN)