diff --git a/src/com/fsck/k9/activity/MessageCompose.java b/src/com/fsck/k9/activity/MessageCompose.java index 35507d3ab..727f524ca 100644 --- a/src/com/fsck/k9/activity/MessageCompose.java +++ b/src/com/fsck/k9/activity/MessageCompose.java @@ -107,9 +107,9 @@ import com.fsck.k9.mail.internet.MimeMultipart; import com.fsck.k9.mail.internet.MimeUtility; import com.fsck.k9.mail.internet.TextBody; import com.fsck.k9.mail.internet.TextBodyBuilder; -import com.fsck.k9.mail.store.local.LocalStore.LocalAttachmentBody; -import com.fsck.k9.mail.store.local.LocalStore.TempFileBody; -import com.fsck.k9.mail.store.local.LocalStore.TempFileMessageBody; +import com.fsck.k9.mail.store.local.LocalAttachmentBody; +import com.fsck.k9.mail.store.local.TempFileBody; +import com.fsck.k9.mail.store.local.TempFileMessageBody; import com.fsck.k9.view.MessageWebView; import org.apache.james.mime4j.codec.EncoderUtil; diff --git a/src/com/fsck/k9/mail/store/local/AttachmentMessageBodyUtil.java b/src/com/fsck/k9/mail/store/local/AttachmentMessageBodyUtil.java new file mode 100644 index 000000000..61cb8456a --- /dev/null +++ b/src/com/fsck/k9/mail/store/local/AttachmentMessageBodyUtil.java @@ -0,0 +1,34 @@ +package com.fsck.k9.mail.store.local; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.apache.commons.io.IOUtils; +import org.apache.james.mime4j.util.MimeUtil; + +import com.fsck.k9.mail.MessagingException; +import com.fsck.k9.mail.internet.MimeMessage; + +public class AttachmentMessageBodyUtil { + public static void writeTo(BinaryAttachmentBody body, OutputStream out) throws IOException, + MessagingException { + InputStream in = body.getInputStream(); + try { + if (MimeUtil.ENC_7BIT.equalsIgnoreCase(body.getEncoding())) { + /* + * If we knew the message was already 7bit clean, then it + * could be sent along without processing. But since we + * don't know, we recursively parse it. + */ + MimeMessage message = new MimeMessage(in, true); + message.setUsing7bitTransport(); + message.writeTo(out); + } else { + IOUtils.copy(in, out); + } + } finally { + in.close(); + } + } +} \ No newline at end of file diff --git a/src/com/fsck/k9/mail/store/local/BinaryAttachmentBody.java b/src/com/fsck/k9/mail/store/local/BinaryAttachmentBody.java new file mode 100644 index 000000000..c03fceb0d --- /dev/null +++ b/src/com/fsck/k9/mail/store/local/BinaryAttachmentBody.java @@ -0,0 +1,54 @@ +package com.fsck.k9.mail.store.local; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.apache.commons.io.IOUtils; +import org.apache.james.mime4j.codec.QuotedPrintableOutputStream; +import org.apache.james.mime4j.util.MimeUtil; + +import com.fsck.k9.mail.Body; +import com.fsck.k9.mail.MessagingException; +import com.fsck.k9.mail.filter.Base64OutputStream; + +public abstract class BinaryAttachmentBody implements Body { + protected String mEncoding; + + @Override + public abstract InputStream getInputStream() throws MessagingException; + + @Override + public void writeTo(OutputStream out) throws IOException, MessagingException { + InputStream in = getInputStream(); + try { + boolean closeStream = false; + if (MimeUtil.isBase64Encoding(mEncoding)) { + out = new Base64OutputStream(out); + closeStream = true; + } else if (MimeUtil.isQuotedPrintableEncoded(mEncoding)){ + out = new QuotedPrintableOutputStream(out, false); + closeStream = true; + } + + try { + IOUtils.copy(in, out); + } finally { + if (closeStream) { + out.close(); + } + } + } finally { + in.close(); + } + } + + @Override + public void setEncoding(String encoding) throws MessagingException { + mEncoding = encoding; + } + + public String getEncoding() { + return mEncoding; + } +} \ No newline at end of file diff --git a/src/com/fsck/k9/mail/store/local/LocalAttachmentBody.java b/src/com/fsck/k9/mail/store/local/LocalAttachmentBody.java new file mode 100644 index 000000000..34772be95 --- /dev/null +++ b/src/com/fsck/k9/mail/store/local/LocalAttachmentBody.java @@ -0,0 +1,37 @@ +package com.fsck.k9.mail.store.local; + +import java.io.ByteArrayInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; + +import android.app.Application; +import android.net.Uri; + +import com.fsck.k9.mail.MessagingException; + +public class LocalAttachmentBody extends BinaryAttachmentBody { + private Application mApplication; + private Uri mUri; + + public LocalAttachmentBody(Uri uri, Application application) { + mApplication = application; + mUri = uri; + } + + @Override + public InputStream getInputStream() throws MessagingException { + try { + return mApplication.getContentResolver().openInputStream(mUri); + } catch (FileNotFoundException fnfe) { + /* + * Since it's completely normal for us to try to serve up attachments that + * have been blown away, we just return an empty stream. + */ + return new ByteArrayInputStream(LocalStore.EMPTY_BYTE_ARRAY); + } + } + + public Uri getContentUri() { + return mUri; + } +} \ No newline at end of file diff --git a/src/com/fsck/k9/mail/store/local/LocalAttachmentMessageBody.java b/src/com/fsck/k9/mail/store/local/LocalAttachmentMessageBody.java new file mode 100644 index 000000000..5c451cf15 --- /dev/null +++ b/src/com/fsck/k9/mail/store/local/LocalAttachmentMessageBody.java @@ -0,0 +1,49 @@ +package com.fsck.k9.mail.store.local; + +import java.io.IOException; +import java.io.OutputStream; + +import org.apache.james.mime4j.util.MimeUtil; + +import android.app.Application; +import android.net.Uri; + +import com.fsck.k9.mail.CompositeBody; +import com.fsck.k9.mail.MessagingException; + +/** + * A {@link LocalAttachmentBody} extension containing a message/rfc822 type body + * + */ +public class LocalAttachmentMessageBody extends LocalAttachmentBody implements CompositeBody { + + public LocalAttachmentMessageBody(Uri uri, Application application) { + super(uri, application); + } + + @Override + public void writeTo(OutputStream out) throws IOException, MessagingException { + AttachmentMessageBodyUtil.writeTo(this, out); + } + + @Override + public void setUsing7bitTransport() throws MessagingException { + /* + * There's nothing to recurse into here, so there's nothing to do. + * The enclosing BodyPart already called setEncoding(MimeUtil.ENC_7BIT). Once + * writeTo() is called, the file with the rfc822 body will be opened + * for reading and will then be recursed. + */ + + } + + @Override + public void setEncoding(String encoding) throws MessagingException { + if (!MimeUtil.ENC_7BIT.equalsIgnoreCase(encoding) + && !MimeUtil.ENC_8BIT.equalsIgnoreCase(encoding)) { + throw new MessagingException( + "Incompatible content-transfer-encoding applied to a CompositeBody"); + } + mEncoding = encoding; + } +} \ No newline at end of file diff --git a/src/com/fsck/k9/mail/store/local/LocalStore.java b/src/com/fsck/k9/mail/store/local/LocalStore.java index 9749531b0..dde0f0f0c 100644 --- a/src/com/fsck/k9/mail/store/local/LocalStore.java +++ b/src/com/fsck/k9/mail/store/local/LocalStore.java @@ -1,10 +1,7 @@ package com.fsck.k9.mail.store.local; -import java.io.ByteArrayInputStream; import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; @@ -26,7 +23,6 @@ import java.util.UUID; import java.util.regex.Pattern; import org.apache.commons.io.IOUtils; -import org.apache.james.mime4j.codec.QuotedPrintableOutputStream; import org.apache.james.mime4j.util.MimeUtil; import android.app.Application; @@ -54,7 +50,6 @@ import com.fsck.k9.helper.Utility; import com.fsck.k9.mail.Address; import com.fsck.k9.mail.Body; import com.fsck.k9.mail.BodyPart; -import com.fsck.k9.mail.CompositeBody; import com.fsck.k9.mail.FetchProfile; import com.fsck.k9.mail.Flag; import com.fsck.k9.mail.Folder; @@ -63,7 +58,6 @@ import com.fsck.k9.mail.Message.RecipientType; import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.Part; import com.fsck.k9.mail.Store; -import com.fsck.k9.mail.filter.Base64OutputStream; import com.fsck.k9.mail.internet.MimeBodyPart; import com.fsck.k9.mail.internet.MimeHeader; import com.fsck.k9.mail.internet.MimeMessage; @@ -98,7 +92,7 @@ public class LocalStore extends Store implements Serializable { private static final Message[] EMPTY_MESSAGE_ARRAY = new Message[0]; private static final String[] EMPTY_STRING_ARRAY = new String[0]; private static final Flag[] EMPTY_FLAG_ARRAY = new Flag[0]; - private static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; + static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; /* * a String containing the columns getMessages expects to work with @@ -4060,178 +4054,6 @@ public class LocalStore extends Store implements Serializable { } } - public abstract static class BinaryAttachmentBody implements Body { - protected String mEncoding; - - @Override - public abstract InputStream getInputStream() throws MessagingException; - - @Override - public void writeTo(OutputStream out) throws IOException, MessagingException { - InputStream in = getInputStream(); - try { - boolean closeStream = false; - if (MimeUtil.isBase64Encoding(mEncoding)) { - out = new Base64OutputStream(out); - closeStream = true; - } else if (MimeUtil.isQuotedPrintableEncoded(mEncoding)){ - out = new QuotedPrintableOutputStream(out, false); - closeStream = true; - } - - try { - IOUtils.copy(in, out); - } finally { - if (closeStream) { - out.close(); - } - } - } finally { - in.close(); - } - } - - @Override - public void setEncoding(String encoding) throws MessagingException { - mEncoding = encoding; - } - - public String getEncoding() { - return mEncoding; - } - } - - public static class TempFileBody extends BinaryAttachmentBody { - private final File mFile; - - public TempFileBody(String filename) { - mFile = new File(filename); - } - - @Override - public InputStream getInputStream() throws MessagingException { - try { - return new FileInputStream(mFile); - } catch (FileNotFoundException e) { - return new ByteArrayInputStream(EMPTY_BYTE_ARRAY); - } - } - } - - public static class LocalAttachmentBody extends BinaryAttachmentBody { - private Application mApplication; - private Uri mUri; - - public LocalAttachmentBody(Uri uri, Application application) { - mApplication = application; - mUri = uri; - } - - @Override - public InputStream getInputStream() throws MessagingException { - try { - return mApplication.getContentResolver().openInputStream(mUri); - } catch (FileNotFoundException fnfe) { - /* - * Since it's completely normal for us to try to serve up attachments that - * have been blown away, we just return an empty stream. - */ - return new ByteArrayInputStream(EMPTY_BYTE_ARRAY); - } - } - - public Uri getContentUri() { - return mUri; - } - } - - /** - * A {@link LocalAttachmentBody} extension containing a message/rfc822 type body - * - */ - public static class LocalAttachmentMessageBody extends LocalAttachmentBody implements CompositeBody { - - public LocalAttachmentMessageBody(Uri uri, Application application) { - super(uri, application); - } - - @Override - public void writeTo(OutputStream out) throws IOException, MessagingException { - AttachmentMessageBodyUtil.writeTo(this, out); - } - - @Override - public void setUsing7bitTransport() throws MessagingException { - /* - * There's nothing to recurse into here, so there's nothing to do. - * The enclosing BodyPart already called setEncoding(MimeUtil.ENC_7BIT). Once - * writeTo() is called, the file with the rfc822 body will be opened - * for reading and will then be recursed. - */ - - } - - @Override - public void setEncoding(String encoding) throws MessagingException { - if (!MimeUtil.ENC_7BIT.equalsIgnoreCase(encoding) - && !MimeUtil.ENC_8BIT.equalsIgnoreCase(encoding)) { - throw new MessagingException( - "Incompatible content-transfer-encoding applied to a CompositeBody"); - } - mEncoding = encoding; - } - } - - public static class TempFileMessageBody extends TempFileBody implements CompositeBody { - - public TempFileMessageBody(String filename) { - super(filename); - } - - @Override - public void writeTo(OutputStream out) throws IOException, MessagingException { - AttachmentMessageBodyUtil.writeTo(this, out); - } - - @Override - public void setUsing7bitTransport() throws MessagingException { - // see LocalAttachmentMessageBody.setUsing7bitTransport() - } - - @Override - public void setEncoding(String encoding) throws MessagingException { - if (!MimeUtil.ENC_7BIT.equalsIgnoreCase(encoding) - && !MimeUtil.ENC_8BIT.equalsIgnoreCase(encoding)) { - throw new MessagingException( - "Incompatible content-transfer-encoding applied to a CompositeBody"); - } - mEncoding = encoding; - } - } - - public static class AttachmentMessageBodyUtil { - public static void writeTo(BinaryAttachmentBody body, OutputStream out) throws IOException, - MessagingException { - InputStream in = body.getInputStream(); - try { - if (MimeUtil.ENC_7BIT.equalsIgnoreCase(body.getEncoding())) { - /* - * If we knew the message was already 7bit clean, then it - * could be sent along without processing. But since we - * don't know, we recursively parse it. - */ - MimeMessage message = new MimeMessage(in, true); - message.setUsing7bitTransport(); - message.writeTo(out); - } else { - IOUtils.copy(in, out); - } - } finally { - in.close(); - } - } - } - static class ThreadInfo { public final long threadId; public final long msgId; diff --git a/src/com/fsck/k9/mail/store/local/TempFileBody.java b/src/com/fsck/k9/mail/store/local/TempFileBody.java new file mode 100644 index 000000000..f093998d8 --- /dev/null +++ b/src/com/fsck/k9/mail/store/local/TempFileBody.java @@ -0,0 +1,26 @@ +package com.fsck.k9.mail.store.local; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; + +import com.fsck.k9.mail.MessagingException; + +public class TempFileBody extends BinaryAttachmentBody { + private final File mFile; + + public TempFileBody(String filename) { + mFile = new File(filename); + } + + @Override + public InputStream getInputStream() throws MessagingException { + try { + return new FileInputStream(mFile); + } catch (FileNotFoundException e) { + return new ByteArrayInputStream(LocalStore.EMPTY_BYTE_ARRAY); + } + } +} \ No newline at end of file diff --git a/src/com/fsck/k9/mail/store/local/TempFileMessageBody.java b/src/com/fsck/k9/mail/store/local/TempFileMessageBody.java new file mode 100644 index 000000000..82188f485 --- /dev/null +++ b/src/com/fsck/k9/mail/store/local/TempFileMessageBody.java @@ -0,0 +1,36 @@ +package com.fsck.k9.mail.store.local; + +import java.io.IOException; +import java.io.OutputStream; + +import org.apache.james.mime4j.util.MimeUtil; + +import com.fsck.k9.mail.CompositeBody; +import com.fsck.k9.mail.MessagingException; + +public class TempFileMessageBody extends TempFileBody implements CompositeBody { + + public TempFileMessageBody(String filename) { + super(filename); + } + + @Override + public void writeTo(OutputStream out) throws IOException, MessagingException { + AttachmentMessageBodyUtil.writeTo(this, out); + } + + @Override + public void setUsing7bitTransport() throws MessagingException { + // see LocalAttachmentMessageBody.setUsing7bitTransport() + } + + @Override + public void setEncoding(String encoding) throws MessagingException { + if (!MimeUtil.ENC_7BIT.equalsIgnoreCase(encoding) + && !MimeUtil.ENC_8BIT.equalsIgnoreCase(encoding)) { + throw new MessagingException( + "Incompatible content-transfer-encoding applied to a CompositeBody"); + } + mEncoding = encoding; + } +} \ No newline at end of file