From cfbbb52d2c2dab5f3faa48ec0282d34b002fc37e Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 7 Jan 2022 17:53:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/model/localBook/TextFile.kt | 30 ++++--------------- 1 file changed, 6 insertions(+), 24 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 ddf8462c9..221d1ec7b 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 @@ -62,8 +62,6 @@ class TextFile(private val book: Book) { var blockContent: String //加载章节 var curOffset: Long = 0 - //block的个数 - var blockPos = 0 //读取的长度 var length: Int val buffer = ByteArray(bufferSize) @@ -77,7 +75,6 @@ class TextFile(private val book: Book) { while (bis.read(buffer, bufferStart, bufferSize - bufferStart) .also { length = it } > 0 ) { - blockPos++ var end = bufferStart + length for (i in bufferStart + length - 1 downTo 0) { if (buffer[i] == blank) { @@ -103,11 +100,7 @@ class TextFile(private val book: Book) { val lastStart = toc.lastOrNull()?.start ?: 0 if (curOffset + chapterLength - lastStart > 50000) { bis.close() - //移除不匹配的规则 - tocRules.removeFirstOrNull() - return tocRules.firstOrNull()?.let { - analyze(it.rule.toPattern(Pattern.MULTILINE)) - } ?: analyze() + return analyze() } //如果 seekPos == 0 && nextChapterPos != 0 表示当前block处前面有一段内容 //第一种情况一定是序章 第二种情况是上一个章节的内容 @@ -162,11 +155,7 @@ class TextFile(private val book: Book) { } if (seekPos == 0 && length > 50000) { bis.close() - //移除不匹配的规则 - tocRules.remove(tocRules.removeFirstOrNull()) - return tocRules.firstOrNull()?.let { - analyze(it.rule.toPattern(Pattern.MULTILINE)) - } ?: analyze() + return analyze() } //block的偏移点 @@ -175,12 +164,6 @@ class TextFile(private val book: Book) { //设置上一章的结尾 val lastChapter = toc.last() lastChapter.end = curOffset - - //当添加的block太多的时候,执行GC - if (blockPos % 15 == 0) { - System.gc() - System.runFinalization() - } } } for (i in toc.indices) { @@ -200,6 +183,10 @@ class TextFile(private val book: Book) { } private fun analyze(): ArrayList { + tocRules.removeFirstOrNull() + tocRules.firstOrNull()?.let { + return analyze(it.rule.toPattern(Pattern.MULTILINE)) + } val toc = arrayListOf() LocalBook.getBookInputStream(book).use { bis -> //加载章节 @@ -261,11 +248,6 @@ class TextFile(private val book: Book) { //block的偏移点 curOffset += length.toLong() - //当添加的block太多的时候,执行GC - if (blockPos % 15 == 0) { - System.gc() - System.runFinalization() - } } } for (i in toc.indices) {