From 7c81d2d3a933cb0e6a1cb5ae57b742602e6dc580 Mon Sep 17 00:00:00 2001 From: Horis <8674809+821938089@users.noreply.github.com> Date: Thu, 17 Apr 2025 11:40:34 +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 --- .../java/io/legado/app/model/ReadManga.kt | 8 +++++-- .../io/legado/app/utils/EncodingDetect.kt | 21 ++++++++++++++----- .../main/res/layout/item_book_manga_page.xml | 2 +- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/ReadManga.kt b/app/src/main/java/io/legado/app/model/ReadManga.kt index b7701f3cc..5e7044084 100644 --- a/app/src/main/java/io/legado/app/model/ReadManga.kt +++ b/app/src/main/java/io/legado/app/model/ReadManga.kt @@ -250,9 +250,13 @@ object ReadManga : CoroutineScope by MainScope() { curMangaChapter?.let { curFinish = true items.addAll(it.pages) - durChapterPos = durChapterPos.coerceIn(0, it.imageCount - 1) + durChapterPos = if (it.imageCount > 0) { + durChapterPos.coerceIn(0, it.imageCount - 1) + } else { + 0 + } pos += durChapterPos - if (!AppConfig.hideMangaTitle) { + if (!AppConfig.hideMangaTitle && it.imageCount > 0) { pos++ } } diff --git a/app/src/main/java/io/legado/app/utils/EncodingDetect.kt b/app/src/main/java/io/legado/app/utils/EncodingDetect.kt index 290c44911..23eceb85a 100644 --- a/app/src/main/java/io/legado/app/utils/EncodingDetect.kt +++ b/app/src/main/java/io/legado/app/utils/EncodingDetect.kt @@ -4,7 +4,6 @@ import android.text.TextUtils import io.legado.app.lib.icu4j.CharsetDetector import org.jsoup.Jsoup import java.io.File -import java.io.FileInputStream /** * 自动获取文件的编码 @@ -70,18 +69,30 @@ object EncodingDetect { */ fun getEncode(file: File): String { val tempByte = getFileBytes(file) + if (tempByte.isEmpty()) { + return "UTF-8" + } return getEncode(tempByte) } - private fun getFileBytes(file: File?): ByteArray { + private fun getFileBytes(file: File): ByteArray { val byteArray = ByteArray(8000) + var pos = 0 try { - FileInputStream(file).use { - it.read(byteArray) + file.inputStream().buffered().use { + while (pos < byteArray.size) { + val n = it.read(byteArray, pos, 1) + if (n == -1) { + break + } + if (byteArray[pos] < 0) { + pos++ + } + } } } catch (e: Exception) { System.err.println("Error: $e") } - return byteArray + return byteArray.copyOf(pos) } } \ No newline at end of file diff --git a/app/src/main/res/layout/item_book_manga_page.xml b/app/src/main/res/layout/item_book_manga_page.xml index b6de8ad5c..7f9565b8e 100644 --- a/app/src/main/res/layout/item_book_manga_page.xml +++ b/app/src/main/res/layout/item_book_manga_page.xml @@ -11,7 +11,7 @@