Example usage for android.util Base64OutputStream close

List of usage examples for android.util Base64OutputStream close

Introduction

In this page you can find the example usage for android.util Base64OutputStream close.

Prototype

public void close() throws IOException 

Source Link

Usage

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 .j  a va2  s .  c  o m*/
    base64Out.close();
    mFile.delete();
}

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);
    }/*from   w ww  .  j a va 2s .  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.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);
    }/* w  w  w  .j  a  v  a  2 s  .co 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.aoeng.degu.utils.net.asyncthhpclient.JsonStreamerEntity.java

@Override
public void writeTo(final OutputStream outstream) throws IOException {
    if (outstream == null) {
        throw new IllegalStateException("Output stream cannot be null.");
    }//w  ww .ja  v  a2 s . c  o m

    // Record the time when uploading started.
    long now = System.currentTimeMillis();

    // Keys used by the HashMaps.
    Set<String> keys;

    // Use GZIP compression when sending streams, otherwise just use
    // a buffered output stream to speed things up a bit.
    OutputStream upload;
    if (null != contentEncoding) {
        upload = new GZIPOutputStream(new BufferedOutputStream(outstream), BUFFER_SIZE);
    } else {
        upload = new BufferedOutputStream(outstream);
    }

    // Always send a JSON object.
    upload.write('{');

    // Send the K/V values.
    keys = kvParams.keySet();
    for (String key : keys) {
        // Write the JSON object's key.
        upload.write(escape(key));
        upload.write(':');

        // Evaluate the value (which cannot be null).
        Object value = kvParams.get(key);

        if (value instanceof Boolean) {
            upload.write((Boolean) value ? JSON_TRUE : JSON_FALSE);
        } else if (value instanceof Long) {
            upload.write((((Number) value).longValue() + "").getBytes());
        } else if (value instanceof Double) {
            upload.write((((Number) value).doubleValue() + "").getBytes());
        } else if (value instanceof Float) {
            upload.write((((Number) value).floatValue() + "").getBytes());
        } else if (value instanceof Integer) {
            upload.write((((Number) value).intValue() + "").getBytes());
        } else {
            upload.write(value.toString().getBytes());
        }

        upload.write(',');
    }

    // Buffer used for reading from input streams.
    byte[] buffer = new byte[BUFFER_SIZE];

    // Send the stream params.
    keys = streamParams.keySet();
    for (String key : keys) {
        RequestParams.StreamWrapper entry = streamParams.get(key);

        // Write the JSON object's key.
        upload.write(escape(key));

        // All uploads are sent as an object containing the file's details.
        upload.write(':');
        upload.write('{');

        // Send the streams's name.
        upload.write(STREAM_NAME);
        upload.write(':');
        upload.write(escape(entry.name));
        upload.write(',');

        // Send the streams's content type.
        upload.write(STREAM_TYPE);
        upload.write(':');
        upload.write(escape(entry.contentType));
        upload.write(',');

        // Prepare the file content's key.
        upload.write(STREAM_CONTENTS);
        upload.write(':');
        upload.write('"');

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

        // Read from input stream until no more data's left to read.
        int bytesRead;
        while ((bytesRead = entry.inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }

        // Close the Base64 output stream.
        outputStream.close();

        // End the file's object and prepare for next one.
        upload.write('"');
        upload.write('}');
        upload.write(',');
    }

    // Include the elapsed time taken to upload everything.
    // This might be useful for somebody, but it serves us well since
    // there will almost always be a ',' as the last sent character.
    upload.write(STREAM_ELAPSED);
    upload.write(':');
    long elapsedTime = System.currentTimeMillis() - now;
    upload.write((elapsedTime + "}").getBytes());

    Log.i(LOG_TAG, "Uploaded JSON in " + Math.floor(elapsedTime / 1000) + " seconds");

    // Flush the contents up the stream.
    upload.flush();
    upload.close();
}

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;/*from   w  w w .  ja  v  a 2 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();
        }
    }
}

From source file:org.mozilla.gecko.BrowserApp.java

private void getFaviconFromCache(final EventCallback callback, final String url) {
    final OnFaviconLoadedListener listener = new OnFaviconLoadedListener() {
        @Override//  w w w .  j  a v  a  2  s.co  m
        public void onFaviconLoaded(final String url, final String faviconURL, final Bitmap favicon) {
            ThreadUtils.assertOnUiThread();
            // Convert Bitmap to Base64 data URI in background.
            ThreadUtils.postToBackgroundThread(new Runnable() {
                @Override
                public void run() {
                    ByteArrayOutputStream out = null;
                    Base64OutputStream b64 = null;

                    // Failed to load favicon from local.
                    if (favicon == null) {
                        callback.sendError("Failed to get favicon from cache");
                    } else {
                        try {
                            out = new ByteArrayOutputStream();
                            out.write("data:image/png;base64,".getBytes());
                            b64 = new Base64OutputStream(out, Base64.NO_WRAP);
                            favicon.compress(Bitmap.CompressFormat.PNG, 100, b64);
                            callback.sendSuccess(new String(out.toByteArray()));
                        } catch (IOException e) {
                            Log.w(LOGTAG, "Failed to convert to base64 data URI");
                            callback.sendError("Failed to convert favicon to a base64 data URI");
                        } finally {
                            try {
                                if (out != null) {
                                    out.close();
                                }
                                if (b64 != null) {
                                    b64.close();
                                }
                            } catch (IOException e) {
                                Log.w(LOGTAG, "Failed to close the streams");
                            }
                        }
                    }
                }
            });
        }
    };
    Favicons.getSizedFaviconForPageFromLocal(getContext(), url, listener);
}