1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2024-12-25 01:06:04 +00:00

Upgrade ISO-8859-1 to UTF-8 for plain text messages

This commit is contained in:
M66B 2020-10-18 19:46:33 +02:00
parent 121456cc8c
commit e9c0bfa542

View file

@ -57,6 +57,7 @@ import java.io.UnsupportedEncodingException;
import java.net.IDN; import java.net.IDN;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.text.Normalizer; import java.text.Normalizer;
import java.text.ParsePosition; import java.text.ParsePosition;
@ -1775,16 +1776,29 @@ public class MessageHelper {
charset = null; charset = null;
if (h.part.isMimeType("text/plain")) { if (h.part.isMimeType("text/plain")) {
if (charset == null) { Charset cs = null;
try {
if (charset != null)
cs = Charset.forName(charset);
} catch (UnsupportedCharsetException ignored) {
}
if (charset == null || StandardCharsets.ISO_8859_1.equals(cs)) {
Charset detected = CharsetHelper.detect(result); Charset detected = CharsetHelper.detect(result);
if (detected == null) { if (StandardCharsets.ISO_8859_1.equals(cs) &&
if (CharsetHelper.isUTF8(result)) { StandardCharsets.UTF_8.equals(detected)) {
Log.i("Charset plain=UTF8"); Log.i("Charset upgrade=UTF8");
result = new String(result.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8); result = new String(result.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
}
} else { } else {
Log.i("Charset plain=" + detected.name()); if (detected == null) {
result = new String(result.getBytes(StandardCharsets.ISO_8859_1), detected); if (CharsetHelper.isUTF8(result)) {
Log.i("Charset plain=UTF8");
result = new String(result.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
}
} else {
Log.i("Charset plain=" + detected.name());
result = new String(result.getBytes(StandardCharsets.ISO_8859_1), detected);
}
} }
} }