Example usage for android.util Base64OutputStream write

List of usage examples for android.util Base64OutputStream write

Introduction

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

Prototype

public void write(byte[] b, int off, int len) throws IOException 

Source Link

Usage

From source file:com.android.yijiang.kzx.http.JsonStreamerEntity.java

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

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

    int bytesRead, bytesWritten = 0, totalSize = (int) wrapper.file.length();

    // 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);
        bytesWritten += bytesRead;/*from www.ja v a 2s . com*/
        progressHandler.sendProgressMessage(bytesWritten, totalSize);
    }

    // Close the Base64 output stream.
    AsyncHttpClient.silentCloseOutputStream(bos);

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

    // Safely close the input stream.
    AsyncHttpClient.silentCloseInputStream(in);
}

From source file:com.amytech.android.library.utils.asynchttp.JsonStreamerEntity.java

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

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

    int bytesRead;
    long bytesWritten = 0, totalSize = wrapper.file.length();

    // 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);
        bytesWritten += bytesRead;/*from  ww w .  j a  v  a  2 s  . c o m*/
        progressHandler.sendProgressMessage(bytesWritten, totalSize);
    }

    // Close the Base64 output stream.
    AsyncHttpClient.silentCloseOutputStream(bos);

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

    // Safely close the input stream.
    AsyncHttpClient.silentCloseInputStream(in);
}

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  ww.  j  av a 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.amytech.android.library.utils.asynchttp.JsonStreamerEntity.java

private void writeToFromStream(OutputStream os, RequestParams.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   w w w  .  ja v a 2s .c o m*/

    // Close the Base64 output stream.
    AsyncHttpClient.silentCloseOutputStream(bos);

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

    // Close input stream.
    if (entry.autoClose) {
        // Safely close the input stream.
        AsyncHttpClient.silentCloseInputStream(entry.inputStream);
    }
}

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   ww w  . ja  v  a 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: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 ww  w .  ja  v a 2  s . com*/
    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: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  w  w.  j  ava  2  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();
}