Example usage for android.util Base64 CRLF

List of usage examples for android.util Base64 CRLF

Introduction

In this page you can find the example usage for android.util Base64 CRLF.

Prototype

int CRLF

To view the source code for android.util Base64 CRLF.

Click Source Link

Document

Encoder flag bit to indicate lines should be terminated with a CRLF pair instead of just an LF.

Usage

From source file:com.android.email.mail.transport.Rfc822Output.java

/**
 * Write text (either as main body or inside a multipart), preceded by appropriate headers.
 *
 * Note this always uses base64, even when not required.  Slightly less efficient for
 * US-ASCII text, but handles all formats even when non-ascii chars are involved.  A small
 * optimization might be to prescan the string for safety and send raw if possible.
 *
 * @param writer the output writer//w  ww . j  ava 2 s  .  c  om
 * @param out the output stream inside the writer (used for byte[] access)
 * @param text The original text of the message
 */
private static void writeTextWithHeaders(Writer writer, OutputStream out, String text) throws IOException {
    writeHeader(writer, "Content-Type", "text/plain; charset=utf-8");
    writeHeader(writer, "Content-Transfer-Encoding", "base64");
    writer.write("\r\n");
    byte[] bytes = text.getBytes("UTF-8");
    writer.flush();
    out.write(Base64.encode(bytes, Base64.CRLF));
}

From source file:com.chen.emailcommon.internet.Rfc822Output.java

/**
 * Write the body text.//from   w  w  w  . j  a va  2  s.co  m
 *
 * Note this always uses base64, even when not required.  Slightly less efficient for
 * US-ASCII text, but handles all formats even when non-ascii chars are involved.  A small
 * optimization might be to prescan the string for safety and send raw if possible.
 *
 * @param writer the output writer
 * @param out the output stream inside the writer (used for byte[] access)
 * @param bodyText Plain text and HTML versions of the original text of the message
 */
private static void writeTextWithHeaders(Writer writer, OutputStream out, String[] bodyText)
        throws IOException {
    boolean html = false;
    String text = bodyText[INDEX_BODY_TEXT];
    if (text == null) {
        text = bodyText[INDEX_BODY_HTML];
        html = true;
    }
    if (text == null) {
        writer.write("\r\n"); // a truly empty message
    } else {
        // first multipart element is the body
        String mimeType = "text/" + (html ? "html" : "plain");
        writeHeader(writer, "Content-Type", mimeType + "; charset=utf-8");
        writeHeader(writer, "Content-Transfer-Encoding", "base64");
        writer.write("\r\n");
        byte[] textBytes = text.getBytes("UTF-8");
        writer.flush();
        out.write(Base64.encode(textBytes, Base64.CRLF));
    }
}

From source file:com.mail163.email.mail.transport.Rfc822Output.java

/**
 * Write text (either as main body or inside a multipart), preceded by appropriate headers.
 *
 * Note this always uses base64, even when not required.  Slightly less efficient for
 * US-ASCII text, but handles all formats even when non-ascii chars are involved.  A small
 * optimization might be to prescan the string for safety and send raw if possible.
 *
 * @param writer the output writer/*  w ww . jav a 2s.c  o m*/
 * @param out the output stream inside the writer (used for byte[] access)
 * @param text The original text of the message
 */
private static void writeTextWithHeaders(Writer writer, OutputStream out, String text) throws IOException {
    Logs.v(Logs.LOG_MessageView, "isContentType >>>>>>>>> :" + isContentType);

    //=============2012.3.4===========
    if (isContentType) {//Content-Type
        writeHeader(writer, "Content-Type", "text/html; charset=utf-8");
    } else {
        writeHeader(writer, "Content-Type", "text/plain; charset=utf-8");
    }

    //=============2012.3.4===========
    writeHeader(writer, "Content-Transfer-Encoding", "base64");
    writer.write("\r\n");
    byte[] bytes = text.getBytes("UTF-8");
    writer.flush();
    out.write(Base64.encode(bytes, Base64.CRLF));
}

From source file:com.tct.emailcommon.internet.Rfc822Output.java

/**
 * Write a single attachment and its payload
 *///from www.  ja  va 2  s. com
