Decode QP streams

pull/147/head
M66B 6 years ago
parent 2be2017bd4
commit 3bf3606f18

@ -482,29 +482,42 @@ public class MessageHelper {
return getHtml(imessage); 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 { private static String getHtml(Part part) throws MessagingException, IOException {
if (part.isMimeType("text/*")) { if (part.isMimeType("text/*")) {
String s; String s;
try { try {
s = part.getContent().toString(); Object content = part.getContent();
} catch (UnsupportedEncodingException ex) { try {
// x-binaryenc if (content instanceof String)
Log.w(Helper.TAG, "Unsupported encoding: " + part.getContentType()); s = (String) content;
// https://javaee.github.io/javamail/FAQ#unsupen else if (content instanceof InputStream)
InputStream is = part.getInputStream(); // Typically com.sun.mail.util.QPDecoderStream
ByteArrayOutputStream os = new ByteArrayOutputStream(); s = readStream((InputStream) content, "UTF-8");
byte[] buffer = new byte[4096]; else
for (int len = is.read(buffer); len != -1; len = is.read(buffer)) s = content.toString();
os.write(buffer, 0, len); } catch (UnsupportedEncodingException ex) {
s = new String(os.toByteArray(), "US-ASCII"); // 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) { } catch (IOException ex) {
// IOException; Unknown encoding: none // IOException; Unknown encoding: none
Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
s = ex.toString(); return "<pre>" + ex + "<br />" + Log.getStackTraceString(ex) + "</pre>";
} }
if (part.isMimeType("text/plain")) if (part.isMimeType("text/plain"))
s = "<pre>" + s.replaceAll("\\r?\\n", "<br />") + "</pre>"; s = "<pre>" + s.replaceAll("\\r?\\n", "<br />") + "</pre>";
return s; return s;
} }
@ -527,7 +540,7 @@ public class MessageHelper {
} catch (ParseException ex) { } catch (ParseException ex) {
// ParseException: In parameter list boundary="...">, expected parameter name, got ";" // ParseException: In parameter list boundary="...">, expected parameter name, got ";"
Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
text = ex.toString(); text = "<pre>" + ex + "<br />" + Log.getStackTraceString(ex) + "</pre>";
} }
return text; return text;
} }
@ -542,7 +555,7 @@ public class MessageHelper {
} }
} catch (ParseException ex) { } catch (ParseException ex) {
Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
return ex.toString(); return "<pre>" + ex + "<br />" + Log.getStackTraceString(ex) + "</pre>";
} }
return null; return null;

Loading…
Cancel
Save