From f6a98d143940ad2d1ccc57f27a97f552980f3cf1 Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 6 Jan 2022 14:15:31 +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 | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 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 acaa9b4da..98deec5a6 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 @@ -49,7 +49,6 @@ class TextFile(private val book: Book) { private fun analyze(pattern: Pattern?): ArrayList { val toc = arrayListOf() LocalBook.getBookInputStream(book).use { bis -> - val buffer = ByteArray(BUFFER_SIZE) var blockContent: String //加载章节 var curOffset: Long = 0 @@ -57,18 +56,23 @@ class TextFile(private val book: Book) { var blockPos = 0 //读取的长度 var length: Int + val buffer = ByteArray(BUFFER_SIZE) + var bufferStart = 0 //获取文件中的数据到buffer,直到没有数据为止 - while (bis.read(buffer).also { length = it } > 0) { + while (bis.read(buffer, bufferStart, BUFFER_SIZE - bufferStart) + .also { length = it } > 0 + ) { blockPos++ //如果存在Chapter if (pattern != null) { //将数据转换成String, 不能超过length - blockContent = String(buffer, 0, length, charset) + blockContent = String(buffer, 0, bufferStart + length, charset) val lastN = blockContent.lastIndexOf("\n") if (lastN > 0) { blockContent = blockContent.substring(0, lastN) val blockContentSize = blockContent.toByteArray(charset).size - bis.skip(-(length - blockContentSize).toLong()) + buffer.copyInto(buffer, 0, blockContentSize - bufferStart, length) + bufferStart = length + bufferStart - blockContentSize length = blockContentSize } //当前Block下使过的String的指针 @@ -199,7 +203,6 @@ class TextFile(private val book: Book) { System.runFinalization() } } - book.tocUrl = pattern?.pattern() ?: "" } for (i in toc.indices) { val bean = toc[i] @@ -212,7 +215,8 @@ class TextFile(private val book: Book) { System.gc() System.runFinalization() - + book.tocUrl = pattern?.pattern() ?: "" + book.save() return toc }