Base64 encodes the supplied bytes array, using the standard base 64 encoding algorithm. - Java java.lang

Java examples for java.lang:String Base64

Description

Base64 encodes the supplied bytes array, using the standard base 64 encoding algorithm.

Demo Code

/**/* ww w . j  a  v  a2s.  c om*/
 * Copyright (c) 2000-2016 Liferay, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;

public class Main {
    private static char[] sCharLookup;

    /**
     * Base64 encodes the supplied bytes array, using the standard base 64 encoding algorithm.
     *
     * @param   bytes  The byte array to encode
     *
     * @return  The base 64 encoded string representing the byte array
     */
    public static String base64Encode(byte[] bytes) {

        /*
         * The base 64 encoding algorithm works as follows:
         *
         * Divide the input bytes stream into blocks of 3 bytes. Divide the 24 bits of a 3-byte block into 4 groups of 6
         * bits. Map each group of 6 bits to 1 printable character, based on the 6-bit value. If the last 3-byte block
         * has only 1 byte of input data, pad 2 bytes of zero (\x0000). After encoding it as a normal block, override
         * the last 2 characters with 2 equal signs (==), so the decoding process knows 2 bytes of zero were padded. If
         * the last 3-byte block has only 2 bytes of input data, pad 1 byte of zero (\x00). After encoding it as a
         * normal block, override the last 1 character with 1 equal signs (=), so the decoding process knows 1 byte of
         * zero was padded.
         */

        int i = 0;
        int bytesToWrite = bytes.length;

        StringBuilder buff = new StringBuilder(bytes.length * 4 / 3);

        while (bytesToWrite >= 3) {
            buff.append(sCharLookup[(bytes[i] >>> 2) & 63]);

            buff.append(sCharLookup[((bytes[i] & 3) << 4)
                    + ((bytes[i + 1] >>> 4) & 15)]);
            buff.append(sCharLookup[((bytes[i + 1] & 15) << 2)
                    + ((bytes[i + 2] >>> 6) & 3)]);
            buff.append(sCharLookup[bytes[i + 2] & 63]);

            bytesToWrite -= 3;
            i = i + 3;
        }

        switch (bytesToWrite) {

        case 2: {
            buff.append(sCharLookup[(bytes[i] >>> 2) & 63]);
            buff.append(sCharLookup[((bytes[i] & 3) << 4)
                    + ((bytes[i + 1] >>> 4) & 15)]);
            buff.append(sCharLookup[((bytes[i + 1] & 15) << 2)]);
            buff.append('=');

            break;
        }

        case 1: {
            buff.append(sCharLookup[(bytes[i] >> 2) & 63]);
            buff.append(sCharLookup[(bytes[i] & 3) << 4]);
            buff.append('=');
            buff.append('=');
        }
        }

        return buff.toString();
    }
}

Related Tutorials