Search in sources :

Example 1 with QuotedPrintableOutputStream

use of org.apache.james.mime4j.codec.QuotedPrintableOutputStream in project k-9 by k9mail.

the class SignSafeOutputStreamTest method testSignSafeQuotedPrintableOutputStream.

@Test
public void testSignSafeQuotedPrintableOutputStream() throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    OutputStream signSafeOutputStream = new SignSafeOutputStream(byteArrayOutputStream);
    OutputStream quotedPrintableOutputStream = new QuotedPrintableOutputStream(signSafeOutputStream, false);
    quotedPrintableOutputStream.write(INPUT_STRING.getBytes("US-ASCII"));
    quotedPrintableOutputStream.close();
    signSafeOutputStream.close();
    assertEquals(EXPECTED_QUOTED_PRINTABLE_SIGNSAFE, new String(byteArrayOutputStream.toByteArray(), "US-ASCII"));
}
Also used : QuotedPrintableOutputStream(org.apache.james.mime4j.codec.QuotedPrintableOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) QuotedPrintableOutputStream(org.apache.james.mime4j.codec.QuotedPrintableOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 2 with QuotedPrintableOutputStream

use of org.apache.james.mime4j.codec.QuotedPrintableOutputStream 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 = null;
            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 3 with QuotedPrintableOutputStream

use of org.apache.james.mime4j.codec.QuotedPrintableOutputStream 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)

Example 4 with QuotedPrintableOutputStream

use of org.apache.james.mime4j.codec.QuotedPrintableOutputStream in project k-9 by k9mail.

the class MigrationTo51 method insertTextualPartIntoDatabase.

private static MimeStructureState insertTextualPartIntoDatabase(SQLiteDatabase db, MimeStructureState structureState, MimeHeader mimeHeader, String content, boolean isHtml) throws IOException {
    if (mimeHeader == null) {
        mimeHeader = new MimeHeader();
    }
    mimeHeader.setHeader(MimeHeader.HEADER_CONTENT_TYPE, isHtml ? "text/html; charset=\"utf-8\"" : "text/plain; charset=\"utf-8\"");
    mimeHeader.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, MimeUtil.ENC_QUOTED_PRINTABLE);
    byte[] contentBytes;
    int decodedBodySize;
    int dataLocation;
    if (content != null) {
        ByteArrayOutputStream contentOutputStream = new ByteArrayOutputStream();
        QuotedPrintableOutputStream quotedPrintableOutputStream = new QuotedPrintableOutputStream(contentOutputStream, false);
        quotedPrintableOutputStream.write(content.getBytes());
        quotedPrintableOutputStream.flush();
        dataLocation = DATA_LOCATION__IN_DATABASE;
        contentBytes = contentOutputStream.toByteArray();
        decodedBodySize = content.length();
    } else {
        dataLocation = DATA_LOCATION__MISSING;
        contentBytes = null;
        decodedBodySize = 0;
    }
    ContentValues cv = new ContentValues();
    cv.put("type", MESSAGE_PART_TYPE__UNKNOWN);
    cv.put("data_location", dataLocation);
    cv.put("mime_type", isHtml ? "text/html" : "text/plain");
    cv.put("header", mimeHeader.toString());
    cv.put("data", contentBytes);
    cv.put("decoded_body_size", decodedBodySize);
    cv.put("encoding", MimeUtil.ENC_QUOTED_PRINTABLE);
    cv.put("charset", "utf-8");
    structureState.applyValues(cv);
    long partId = db.insertOrThrow("message_parts", null, cv);
    return structureState.nextChild(partId);
}
Also used : ContentValues(android.content.ContentValues) QuotedPrintableOutputStream(org.apache.james.mime4j.codec.QuotedPrintableOutputStream) MimeHeader(com.fsck.k9.mail.internet.MimeHeader) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 5 with QuotedPrintableOutputStream

use of org.apache.james.mime4j.codec.QuotedPrintableOutputStream in project k-9 by k9mail.

the class SignSafeOutputStreamTest method testQuotedPrintableOutputStream.

@Test
public void testQuotedPrintableOutputStream() throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    OutputStream output = new QuotedPrintableOutputStream(byteArrayOutputStream, false);
    output.write(INPUT_STRING.getBytes("US-ASCII"));
    output.close();
    assertEquals(EXPECTED_QUOTED_PRINTABLE, new String(byteArrayOutputStream.toByteArray(), "US-ASCII"));
}
Also used : QuotedPrintableOutputStream(org.apache.james.mime4j.codec.QuotedPrintableOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) QuotedPrintableOutputStream(org.apache.james.mime4j.codec.QuotedPrintableOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

QuotedPrintableOutputStream (org.apache.james.mime4j.codec.QuotedPrintableOutputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 OutputStream (java.io.OutputStream)3 Base64OutputStream (com.fsck.k9.mail.filter.Base64OutputStream)2 InputStream (java.io.InputStream)2 Test (org.junit.Test)2 ContentValues (android.content.ContentValues)1 MessagingException (com.fsck.k9.mail.MessagingException)1 MimeHeader (com.fsck.k9.mail.internet.MimeHeader)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