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") }