Java Base64 Encode toBase64(byte[] data)

Here you can find the source of toBase64(byte[] data)

Description

Converts a byte array into a base 64 encoded string.

License

Apache License

Parameter

Parameter Description
data a byte array, which may be null or empty

Return

a String

Declaration

public static String toBase64(byte[] data) 

Method Source Code

//package com.java2s;
/*/*from   w  ww .j  a  v a  2s. com*/
 * Copyright 2009 Google Inc.
 *
 * 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.
 */

public class Main {
    /**
     * An array mapping size but values to the characters that will be used to
     * represent them. Note that this is not identical to the set of characters
     * used by MIME-Base64.
     */
    private static final char[] base64Chars = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
            'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
            'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0',
            '1', '2', '3', '4', '5', '6', '7', '8', '9', '$', '_' };

    /**
     * Converts a byte array into a base 64 encoded string. Null is encoded as
     * null, and an empty array is encoded as an empty string. Otherwise, the byte
     * data is read 3 bytes at a time, with bytes off the end of the array padded
     * with zeros. Each 24-bit chunk is encoded as 4 characters from the sequence
     * [A-Za-z0-9$_]. If one of the source positions consists entirely of padding
     * zeros, an '=' character is used instead.
     *
     * @param data a byte array, which may be null or empty
     * @return a String
     */
    public static String toBase64(byte[] data) {
        if (data == null) {
            return null;
        }

        int len = data.length;
        if (len == 0) {
            return "";
        }

        int olen = 4 * ((len + 2) / 3);
        char[] chars = new char[olen];

        int iidx = 0;
        int oidx = 0;
        int charsLeft = len;
        while (charsLeft > 0) {
            int b0 = data[iidx++] & 0xff;
            int b1 = (charsLeft > 1) ? data[iidx++] & 0xff : 0;
            int b2 = (charsLeft > 2) ? data[iidx++] & 0xff : 0;
            int b24 = (b0 << 16) | (b1 << 8) | b2;

            int c0 = (b24 >> 18) & 0x3f;
            int c1 = (b24 >> 12) & 0x3f;
            int c2 = (b24 >> 6) & 0x3f;
            int c3 = b24 & 0x3f;

            chars[oidx++] = base64Chars[c0];
            chars[oidx++] = base64Chars[c1];
            chars[oidx++] = (charsLeft > 1) ? base64Chars[c2] : '=';
            chars[oidx++] = (charsLeft > 2) ? base64Chars[c3] : '=';

            charsLeft -= 3;
        }

        return new String(chars);
    }

    /**
     * Return a string containing a base-64 encoded version of the given long
     * value.  Leading groups of all zero bits are omitted.
     */
    public static String toBase64(long value) {
        // Convert to ints early to avoid need for long ops
        int low = (int) (value & 0xffffffff);
        int high = (int) (value >> 32);

        StringBuilder sb = new StringBuilder();
        boolean haveNonZero = base64Append(sb, (high >> 28) & 0xf, false);
        haveNonZero = base64Append(sb, (high >> 22) & 0x3f, haveNonZero);
        haveNonZero = base64Append(sb, (high >> 16) & 0x3f, haveNonZero);
        haveNonZero = base64Append(sb, (high >> 10) & 0x3f, haveNonZero);
        haveNonZero = base64Append(sb, (high >> 4) & 0x3f, haveNonZero);
        int v = ((high & 0xf) << 2) | ((low >> 30) & 0x3);
        haveNonZero = base64Append(sb, v, haveNonZero);
        haveNonZero = base64Append(sb, (low >> 24) & 0x3f, haveNonZero);
        haveNonZero = base64Append(sb, (low >> 18) & 0x3f, haveNonZero);
        haveNonZero = base64Append(sb, (low >> 12) & 0x3f, haveNonZero);
        base64Append(sb, (low >> 6) & 0x3f, haveNonZero);
        base64Append(sb, low & 0x3f, true);

        return sb.toString();
    }

    private static boolean base64Append(StringBuilder sb, int digit, boolean haveNonZero) {
        if (digit > 0) {
            haveNonZero = true;
        }
        if (haveNonZero) {
            sb.append(base64Chars[digit]);
        }
        return haveNonZero;
    }
}

Related

  1. encodeBase64(String data)
  2. toBase64(byte[] array)
  3. toBase64(byte[] aValue)
  4. toBase64(byte[] buf, int start, int length)
  5. toBase64(byte[] data)
  6. toBase64(final byte[] bytes)
  7. toBase64(final byte[] value)
  8. toBase64(final String str)
  9. toBase64(int x)