Auto fix incorrect UTF-16

This commit is contained in:
M66B 2020-11-19 08:39:10 +01:00
parent c2cf366e30
commit 16e890e0a1
1 changed files with 18 additions and 0 deletions

View File

@ -1790,6 +1790,24 @@ public class MessageHelper {
result = HtmlHelper.flow(result);
result = "<div x-plain=\"true\">" + HtmlHelper.formatPre(result) + "</div>";
} else if (h.part.isMimeType("text/html")) {
// Fix incorrect UTF16
if (charset != null)
try {
Charset c = Charset.forName(charset);
if (CHARSET16.contains(c)) {
Charset detected = CharsetHelper.detect(result);
if (!CHARSET16.contains(detected))
Log.e(new Throwable("Charset=" + c + " detected=" + detected));
if (StandardCharsets.US_ASCII.equals(detected) ||
StandardCharsets.UTF_8.equals(detected)) {
charset = null;
result = new String(result.getBytes(c), detected);
}
}
} catch (Throwable ex) {
Log.w(ex);
}
if (charset == null) {
// <meta charset="utf-8" />
// <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />