Check whether text attachment is UTF-8

master
M66B 13 hours ago
parent e072b6c6a0
commit 21d81f250a

@ -23,6 +23,9 @@ import android.text.TextUtils;
import android.util.Pair;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
@ -76,6 +79,19 @@ public class CharsetHelper {
return isUTF8(octets);
}
static boolean isUTF8(File file) {
try (FileInputStream fis = new FileInputStream(file)) {
ByteArrayOutputStream os = new ByteArrayOutputStream(Math.max(MAX_SAMPLE_SIZE, fis.available()));
byte[] buffer = new byte[MAX_SAMPLE_SIZE];
for (int len = fis.read(buffer); len != -1; len = fis.read(buffer))
os.write(buffer, 0, len);
return isUTF8(os.toByteArray());
} catch (Throwable ex) {
Log.e(ex);
return false;
}
}
static boolean isUTF8(byte[] octets) {
return isValid(octets, StandardCharsets.UTF_8);
}

@ -1473,13 +1473,14 @@ public class MessageHelper {
BodyPart attachmentPart = new MimeBodyPart();
File file = attachment.getFile(context);
boolean isUtf8 = (attachment.type != null && attachment.type.startsWith("text/") && CharsetHelper.isUTF8(file));
FileDataSource dataSource = new FileDataSource(file);
dataSource.setFileTypeMap(new FileTypeMap() {
@Override
public String getContentType(File file) {
// https://tools.ietf.org/html/rfc6047
if ("text/calendar".equals(attachment.type))
if ("text/calendar".equals(attachment.type) || isUtf8)
return attachment.type + "; charset=UTF-8;";
return attachment.type;

Loading…
Cancel
Save