Search in sources :

Example 1 with Base64OutputStream

use of com.fsck.k9.mail.filter.Base64OutputStream in project k-9 by k9mail.

the class BinaryTempFileBody method setEncoding.

public void setEncoding(String encoding) throws MessagingException {
    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);
        final OutputStream out = new FileOutputStream(newFile);
        try {
            OutputStream wrappedOut;
            if (MimeUtil.ENC_QUOTED_PRINTABLE.equals(encoding)) {
                wrappedOut = new QuotedPrintableOutputStream(out, false);
            } else if (MimeUtil.ENC_BASE64.equals(encoding)) {
                wrappedOut = new Base64OutputStream(out);
            } else {
                throw new RuntimeException("Target encoding not supported: " + encoding);
            }
            InputStream in = getInputStream();
            try {
                IOUtils.copy(in, wrappedOut);
            } finally {
                IOUtils.closeQuietly(in);
                IOUtils.closeQuietly(wrappedOut);
            }
        } finally {
            IOUtils.closeQuietly(out);
        }
        mFile = newFile;
        mEncoding = encoding;
    } catch (IOException e) {
        throw new MessagingException("Unable to convert body", e);
    }
}
Also used : QuotedPrintableOutputStream(org.apache.james.mime4j.codec.QuotedPrintableOutputStream) MessagingException(com.fsck.k9.mail.MessagingException) FileInputStream(java.io.FileInputStream) FilterInputStream(java.io.FilterInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Base64OutputStream(com.fsck.k9.mail.filter.Base64OutputStream) FileOutputStream(java.io.FileOutputStream) QuotedPrintableOutputStream(org.apache.james.mime4j.codec.QuotedPrintableOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Base64OutputStream(com.fsck.k9.mail.filter.Base64OutputStream) File(java.io.File)

Example 2 with Base64OutputStream

use of com.fsck.k9.mail.filter.Base64OutputStream in project k-9 by k9mail.

the class BinaryAttachmentBody method writeTo.

@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();
    }
}
Also used : QuotedPrintableOutputStream(org.apache.james.mime4j.codec.QuotedPrintableOutputStream) InputStream(java.io.InputStream) Base64OutputStream(com.fsck.k9.mail.filter.Base64OutputStream)

Aggregations

Base64OutputStream (com.fsck.k9.mail.filter.Base64OutputStream)2 InputStream (java.io.InputStream)2 QuotedPrintableOutputStream (org.apache.james.mime4j.codec.QuotedPrintableOutputStream)2 MessagingException (com.fsck.k9.mail.MessagingException)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 FilterInputStream (java.io.FilterInputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1