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.hujiang.restvolley.webapi.request.JsonStreamerEntity.java

private void writeToFromFile(OutputStream os, FileWrapper wrapper) throws IOException {

    // Send the meta data.
    writeMetaData(os, wrapper.file.getName(), wrapper.contentType);

    int bytesRead;

    // Open the file for reading.
    FileInputStream in = new FileInputStream(wrapper.file);

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

    // Read from file until no more data's left to read.
    while ((bytesRead = in.read(buffer)) != -1) {
        bos.write(buffer, 0, bytesRead);
    }/*w  w  w .ja va  2 s .c  o  m*/

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

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

    // Safely close the input stream.
    if (in != null) {
        in.close();
    }
}

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

/**
 * Write a single attachment and its payload
 *///from   w  ww. java  2  s  .  co  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 {
            // 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.tct.emailcommon.internet.Rfc822Output.java

/**
 * Write a single attachment and its payload
 *//*from w w  w  .  j  a va2 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:edu.ncsu.asbransc.mouflon.recorder.UploadFile.java

private void encodeFileBase64(File file, DataOutputStream out) {
    byte[] buffer = new byte[4096];
    int bytesRead = 0;
    FileInputStream in = null;// w w  w  . j a  v a2 s.  c o  m
    try {
        in = new FileInputStream(file);
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
        return;
    }
    Base64OutputStream out64 = new Base64OutputStream(out, Base64.NO_CLOSE);
    try {
        while ((bytesRead = in.read(buffer)) > 0) { //this loop grabs more of the file and uploads it 4KB  at a time
            out64.write(buffer, 0, bytesRead);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            in.close();
            out64.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }
}