MCPcopy Create free account
hub / github.com/M66B/FairEmail / detect

Method detect

app/src/main/java/eu/faircode/email/CharsetHelper.java:222–263  ·  view source on GitHub ↗
(String text, Charset ref)

Source from the content-addressed store, hash-verified

220 }
221
222 public static Charset detect(String text, Charset ref) {
223 if (text == null)
224 return null;
225
226 try {
227 byte[] octets = text.getBytes(StandardCharsets.ISO_8859_1);
228
229 byte[] sample;
230 if (octets.length < MAX_SAMPLE_SIZE)
231 sample = octets;
232 else {
233 sample = new byte[MAX_SAMPLE_SIZE];
234 System.arraycopy(octets, 0, sample, 0, MAX_SAMPLE_SIZE);
235 }
236
237 Log.i("compact_enc_det sample=" + sample.length);
238 DetectResult detected = jni_detect_charset(sample,
239 ref == null ? StandardCharsets.ISO_8859_1.name() : ref.name(),
240 Locale.getDefault().getLanguage());
241
242 if (TextUtils.isEmpty(detected.charset)) {
243 Log.e("compact_enc_det result=" + detected);
244 return null;
245 } else if (COMMON.contains(detected.charset) || LESS_COMMON.contains(detected.charset))
246 Log.i("compact_enc_det result=" + detected);
247 else if ("UTF-7".equals(detected.charset))
248 return null;
249 else if ("GB18030".equals(detected.charset)) {
250 boolean chinese = Locale.getDefault().getLanguage().equals(CHINESE);
251 // https://github.com/google/compact_enc_det/issues/8
252 Log.e("compact_enc_det result=" + detected + " chinese=" + chinese);
253 if (!chinese)
254 return null;
255 } else
256 Log.e("compact_enc_det result=" + detected);
257
258 return Charset.forName(detected.charset);
259 } catch (Throwable ex) {
260 Log.w(ex);
261 return null;
262 }
263 }
264
265 private static class DetectResult {
266 String charset;

Callers 3

fixEncodingMethod · 0.95
getHtmlMethod · 0.95
getStructureMethod · 0.95

Calls 11

iMethod · 0.95
jni_detect_charsetMethod · 0.95
eMethod · 0.95
wMethod · 0.95
getLanguageMethod · 0.80
getDefaultMethod · 0.80
getBytesMethod · 0.45
nameMethod · 0.45
isEmptyMethod · 0.45
containsMethod · 0.45
equalsMethod · 0.45

Tested by

no test coverage detected