This commit is contained in:
gedoor 2022-01-02 21:12:46 +08:00
parent 8b558df5e7
commit 6b046f8707
2 changed files with 10 additions and 10 deletions

View File

@ -8,6 +8,8 @@
*/ */
package io.legado.app.lib.icu4j; package io.legado.app.lib.icu4j;
import androidx.annotation.Nullable;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.Reader; import java.io.Reader;
@ -150,6 +152,7 @@ public class CharsetDetector {
* <code>null</code> if there are no matches. * <code>null</code> if there are no matches.
* @stable ICU 3.4 * @stable ICU 3.4
*/ */
@Nullable
public CharsetMatch detect() { public CharsetMatch detect() {
// TODO: A better implementation would be to copy the detect loop from // 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 // detectAll(), and cut it short as soon as a match with a high confidence

View File

@ -46,10 +46,8 @@ object EncodingDetect {
} }
fun getEncode(bytes: ByteArray): String { fun getEncode(bytes: ByteArray): String {
val detector = CharsetDetector() val match = CharsetDetector().setText(bytes).detect()
detector.setText(bytes) return match?.name ?: "UTF-8"
val match = detector.detect()
return match.name
} }
/** /**
@ -67,13 +65,12 @@ object EncodingDetect {
return getEncode(tempByte) return getEncode(tempByte)
} }
private fun getFileBytes(testFile: File?): ByteArray { private fun getFileBytes(file: File?): ByteArray {
val fis: FileInputStream val byteArray = ByteArray(8000)
val byteArray = ByteArray(2000)
try { try {
fis = FileInputStream(testFile) FileInputStream(file).use {
fis.read(byteArray) it.read(byteArray)
fis.close() }
} catch (e: Exception) { } catch (e: Exception) {
System.err.println("Error: $e") System.err.println("Error: $e")
} }