record2021
gedoor 3 years ago
parent 8b558df5e7
commit 6b046f8707
  1. 3
      app/src/main/java/io/legado/app/lib/icu4j/CharsetDetector.java
  2. 17
      app/src/main/java/io/legado/app/utils/EncodingDetect.kt

@ -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

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

Loading…
Cancel
Save