Java Base64 Encode base64Encode(byte[] aData)

Here you can find the source of base64Encode(byte[] aData)

Description

Encodes the given byte[] using the Base64-encoding, as specified in RFC-2045 (Section 6.8).

License

Apache License

Parameter

Parameter Description
aData the data to be encoded

Return

the Base64-encoded aData

Declaration

public static String base64Encode(byte[] aData) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    private static byte[] mBase64EncMap, mBase64DecMap;

    /**//from   w w  w.  j  a  v  a 2 s.c  o  m
     * Encodes the given byte[] using the Base64-encoding, as specified in
     * RFC-2045 (Section 6.8).
     * 
     * @param aData the data to be encoded
     * @return the Base64-encoded <var>aData</var>
     * @exception IllegalArgumentException if NULL or empty array is passed
     */
    public static String base64Encode(byte[] aData) {
        if (aData == null || aData.length == 0)
            throw new IllegalArgumentException("Can not encode NULL or empty byte array.");

        byte encodedBuf[] = new byte[(aData.length + 2) / 3 * 4];

        // 3-byte to 4-byte conversion
        int srcIndex, destIndex;
        for (srcIndex = 0, destIndex = 0; srcIndex < aData.length - 2; srcIndex += 3) {
            encodedBuf[destIndex++] = mBase64EncMap[aData[srcIndex] >>> 2 & 077];
            encodedBuf[destIndex++] = mBase64EncMap[aData[srcIndex + 1] >>> 4 & 017 | aData[srcIndex] << 4 & 077];
            encodedBuf[destIndex++] = mBase64EncMap[aData[srcIndex + 2] >>> 6 & 003
                    | aData[srcIndex + 1] << 2 & 077];
            encodedBuf[destIndex++] = mBase64EncMap[aData[srcIndex + 2] & 077];
        }

        // Convert the last 1 or 2 bytes
        if (srcIndex < aData.length) {
            encodedBuf[destIndex++] = mBase64EncMap[aData[srcIndex] >>> 2 & 077];
            if (srcIndex < aData.length - 1) {
                encodedBuf[destIndex++] = mBase64EncMap[aData[srcIndex + 1] >>> 4 & 017
                        | aData[srcIndex] << 4 & 077];
                encodedBuf[destIndex++] = mBase64EncMap[aData[srcIndex + 1] << 2 & 077];
            } else {
                encodedBuf[destIndex++] = mBase64EncMap[aData[srcIndex] << 4 & 077];
            }
        }

        // Add padding to the end of encoded data
        while (destIndex < encodedBuf.length) {
            encodedBuf[destIndex] = (byte) '=';
            destIndex++;
        }

        String result = new String(encodedBuf);
        return result;
    }

    /**
     * Encodes the given byte[] using the Base64-encoding, as specified in
     * RFC-2045 (Section 6.8).
     * 
     * @param aData the data to be encoded
     * @return the Base64-encoded <var>aData</var>
     * @exception IllegalArgumentException if NULL or empty array is passed
     */
    public static String base64Encode(byte[] aData, int offset, int len) {
        if (aData == null || aData.length == 0)
            throw new IllegalArgumentException("Can not encode NULL or empty byte array.");

        if (offset < 0 || offset >= aData.length) {
            throw new IllegalArgumentException("Offset out of range.");
        }

        int vLen = aData.length - offset;
        vLen = Math.min(len, vLen);

        byte encodedBuf[] = new byte[(vLen + 2) / 3 * 4];

        int lastIndex = offset + vLen;
        // 3-byte to 4-byte conversion
        int srcIndex, destIndex;
        for (srcIndex = offset, destIndex = 0; srcIndex < lastIndex - 2; srcIndex += 3) {
            encodedBuf[destIndex++] = mBase64EncMap[aData[srcIndex] >>> 2 & 077];
            encodedBuf[destIndex++] = mBase64EncMap[aData[srcIndex + 1] >>> 4 & 017 | aData[srcIndex] << 4 & 077];
            encodedBuf[destIndex++] = mBase64EncMap[aData[srcIndex + 2] >>> 6 & 003
                    | aData[srcIndex + 1] << 2 & 077];
            encodedBuf[destIndex++] = mBase64EncMap[aData[srcIndex + 2] & 077];
        }

        // Convert the last 1 or 2 bytes
        if (srcIndex < lastIndex) {
            encodedBuf[destIndex++] = mBase64EncMap[aData[srcIndex] >>> 2 & 077];
            if (srcIndex < lastIndex - 1) {
                encodedBuf[destIndex++] = mBase64EncMap[aData[srcIndex + 1] >>> 4 & 017
                        | aData[srcIndex] << 4 & 077];
                encodedBuf[destIndex++] = mBase64EncMap[aData[srcIndex + 1] << 2 & 077];
            } else {
                encodedBuf[destIndex++] = mBase64EncMap[aData[srcIndex] << 4 & 077];
            }
        }

        // Add padding to the end of encoded data
        while (destIndex < encodedBuf.length) {
            encodedBuf[destIndex] = (byte) '=';
            destIndex++;
        }

        String result = new String(encodedBuf);
        return result;
    }
}

Related

  1. base64Encode(byte[] aData)
  2. base64Encode(byte[] buf)
  3. base64Encode(byte[] buf, int tw)
  4. base64Encode(byte[] bytes)