From 3bf3606f1817b1cafb6d927708831fbfd1eae98f Mon Sep 17 00:00:00 2001 From: M66B Date: Thu, 20 Dec 2018 07:13:30 +0100 Subject: [PATCH] Decode QP streams --- .../java/eu/faircode/email/MessageHelper.java | 41 ++++++++++++------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index be71084724..f19a82085e 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -482,29 +482,42 @@ public class MessageHelper { return getHtml(imessage); } + private static String readStream(InputStream is, String charset) throws IOException { + ByteArrayOutputStream os = new ByteArrayOutputStream(); + byte[] buffer = new byte[4096]; + for (int len = is.read(buffer); len != -1; len = is.read(buffer)) + os.write(buffer, 0, len); + return new String(os.toByteArray(), charset); + } + private static String getHtml(Part part) throws MessagingException, IOException { if (part.isMimeType("text/*")) { String s; try { - s = part.getContent().toString(); - } catch (UnsupportedEncodingException ex) { - // x-binaryenc - Log.w(Helper.TAG, "Unsupported encoding: " + part.getContentType()); - // https://javaee.github.io/javamail/FAQ#unsupen - InputStream is = part.getInputStream(); - ByteArrayOutputStream os = new ByteArrayOutputStream(); - byte[] buffer = new byte[4096]; - for (int len = is.read(buffer); len != -1; len = is.read(buffer)) - os.write(buffer, 0, len); - s = new String(os.toByteArray(), "US-ASCII"); + Object content = part.getContent(); + try { + if (content instanceof String) + s = (String) content; + else if (content instanceof InputStream) + // Typically com.sun.mail.util.QPDecoderStream + s = readStream((InputStream) content, "UTF-8"); + else + s = content.toString(); + } catch (UnsupportedEncodingException ex) { + // x-binaryenc + // https://javaee.github.io/javamail/FAQ#unsupen + Log.w(Helper.TAG, "Unsupported encoding: " + part.getContentType()); + return readStream(part.getInputStream(), "US-ASCII"); + } } catch (IOException ex) { // IOException; Unknown encoding: none Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); - s = ex.toString(); + return "
" + ex + "
" + Log.getStackTraceString(ex) + "
"; } if (part.isMimeType("text/plain")) s = "
" + s.replaceAll("\\r?\\n", "
") + "
"; + return s; } @@ -527,7 +540,7 @@ public class MessageHelper { } catch (ParseException ex) { // ParseException: In parameter list boundary="...">, expected parameter name, got ";" Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); - text = ex.toString(); + text = "
" + ex + "
" + Log.getStackTraceString(ex) + "
"; } return text; } @@ -542,7 +555,7 @@ public class MessageHelper { } } catch (ParseException ex) { Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); - return ex.toString(); + return "
" + ex + "
" + Log.getStackTraceString(ex) + "
"; } return null;