From f7d3eaa0068c2d03ecdb57742b39b599e85811a7 Mon Sep 17 00:00:00 2001 From: cketti Date: Tue, 25 Nov 2014 23:59:03 +0100 Subject: [PATCH] Fix setUsing7bitTransport() functionality for BinaryTempFileBody --- .../k9/mail/internet/BinaryTempFileBody.java | 44 ++++++++++++++++++- .../internet/BinaryTempFileMessageBody.java | 4 ++ .../fsck/k9/mail/internet/MimeUtility.java | 5 +-- tests/src/com/fsck/k9/mail/MessageTest.java | 5 +-- 4 files changed, 50 insertions(+), 8 deletions(-) diff --git a/src/com/fsck/k9/mail/internet/BinaryTempFileBody.java b/src/com/fsck/k9/mail/internet/BinaryTempFileBody.java index dceec19e1..677739bbf 100644 --- a/src/com/fsck/k9/mail/internet/BinaryTempFileBody.java +++ b/src/com/fsck/k9/mail/internet/BinaryTempFileBody.java @@ -2,7 +2,10 @@ package com.fsck.k9.mail.internet; import com.fsck.k9.mail.Body; import com.fsck.k9.mail.MessagingException; +import com.fsck.k9.mail.filter.Base64OutputStream; import org.apache.commons.io.IOUtils; +import org.apache.james.mime4j.codec.QuotedPrintableOutputStream; +import org.apache.james.mime4j.util.MimeUtil; import java.io.*; @@ -24,13 +27,50 @@ public class BinaryTempFileBody implements Body { } public void setEncoding(String encoding) throws MessagingException { - mEncoding = encoding; + if (mEncoding != null && mEncoding.equalsIgnoreCase(encoding)) { + return; + } + + // The encoding changed, so we need to convert the message + if (!MimeUtil.ENC_8BIT.equalsIgnoreCase(mEncoding)) { + throw new RuntimeException("Can't convert from encoding: " + mEncoding); + } + + try { + File newFile = File.createTempFile("body", null, mTempDirectory); + OutputStream out = new FileOutputStream(newFile); + try { + if (MimeUtil.ENC_QUOTED_PRINTABLE.equals(encoding)) { + out = new QuotedPrintableOutputStream(out, false); + } else if (MimeUtil.ENC_BASE64.equals(encoding)) { + out = new Base64OutputStream(out); + } else { + throw new RuntimeException("Target encoding not supported: " + encoding); + } + + InputStream in = getInputStream(); + try { + IOUtils.copy(in, out); + } finally { + in.close(); + } + } finally { + out.close(); + } + + mFile = newFile; + mEncoding = encoding; + } catch (IOException e) { + throw new MessagingException("Unable to convert body", e); + } } - public BinaryTempFileBody() { + public BinaryTempFileBody(String encoding) { if (mTempDirectory == null) { throw new RuntimeException("setTempDirectory has not been called on BinaryTempFileBody!"); } + + mEncoding = encoding; } public OutputStream getOutputStream() throws IOException { diff --git a/src/com/fsck/k9/mail/internet/BinaryTempFileMessageBody.java b/src/com/fsck/k9/mail/internet/BinaryTempFileMessageBody.java index c1503b342..e5fe7c370 100644 --- a/src/com/fsck/k9/mail/internet/BinaryTempFileMessageBody.java +++ b/src/com/fsck/k9/mail/internet/BinaryTempFileMessageBody.java @@ -18,6 +18,10 @@ import com.fsck.k9.mail.MessagingException; */ public class BinaryTempFileMessageBody extends BinaryTempFileBody implements CompositeBody { + public BinaryTempFileMessageBody(String encoding) { + super(encoding); + } + @Override public void setEncoding(String encoding) throws MessagingException { if (!MimeUtil.ENC_7BIT.equalsIgnoreCase(encoding) diff --git a/src/com/fsck/k9/mail/internet/MimeUtility.java b/src/com/fsck/k9/mail/internet/MimeUtility.java index cdd63789e..b78f8e378 100644 --- a/src/com/fsck/k9/mail/internet/MimeUtility.java +++ b/src/com/fsck/k9/mail/internet/MimeUtility.java @@ -1136,11 +1136,10 @@ public class MimeUtility { BinaryTempFileBody tempBody; if (MimeUtil.isMessage(contentType)) { - tempBody = new BinaryTempFileMessageBody(); + tempBody = new BinaryTempFileMessageBody(contentTransferEncoding); } else { - tempBody = new BinaryTempFileBody(); + tempBody = new BinaryTempFileBody(contentTransferEncoding); } - tempBody.setEncoding(contentTransferEncoding); OutputStream out = tempBody.getOutputStream(); try { diff --git a/tests/src/com/fsck/k9/mail/MessageTest.java b/tests/src/com/fsck/k9/mail/MessageTest.java index c5fc39c74..30a8852b4 100644 --- a/tests/src/com/fsck/k9/mail/MessageTest.java +++ b/tests/src/com/fsck/k9/mail/MessageTest.java @@ -291,8 +291,7 @@ public class MessageTest extends AndroidTestCase { private MimeMessage nestedMessage(MimeMessage subMessage) throws MessagingException, IOException { - BinaryTempFileMessageBody tempMessageBody = new BinaryTempFileMessageBody(); - tempMessageBody.setEncoding(MimeUtil.ENC_8BIT); + BinaryTempFileMessageBody tempMessageBody = new BinaryTempFileMessageBody(MimeUtil.ENC_8BIT); OutputStream out = tempMessageBody.getOutputStream(); try { @@ -334,7 +333,7 @@ public class MessageTest extends AndroidTestCase { String encodedTestString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz0123456789+/\r\n"; - BinaryTempFileBody tempFileBody = new BinaryTempFileBody(); + BinaryTempFileBody tempFileBody = new BinaryTempFileBody(MimeUtil.ENC_BASE64); InputStream in = new ByteArrayInputStream( encodedTestString.getBytes("UTF-8"));