Example usage for android.util Base64 NO_CLOSE

List of usage examples for android.util Base64 NO_CLOSE

Introduction

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

Prototype

int NO_CLOSE

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

Click Source Link

Document

Flag to pass to Base64OutputStream to indicate that it should not close the output stream it is wrapping when it itself is closed.

Usage

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

@Override
public void writeTo(OutputStream out) throws IOException, MessagingException {
    InputStream in = getInputStream();
    Base64OutputStream base64Out = new Base64OutputStream(out, Base64.CRLF | Base64.NO_CLOSE);
    IOUtils.copy(in, base64Out);//from  w  ww  . j  a  va 2  s. c o  m
    base64Out.close();
    mFile.delete();
}

From source file:com.android.phone.common.mail.internet.BinaryTempFileBody.java

@Override
public void writeTo(OutputStream out) throws IOException, MessagingException {
    InputStream in = getInputStream();
    Base64OutputStream base64Out = new Base64OutputStream(out, Base64.CRLF | Base64.NO_CLOSE);
    IOUtils.copy(in, base64Out);//ww  w . ja  v  a  2 s .  c om
    base64Out.close();
    mFile.delete();
    in.close();
}

From source file:com.hybris.mobile.lib.commerce.helper.SecurityHelper.java

/**
 * Encrypt String associated with a key/*from   w  w w. j  a  va 2s  .  c om*/
 *
 * @param value The value to encrypt
 * @return encrypted string
 */
public static String encrypt(String value) {
    if (StringUtils.isBlank(value)) {
        throw new IllegalArgumentException();
    }

    String encryptedText = "";

    try {
        Cipher cipher = Cipher.getInstance(CIPHER);
        cipher.init(Cipher.ENCRYPT_MODE, mSecretKeySpec, mIvParameterSpec);
        encryptedText = Base64.encodeToString(cipher.doFinal(value.getBytes()), Base64.NO_CLOSE);
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "Algorithm not found.");
    } catch (NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException e) {
        Log.e(TAG, "Exception during encrypt");
    } catch (InvalidKeyException e) {
        Log.e(TAG, "No valid key provided.");
    } catch (InvalidAlgorithmParameterException e) {
        Log.e(TAG, "Algorithm parameter specification is invalid");
    }

    return encryptedText;
}

From source file:com.android.email.mail.internet.BinaryTempFileBody.java

public void writeTo(OutputStream out) throws IOException, MessagingException {
    InputStream in = getInputStream();
    Base64OutputStream base64Out = new Base64OutputStream(out, Base64.CRLF | Base64.NO_CLOSE);
    IOUtils.copy(in, base64Out);/*from   w  w w. java2  s.  com*/
    base64Out.close();
    mFile.delete();
}

From source file:com.hybris.mobile.lib.commerce.helper.SecurityHelper.java

/**
 * Decrypt secure String associated to the key
 *
 * @param value The value to decrypt/*from   w  ww  .  j a  va  2s . c  om*/
 * @return decrypted string
 */
public static String decrypt(String value) {
    String decryptedText = "";
    try {
        if (StringUtils.isNotBlank(value)) {
            Cipher cipher = Cipher.getInstance(CIPHER);
            cipher.init(Cipher.DECRYPT_MODE, mSecretKeySpec, mIvParameterSpec);
            decryptedText = new String(cipher.doFinal(Base64.decode(value, Base64.NO_CLOSE)), ENCODING);
            return decryptedText;
        }
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "Algorithm not found.");
    } catch (NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
        Log.e(TAG, "Exception during decrypt");
    } catch (InvalidKeyException e) {
        Log.e(TAG, "No valid key provided.");
    } catch (InvalidAlgorithmParameterException e) {
        Log.e(TAG, "Algorithm parameter specification is invalid");
    } catch (UnsupportedEncodingException e) {
        Log.e(TAG, "Character to convert is unavailable");
    }

    return decryptedText;
}

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

/**
 * Write a single attachment and its payload
 *///from   w  w  w  .  ja  va2s  .  c  o m
private static void writeOneAttachment(Context context, Writer writer, OutputStream out, Attachment attachment)
        throws IOException, MessagingException {
    writeHeader(writer, "Content-Type", attachment.mMimeType + ";\n name=\"" + attachment.mFileName + "\"");
    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=\"" + attachment.mFileName
                + "\";" + "\n size=" + Long.toString(attachment.mSize));
    }
    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 {
            // try to open the file
            Uri fileUri = Uri.parse(attachment.mContentUri);
            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
    } catch (IOException ioe) {
        throw new MessagingException("Invalid attachment.", ioe);
    }
}

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

/**
 * Write a single attachment and its payload
 *//*from   ww w. j ava  2 s.  c o m*/
private static void writeOneAttachment(Context context, Writer writer, OutputStream out, Attachment attachment)
        throws IOException, MessagingException {
    writeHeader(writer, "Content-Type", attachment.mMimeType + ";\n name=\"" + attachment.mFileName + "\"");
    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=\"" + attachment.mFileName
                + "\";" + "\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.d(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.indeema.emailcommon.internet.Rfc822Output.java

/**
 * Write a single attachment and its payload
 *///from w  w  w.  j  a va2s.c o  m
private static void writeOneAttachment(Context context, Writer writer, OutputStream out,
        EmailContent.Attachment attachment) throws IOException, MessagingException {
    writeHeader(writer, "Content-Type", attachment.mMimeType + ";\n name=\"" + attachment.mFileName + "\"");
    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 & EmailContent.Attachment.FLAG_ICS_ALTERNATIVE_PART) == 0) {
        writeHeader(writer, "Content-Disposition", "attachment;" + "\n filename=\"" + attachment.mFileName
                + "\";" + "\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.d(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.hujiang.restvolley.webapi.request.JsonStreamerEntity.java

private void writeToFromStream(OutputStream os, StreamWrapper entry) throws IOException {

    // Send the meta data.
    writeMetaData(os, entry.name, entry.contentType);

    int bytesRead;

    // Upload the file's contents in Base64.
    Base64OutputStream bos = new Base64OutputStream(os, Base64.NO_CLOSE | Base64.NO_WRAP);

    // Read from input stream until no more data's left to read.
    while ((bytesRead = entry.inputStream.read(buffer)) != -1) {
        bos.write(buffer, 0, bytesRead);
    }//from www .ja va 2s .c o m

    // Close the Base64 output stream.
    if (bos != null) {
        bos.close();
    }

    // End the meta data.
    endMetaData(os);

    // Close input stream.
    if (entry.autoClose) {
        // Safely close the input stream.
        if (entry.inputStream != null) {
            entry.inputStream.close();
        }
    }
}

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

/**
 * Write a single attachment and its payload
 *///from w  w w. j  ava2 s  .c  om
private static void writeOneAttachment(Context context, Writer writer, OutputStream out, Attachment attachment)
        throws IOException, MessagingException {
    //       String attachmentName = attachment.mFileName;
    String attachmentName = EncoderUtil.encodeAddressDisplayName(attachment.mFileName);

    writeHeader(writer, "Content-Type", attachment.mMimeType + ";\n name=\"" + attachmentName + "\"");

    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=\"" + attachmentName + "\";"
                + "\n size=" + Long.toString(attachment.mSize));
    }

    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 {
            // try to open the file
            Uri fileUri = Uri.parse(attachment.mContentUri);
            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
    } catch (IOException ioe) {
        throw new MessagingException("Invalid attachment.", ioe);
    }
}