Java Base64 Encode encodeBASE64(InputStream in, OutputStream out)

Here you can find the source of encodeBASE64(InputStream in, OutputStream out)

Description

This implements a BASE64 character decoder as specified in RFC1521.

License

Open Source License

Return

number of bytes read from the input stream

Declaration

public static int encodeBASE64(InputStream in, OutputStream out)
        throws IOException 

Method Source Code

//package com.java2s;
/*/*from ww  w . j av  a  2  s  .com*/
 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 * This component and the accompanying materials are made available
 * under the terms of "Eclipse Public License v1.0"
 * which accompanies this distribution, and is available
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
 *
 * Initial Contributors:
 * Nokia Corporation - initial contribution.
 *
 * Contributors:
 *
 * Description: 
 *
 */

import java.io.IOException;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

public class Main {
    /**
     * This character array provides the character to value map
     * based on RFC1521.
     */
    private static final char base64EncodeMap[] = {
            //   0   1   2   3   4   5   6   7
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 0
            'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 1
            'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 2
            'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', // 3
            'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 4
            'o', 'p', 'q', 'r', 's', 't', 'u', 'v', // 5
            'w', 'x', 'y', 'z', '0', '1', '2', '3', // 6
            '4', '5', '6', '7', '8', '9', '+', '/' // 7
    };

    /**
     * This implements a BASE64 character decoder as specified
     * in RFC1521.
     *
     * @return number of bytes read from the input stream
     */
    public static int encodeBASE64(InputStream in, OutputStream out)
            throws IOException {
        int len;
        int count = 0;
        byte[] chunk = new byte[3];
        while ((len = in.read(chunk)) > 0) {
            byte a, b, c;
            count += len;
            if (len == 1) {
                a = chunk[0];
                b = 0;
                c = 0;
                out.write(base64EncodeMap[(a >>> 2) & 0x3f]);
                out.write(base64EncodeMap[((a << 4) & 0x30)
                        + ((b >>> 4) & 0xf)]);
                out.write('=');
                out.write('=');
                break;
            } else if (len == 2) {
                a = chunk[0];
                b = chunk[1];
                c = 0;
                out.write(base64EncodeMap[(a >>> 2) & 0x3f]);
                out.write(base64EncodeMap[((a << 4) & 0x30)
                        + ((b >>> 4) & 0xf)]);
                out.write(base64EncodeMap[((b << 2) & 0x3c)
                        + ((c >>> 6) & 0x3)]);
                out.write('=');
                break;
            } else {
                a = chunk[0];
                b = chunk[1];
                c = chunk[2];
                out.write(base64EncodeMap[(a >>> 2) & 0x3f]);
                out.write(base64EncodeMap[((a << 4) & 0x30)
                        + ((b >>> 4) & 0xf)]);
                out.write(base64EncodeMap[((b << 2) & 0x3c)
                        + ((c >>> 6) & 0x3)]);
                out.write(base64EncodeMap[c & 0x3F]);
                // continue reading input
            }
        }
        return count;
    }

    /**
     * Encodes binary data into a String according to BASE64 algorithm
     * as specified in RFC1521. If byte array is <code<null</code> or
     * empty, returns an empty string
     */
    public static String encodeBASE64(byte[] data) {
        if (data != null && data.length > 0) {
            try {
                int size = 4 * (data.length / 3);
                if (data.length % 3 != 0)
                    size += 4;
                ByteArrayOutputStream out = new ByteArrayOutputStream(size);
                encodeBASE64(new ByteArrayInputStream(data), out);
                return new String(out.toByteArray());
            } catch (IOException x) {
                // this should never happen
                x.printStackTrace();
            }
        }
        return "";
    }
}

Related

  1. base64Encoder(byte[] src, int start, int wrapAt)
  2. base64Encoder(String str)
  3. base64encoding(byte[] bytes)
  4. base64UrlDecode(final String encodedString)
  5. base64UrlEncode(final byte[] v)
  6. encodeBase64(InputStream is, OutputStream os)
  7. encodeBase64(String data)
  8. encodeBase64(String data)
  9. toBase64(byte[] array)