private static void writeOneAttachment(Context context, Writer writer, OutputStream out, Attachment attachment)
        throws IOException, MessagingException {
    writeHeader(writer, "Content-Type", attachment.mMimeType + ";\n name=\"" +
    // TS: junwei-xu 2014-12-05 EMAIL BUGFIX_861660 MOD_S
            MimeUtility.foldAndEncode2(attachment.mFileName, "ContentType".length() + 2) + "\"");
    // TS: junwei-xu 2014-12-05 EMAIL BUGFIX_861660 MOD_E
    writeHeader(writer, "Content-Transfer-Encoding", "base64");
    // Most attachments (real files) will send Content-Disposition.  The suppression option
    // is used when sending calendar invites.
    if ((attachment.mFlags & Attachment.FLAG_ICS_ALTERNATIVE_PART) == 0) {
        writeHeader(writer, "Content-Disposition", "attachment;" + "\n filename=\"" +
        // TS: junwei-xu 2014-12-05 EMAIL BUGFIX_861660 MOD_S
                MimeUtility.foldAndEncode2(attachment.mFileName, "ContentType".length() + 2)
                // TS: junwei-xu 2014-12-05 EMAIL BUGFIX_861660 MOD_E
                + "\";" + "\n size=" + Long.toString(attachment.mSize));
    }
    if (attachment.mContentId != null) {
        writeHeader(writer, "Content-ID", attachment.mContentId);
    }
    writer.append("\r\n");

    // Set up input stream and write it out via base64
    InputStream inStream = null;
    try {
        // Use content, if provided; otherwise, use the contentUri
        if (attachment.mContentBytes != null) {
            inStream = new ByteArrayInputStream(attachment.mContentBytes);
        } else {
            // First try the cached file
            final String cachedFile = attachment.getCachedFileUri();
            if (!TextUtils.isEmpty(cachedFile)) {
                final Uri cachedFileUri = Uri.parse(cachedFile);
                try {
                    inStream = context.getContentResolver().openInputStream(cachedFileUri);
                } catch (FileNotFoundException e) {
                    // Couldn't open the cached file, fall back to the original content uri
                    inStream = null;

                    LogUtils.i(TAG, "Rfc822Output#writeOneAttachment(), failed to load"
                            + "cached file, falling back to: %s", attachment.getContentUri());
                }
            }

            if (inStream == null) {
                // try to open the file
                final Uri fileUri = Uri.parse(attachment.getContentUri());
                inStream = context.getContentResolver().openInputStream(fileUri);
            }
        }
        // switch to output stream for base64 text output
        writer.flush();
        Base64OutputStream base64Out = new Base64OutputStream(out, Base64.CRLF | Base64.NO_CLOSE);
        // copy base64 data and close up
        IOUtils.copy(inStream, base64Out);
        base64Out.close();

        // The old Base64OutputStream wrote an extra CRLF after
        // the output.  It's not required by the base-64 spec; not
        // sure if it's required by RFC 822 or not.
        out.write('\r');
        out.write('\n');
        out.flush();
    } catch (FileNotFoundException fnfe) {
        // Ignore this - empty file is OK
        LogUtils.e(TAG, fnfe,
                "Rfc822Output#writeOneAttachment(), FileNotFoundException" + "when sending attachment");
    } catch (IOException ioe) {
        LogUtils.e(TAG, ioe, "Rfc822Output#writeOneAttachment(), IOException" + "when sending attachment");
        throw new MessagingException("Invalid attachment.", ioe);
    }
}

From source file:com.android.emailcommon.internet.Rfc822Output.java

/**
 * Write the body text. If only one version of the body is specified (either plain text
 * or HTML), the text is written directly. Otherwise, the plain text and HTML bodies
 * are both written with the appropriate headers.
 *
 * Note this always uses base64, even when not required.  Slightly less efficient for
 * US-ASCII text, but handles all formats even when non-ascii chars are involved.  A small
 * optimization might be to prescan the string for safety and send raw if possible.
 *
 * @param writer the output writer/*www.ja va 2  s. c  o  m*/
 * @param out the output stream inside the writer (used for byte[] access)
 * @param bodyText Plain text and HTML versions of the original text of the message
 */
