From 6b046f87074732000532257d1acd97e2a9988bf2 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 2 Jan 2022 21:12:46 +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 --- .../legado/app/lib/icu4j/CharsetDetector.java | 3 +++ .../java/io/legado/app/utils/EncodingDetect.kt | 17 +++++++---------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/io/legado/app/lib/icu4j/CharsetDetector.java b/app/src/main/java/io/legado/app/lib/icu4j/CharsetDetector.java index 8d35b65a1..8432cb005 100644 --- a/app/src/main/java/io/legado/app/lib/icu4j/CharsetDetector.java +++ b/app/src/main/java/io/legado/app/lib/icu4j/CharsetDetector.java @@ -8,6 +8,8 @@ */ package io.legado.app.lib.icu4j; +import androidx.annotation.Nullable; + import java.io.IOException; import java.io.InputStream; import java.io.Reader; @@ -150,6 +152,7 @@ public class CharsetDetector { * null if there are no matches. * @stable ICU 3.4 */ + @Nullable public CharsetMatch detect() { // TODO: A better implementation would be to copy the detect loop from // detectAll(), and cut it short as soon as a match with a high confidence 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 de698c8e0..83f659fa9 100644 --- a/app/src/main/java/io/legado/app/utils/EncodingDetect.kt +++ b/app/src/main/java/io/legado/app/utils/EncodingDetect.kt @@ -46,10 +46,8 @@ object EncodingDetect { } fun getEncode(bytes: ByteArray): String { - val detector = CharsetDetector() - detector.setText(bytes) - val match = detector.detect() - return match.name + val match = CharsetDetector().setText(bytes).detect() + return match?.name ?: "UTF-8" } /** @@ -67,13 +65,12 @@ object EncodingDetect { return getEncode(tempByte) } - private fun getFileBytes(testFile: File?): ByteArray { - val fis: FileInputStream - val byteArray = ByteArray(2000) + private fun getFileBytes(file: File?): ByteArray { + val byteArray = ByteArray(8000) try { - fis = FileInputStream(testFile) - fis.read(byteArray) - fis.close() + FileInputStream(file).use { + it.read(byteArray) + } } catch (e: Exception) { System.err.println("Error: $e") }