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;
import androidx.annotation.Nullable;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
@ -150,6 +152,7 @@ public class CharsetDetector {
* <code>null</code> 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

View File

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