private static void writeTextWithHeaders(Writer writer, OutputStream out, String[] bodyText)
        throws IOException {
    String text = bodyText[INDEX_BODY_TEXT];
    String html = bodyText[INDEX_BODY_HTML];

    if (text == null) {
        writer.write("\r\n"); // a truly empty message
    } else {
        String multipartBoundary = null;
        boolean multipart = html != null;

        // Simplified case for no multipart - just emit text and be done.
        if (multipart) {
            // continue with multipart headers, then into multipart body
            multipartBoundary = getNextBoundary();

            writeHeader(writer, "Content-Type",
                    "multipart/alternative; boundary=\"" + multipartBoundary + "\"");
            // Finish headers and prepare for body section(s)
            writer.write("\r\n");
            writeBoundary(writer, multipartBoundary, false);
        }

        // first multipart element is the body
        writeHeader(writer, "Content-Type", "text/plain; charset=utf-8");
        writeHeader(writer, "Content-Transfer-Encoding", "base64");
        writer.write("\r\n");
        byte[] textBytes = text.getBytes("UTF-8");
        writer.flush();
        out.write(Base64.encode(textBytes, Base64.CRLF));

        if (multipart) {
            // next multipart section
            writeBoundary(writer, multipartBoundary, false);

            writeHeader(writer, "Content-Type", "text/html; charset=utf-8");
            writeHeader(writer, "Content-Transfer-Encoding", "base64");
            writer.write("\r\n");
            byte[] htmlBytes = html.getBytes("UTF-8");
            writer.flush();
            out.write(Base64.encode(htmlBytes, Base64.CRLF));

            // end of multipart section
            writeBoundary(writer, multipartBoundary, true);
        }
    }
}

From source file:com.tct.emailcommon.internet.Rfc822Output.java

/**
 * Write the body text./*from   w  w  w  .  ja va  2  s  .c  o  m*/
 *
 * Note this always uses base64, even when not required.  Slightly less efficient for
 * US-ASCII text, but handles all formats even when non-ascii chars are involved.  A small
 * optimization might be to prescan the string for safety and send raw if possible.
 *
 * @param writer the output writer
 * @param out the output stream inside the writer (used for byte[] access)
 * @param bodyText Plain text and HTML versions of the original text of the message
 */
private static void writeTextWithHeaders(Writer writer, OutputStream out, String[] bodyText,
        List<Attachment> atts) throws IOException {
    boolean html = false;
    String text = bodyText[INDEX_BODY_TEXT];
    // TS: zheng.zou 2015-03-31 EMAIL BUGFIX-962573 MOD_S
    String htmlText = bodyText[INDEX_BODY_HTML];
    if (TextUtils.isEmpty(text) || !TextUtils.isEmpty(htmlText)) {
        // TS: zheng.zou 2015-03-31 EMAIL BUGFIX-962573 MOD_E
        text = bodyText[INDEX_BODY_HTML];
        html = true;
    }
    //TS: zhaotianyong 2015-04-13 EMAIL BUGFIX_962560 ADD_S
    if (atts != null && html) {
        for (Attachment att : atts) {
            text = AttachmentUtilities.refactorHtmlBody(text, att);
        }
    }
    //TS: zhaotianyong 2015-04-13 EMAIL BUGFIX_962560 ADD_E
    if (TextUtils.isEmpty(text)) {
        writer.write("\r\n"); // a truly empty message
    } else {
        // first multipart element is the body
        final String mimeType = "text/" + (html ? "html" : "plain");
        writeHeader(writer, "Content-Type", mimeType + "; charset=utf-8");
        writeHeader(writer, "Content-Transfer-Encoding", "base64");
        writer.write("\r\n");
        final byte[] textBytes = text.getBytes("UTF-8");
        writer.flush();
        out.write(Base64.encode(textBytes, Base64.CRLF));
    }
}

From source file:com.android.launcher3.Utilities.java

public static String encrypt(String input) {
    return Base64.encodeToString(input.getBytes(), Base64.CRLF);
}

From source file:com.android.launcher3.Utilities.java

public static String decrypt(String input) {
    return new String(Base64.decode(input, Base64.CRLF));
}