(String text, Charset ref)
| 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; |
no test coverage detected