1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-27 11:42:16 -05:00

Modify BinaryTempFileBody to retain the encoded body

For now this breaks a lot of things, e.g. saving messages to the database
and making messages 7-bit safe.
This commit is contained in:
cketti 2014-11-25 19:59:05 +01:00
parent d32d6eed0e
commit 9f4f0cf6a8
6 changed files with 17 additions and 49 deletions

View File

@ -2,10 +2,7 @@ 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.*;
@ -32,8 +29,7 @@ public class BinaryTempFileBody implements Body {
public BinaryTempFileBody() {
if (mTempDirectory == null) {
throw new
RuntimeException("setTempDirectory has not been called on BinaryTempFileBody!");
throw new RuntimeException("setTempDirectory has not been called on BinaryTempFileBody!");
}
}
@ -53,23 +49,8 @@ public class BinaryTempFileBody implements Body {
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();
}

View File

@ -531,8 +531,7 @@ public class MimeMessage extends Message {
public void body(BodyDescriptor bd, InputStream in) throws IOException {
expect(Part.class);
try {
Body body = MimeUtility.decodeBody(in,
bd.getTransferEncoding(), bd.getMimeType());
Body body = MimeUtility.createBody(in, bd.getTransferEncoding(), bd.getMimeType());
((Part)stack.peek()).setBody(body);
} catch (MessagingException me) {
throw new Error(me);

View File

@ -1127,24 +1127,11 @@ public class MimeUtility {
return false;
}
/**
* Removes any content transfer encoding from the stream and returns a Body.
* @throws MessagingException
*/
public static Body decodeBody(InputStream in,
String contentTransferEncoding, String contentType)
public static Body createBody(InputStream in, String contentTransferEncoding, String contentType)
throws IOException, MessagingException {
/*
* We'll remove any transfer encoding by wrapping the stream.
*/
if (contentTransferEncoding != null) {
contentTransferEncoding =
MimeUtility.getHeaderParameter(contentTransferEncoding, null);
if (MimeUtil.ENC_QUOTED_PRINTABLE.equalsIgnoreCase(contentTransferEncoding)) {
in = new QuotedPrintableInputStream(in);
} else if (MimeUtil.ENC_BASE64.equalsIgnoreCase(contentTransferEncoding)) {
in = new Base64InputStream(in);
}
contentTransferEncoding = MimeUtility.getHeaderParameter(contentTransferEncoding, null);
}
BinaryTempFileBody tempBody;
@ -1154,12 +1141,14 @@ public class MimeUtility {
tempBody = new BinaryTempFileBody();
}
tempBody.setEncoding(contentTransferEncoding);
OutputStream out = tempBody.getOutputStream();
try {
IOUtils.copy(in, out);
} finally {
out.close();
}
return tempBody;
}

View File

@ -25,8 +25,6 @@ import java.security.GeneralSecurityException;
import java.security.Security;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Deque;
@ -1625,7 +1623,7 @@ public class ImapStore extends Store {
.getHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING)[0];
String contentType = part
.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0];
MimeMessageHelper.setBody(part, MimeUtility.decodeBody(bodyStream,
MimeMessageHelper.setBody(part, MimeUtility.createBody(bodyStream,
contentTransferEncoding, contentType));
} else {
// This shouldn't happen
@ -3594,7 +3592,7 @@ public class ImapStore extends Store {
String contentType = mPart
.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0];
return MimeUtility.decodeBody(literal, contentTransferEncoding,
return MimeUtility.createBody(literal, contentTransferEncoding,
contentType);
}
return null;

View File

@ -332,12 +332,12 @@ public class MessageTest extends AndroidTestCase {
private MimeBodyPart binaryBodyPart() throws IOException,
MessagingException {
String encodedTestString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz0123456789+/";
+ "abcdefghijklmnopqrstuvwxyz0123456789+/\r\n";
BinaryTempFileBody tempFileBody = new BinaryTempFileBody();
InputStream in = new Base64InputStream(new ByteArrayInputStream(
encodedTestString.getBytes("UTF-8")));
InputStream in = new ByteArrayInputStream(
encodedTestString.getBytes("UTF-8"));
OutputStream out = tempFileBody.getOutputStream();
try {

View File

@ -37,9 +37,10 @@ public class ReconstructMessageTest extends AndroidTestCase {
"\r\n" +
"------Boundary\r\n" +
"Content-Type: text/plain\r\n" +
"Content-Transfer-Encoding: quoted-printable\r\n" +
"Content-Transfer-Encoding: base64\r\n" +
"\r\n" +
"=2E=2E=2E\r\n" +
"VGhpcyBpcyBhIHRl\r\n" +
"c3QgbWVzc2FnZQ==\r\n" +
"\r\n" +
"------Boundary--\r\n";