Experiment: search attachment content

This commit is contained in:
M66B
2023-08-09 18:54:37 +02:00
parent 9276018795
commit b03f0ebe6b
10 changed files with 199 additions and 21 deletions

View File

@@ -225,19 +225,7 @@ public class CharsetHelper {
try {
byte[] octets = text.getBytes(StandardCharsets.ISO_8859_1);
byte[] sample;
if (octets.length < MAX_SAMPLE_SIZE)
sample = octets;
else {
sample = new byte[MAX_SAMPLE_SIZE];
System.arraycopy(octets, 0, sample, 0, MAX_SAMPLE_SIZE);
}
Log.i("compact_enc_det sample=" + sample.length);
DetectResult detected = jni_detect_charset(sample,
ref == null ? StandardCharsets.ISO_8859_1.name() : ref.name(),
Locale.getDefault().getLanguage());
DetectResult detected = _detect(octets, ref);
if (TextUtils.isEmpty(detected.charset)) {
Log.e("compact_enc_det result=" + detected);
@@ -262,7 +250,34 @@ public class CharsetHelper {
}
}
private static class DetectResult {
public static Charset detect(byte[] octets, Charset ref) {
try {
DetectResult detected = _detect(octets, ref);
if (TextUtils.isEmpty(detected.charset))
return null;
return Charset.forName(detected.charset);
} catch (Throwable ex) {
Log.w(ex);
return null;
}
}
private static DetectResult _detect(byte[] octets, Charset ref) {
byte[] sample;
if (octets.length < MAX_SAMPLE_SIZE)
sample = octets;
else {
sample = new byte[MAX_SAMPLE_SIZE];
System.arraycopy(octets, 0, sample, 0, MAX_SAMPLE_SIZE);
}
Log.i("compact_enc_det sample=" + sample.length);
return jni_detect_charset(sample,
ref == null ? StandardCharsets.ISO_8859_1.name() : ref.name(),
Locale.getDefault().getLanguage());
}
public static class DetectResult {
String charset;
int sample_size;
int bytes_consumed;