Android Base64 InputStream Encode streamToBase64(InputStream in, StringBuilder sb)

Here you can find the source of streamToBase64(InputStream in, StringBuilder sb)

Description

stream To Base

Declaration

public static void streamToBase64(InputStream in, StringBuilder sb)
            throws IOException 

Method Source Code

//package com.java2s;
import java.io.IOException;
import java.io.InputStream;

import android.util.Base64;

public class Main {
    private static final int BASE64_DATA_URI = Base64.NO_WRAP;

    public static void streamToBase64(InputStream in, StringBuilder sb)
            throws IOException {
        int buflen = 4096;
        byte[] buffer = new byte[buflen];
        int offset = 0;
        int len = 0;
        while (len != -1) {
            len = in.read(buffer, offset, buffer.length - offset);
            if (len != -1) {
                // must process a multiple of 3 bytes, so that no padding chars 
                // are placed 
                int total = offset + len;
                offset = total % 3;//from   w  ww . j av a 2  s . c  om
                int bytesToProcess = total - offset;
                if (0 < bytesToProcess) {
                    sb.append(Base64.encodeToString(buffer, 0,
                            bytesToProcess, BASE64_DATA_URI));
                }
                // shuffle unused bytes to start of array
                System.arraycopy(buffer, bytesToProcess, buffer, 0, offset);
            } else if (0 < offset) {
                // flush
                sb.append(Base64.encodeToString(buffer, 0, offset,
                        BASE64_DATA_URI));
            }
        }
        in.close();
    }
}

Related

  1. encode(InputStream in, Writer writer)
  2. encode(InputStream in, OutputStream out)