mirror of
https://github.com/moparisthebest/k-9
synced 2024-11-10 11:35:11 -05:00
Display an error message (instead of "null") if a message couldn't be decoded because of a missing charset.
See issue 1480 for a test case.
This commit is contained in:
parent
44f161fc7f
commit
e59987fdcb
@ -881,4 +881,6 @@ Welcome to K-9 Mail setup. K-9 is an open source mail client for Android origin
|
|||||||
|
|
||||||
<string name="save_or_discard_draft_message_dlg_title">Save draft message?</string>
|
<string name="save_or_discard_draft_message_dlg_title">Save draft message?</string>
|
||||||
<string name="save_or_discard_draft_message_instructions_fmt">Save or Discard this message?</string>
|
<string name="save_or_discard_draft_message_instructions_fmt">Save or Discard this message?</string>
|
||||||
|
|
||||||
|
<string name="charset_not_found">This message can\'t be displayed because the charset \"<xliff:g id="charset">%s</xliff:g>\" wasn\'t found.</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
@ -2,12 +2,14 @@
|
|||||||
package com.fsck.k9.mail.internet;
|
package com.fsck.k9.mail.internet;
|
||||||
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import com.fsck.k9.R;
|
||||||
import com.fsck.k9.K9;
|
import com.fsck.k9.K9;
|
||||||
import com.fsck.k9.mail.*;
|
import com.fsck.k9.mail.*;
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.apache.james.mime4j.decoder.Base64InputStream;
|
import org.apache.james.mime4j.decoder.Base64InputStream;
|
||||||
import org.apache.james.mime4j.decoder.DecoderUtil;
|
import org.apache.james.mime4j.decoder.DecoderUtil;
|
||||||
import org.apache.james.mime4j.decoder.QuotedPrintableInputStream;
|
import org.apache.james.mime4j.decoder.QuotedPrintableInputStream;
|
||||||
|
import org.apache.james.mime4j.util.CharsetUtil;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -345,68 +347,58 @@ public class MimeUtility
|
|||||||
* to be done.
|
* to be done.
|
||||||
* @param part
|
* @param part
|
||||||
* @return
|
* @return
|
||||||
* @throws IOException
|
|
||||||
*/
|
*/
|
||||||
public static String getTextFromPart(Part part)
|
public static String getTextFromPart(Part part)
|
||||||
{
|
{
|
||||||
Charset mCharsetConverter;
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (part != null && part.getBody() != null)
|
if ((part != null) && (part.getBody() != null))
|
||||||
{
|
{
|
||||||
Body body = part.getBody();
|
final Body body = part.getBody();
|
||||||
if (body instanceof TextBody)
|
if (body instanceof TextBody)
|
||||||
{
|
{
|
||||||
return ((TextBody)body).getText();
|
return ((TextBody)body).getText();
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
InputStream in = part.getBody().getInputStream();
|
|
||||||
String mimeType = part.getMimeType();
|
|
||||||
if (mimeType != null && MimeUtility.mimeTypeMatches(mimeType, "text/*"))
|
|
||||||
{
|
|
||||||
/*
|
|
||||||
* Now we read the part into a buffer for further processing. Because
|
|
||||||
* the stream is now wrapped we'll remove any transfer encoding at this point.
|
|
||||||
*/
|
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
||||||
IOUtils.copy(in, out);
|
|
||||||
|
|
||||||
byte[] bytes = out.toByteArray();
|
final String mimeType = part.getMimeType();
|
||||||
in.close();
|
if ((mimeType != null) && MimeUtility.mimeTypeMatches(mimeType, "text/*"))
|
||||||
out.close();
|
{
|
||||||
|
|
||||||
String charset = getHeaderParameter(part.getContentType(), "charset");
|
|
||||||
/*
|
/*
|
||||||
* We've got a text part, so let's see if it needs to be processed further.
|
* We've got a text part, so let's see if it needs to be processed further.
|
||||||
*/
|
*/
|
||||||
if (charset != null)
|
final String originalCharset = getHeaderParameter(part.getContentType(), "charset");
|
||||||
|
String charset = "ASCII"; // No encoding, so use us-ascii, which is the standard.
|
||||||
|
if (originalCharset != null)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* See if there is conversion from the MIME charset to the Java one.
|
* See if there is conversion from the MIME charset to the Java one.
|
||||||
*/
|
*/
|
||||||
mCharsetConverter = Charset.forName(charset);
|
charset = CharsetUtil.toJavaCharset(originalCharset);
|
||||||
charset = mCharsetConverter.name();
|
|
||||||
}
|
if (charset == null)
|
||||||
if (charset != null)
|
|
||||||
{
|
{
|
||||||
|
return String.format(K9.app.getString(R.string.charset_not_found), originalCharset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* We've got a charset encoding, so decode using it.
|
* Now we read the part into a buffer for further processing. Because
|
||||||
|
* the stream is now wrapped we'll remove any transfer encoding at this point.
|
||||||
*/
|
*/
|
||||||
return new String(bytes, 0, bytes.length, charset);
|
final InputStream in = part.getBody().getInputStream();
|
||||||
}
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
else
|
IOUtils.copy(in, out);
|
||||||
{
|
in.close();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* No encoding, so use us-ascii, which is the standard.
|
* Convert and return as new String
|
||||||
*/
|
*/
|
||||||
return new String(bytes, 0, bytes.length, "ASCII");
|
final String result = out.toString(charset);
|
||||||
|
out.close();
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}//if text body
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
|
Loading…
Reference in New Issue
Block a user