From 564e2432e1845c11495c8f7876f81f7284a92548 Mon Sep 17 00:00:00 2001 From: cketti Date: Fri, 23 Jan 2015 03:42:00 +0100 Subject: [PATCH] Get size of decoded body content when saving Before downloading we show the encoded size of attachments. After download we strip the transport encoding to find out the size of the decoded content. --- .../com/fsck/k9/mailstore/LocalFolder.java | 31 +++++++++++++++++-- .../com/fsck/k9/mailstore/LocalStore.java | 2 +- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalFolder.java b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalFolder.java index 0ed6db404..53cc24db0 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalFolder.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalFolder.java @@ -42,11 +42,13 @@ import com.fsck.k9.mail.MessageRetrievalListener; import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.Multipart; import com.fsck.k9.mail.Part; +import com.fsck.k9.mail.filter.CountingOutputStream; import com.fsck.k9.mail.internet.MimeHeader; import com.fsck.k9.mail.internet.MimeMessage; import com.fsck.k9.mail.internet.MimeMultipart; import com.fsck.k9.mailstore.LockableDatabase.DbCallback; import com.fsck.k9.mailstore.LockableDatabase.WrappedException; +import org.apache.commons.io.IOUtils; import org.apache.james.mime4j.MimeException; import org.apache.james.mime4j.parser.ContentHandler; import org.apache.james.mime4j.parser.MimeStreamParser; @@ -1373,11 +1375,11 @@ public class LocalFolder extends Folder implements Serializable { byte[] bodyData = getBodyBytes(body); String encoding = getTransferEncoding(part); - if (attachment.size == AttachmentViewInfo.UNKNOWN_SIZE) { - //FIXME: Use size of content when transfer encoding is stripped + long size = decodeAndCountBytes(bodyData, encoding); + if (size == AttachmentViewInfo.UNKNOWN_SIZE) { cv.put("decoded_body_size", bodyData.length); } else { - cv.put("decoded_body_size", attachment.size); + cv.put("decoded_body_size", size); } cv.put("encoding", encoding); @@ -1387,6 +1389,29 @@ public class LocalFolder extends Folder implements Serializable { } } + private long decodeAndCountBytes(byte[] bodyData, String encoding) { + ByteArrayInputStream rawInputStream = new ByteArrayInputStream(bodyData); + return decodeAndCountBytes(encoding, rawInputStream); + } + + private long decodeAndCountBytes(String encoding, ByteArrayInputStream rawInputStream) { + InputStream decodingInputStream = localStore.getDecodingInputStream(rawInputStream, encoding); + try { + CountingOutputStream countingOutputStream = new CountingOutputStream(); + try { + IOUtils.copy(decodingInputStream, countingOutputStream); + + return countingOutputStream.getCount(); + } catch (IOException e) { + return AttachmentViewInfo.UNKNOWN_SIZE; + } + } finally { + try { + decodingInputStream.close(); + } catch (IOException e) { /* ignore */ } + } + } + private byte[] getHeaderBytes(Part part) throws IOException, MessagingException { ByteArrayOutputStream output = new ByteArrayOutputStream(); part.writeHeaderTo(output); diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalStore.java b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalStore.java index a33b44bc0..e50015d91 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalStore.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalStore.java @@ -717,7 +717,7 @@ public class LocalStore extends Store implements Serializable { } } - private InputStream getDecodingInputStream(InputStream rawInputStream, String encoding) { + InputStream getDecodingInputStream(InputStream rawInputStream, String encoding) { if (MimeUtil.ENC_BASE64.equals(encoding)) { return new Base64InputStream(rawInputStream); }