Java Base64 Encode Base64Encode(byte[] input, boolean addLineBreaks)

Here you can find the source of Base64Encode(byte[] input, boolean addLineBreaks)

Description

Base Encode

License

Open Source License

Declaration

public static String Base64Encode(byte[] input, boolean addLineBreaks) 

Method Source Code

//package com.java2s;
/*##############################################################################

 Copyright (C) 2011 HPCC Systems.//  w w w .j  av  a2s. c  om

 All rights reserved. This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as
 published by the Free Software Foundation, either version 3 of the
 License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program. If not, see <http://www.gnu.org/licenses/>.
 ############################################################################## */

public class Main {
    static final char pad = '=';
    static final char BASE64_enc[] = { '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', '+', '"' };

    public static String Base64Encode(byte[] input, boolean addLineBreaks) {
        int length = input.length;
        StringBuilder out = new StringBuilder("");
        char one;
        char two;
        char three;

        int i;
        for (i = 0; i < length && length - i >= 3;) {
            one = (char) input[i++];
            two = (char) input[i++];
            three = (char) input[i++];

            out.append((char) BASE64_enc[one >> 2]);
            out.append((char) BASE64_enc[((one << 4) & 0x30 | (two >> 4))]);
            out.append((char) BASE64_enc[((two << 2) & 0x3c | (three >> 6))]);
            out.append((char) BASE64_enc[three & 0x3f]);

            if (addLineBreaks && (i % 54 == 0))
                out.append("\n");

            switch (length - i) {
            case 2:
                one = (char) input[i++];
                two = (char) input[i++];

                out.append((char) BASE64_enc[one >> 2]);
                out.append((char) BASE64_enc[((one << 4) & 0x30 | (two >> 4))]);
                out.append((char) BASE64_enc[((two << 2) & 0x3c)]);
                out.append(pad);
                break;

            case 1:
                one = (char) input[i++];

                out.append((char) BASE64_enc[one >> 2]);
                out.append((char) BASE64_enc[((one << 4) & 0x30)]);
                out.append(pad);
                out.append(pad);
                break;
            }

        }
        return out.toString();
    }
}

Related

  1. base64Encode(byte[] data)
  2. base64encode(byte[] data)
  3. base64Encode(byte[] in)
  4. base64Encode(byte[] in)
  5. base64Encode(byte[] in)
  6. base64Encode(byte[] param)
  7. base64encode(final byte[] data)
  8. base64Encode(int value)
  9. base64Encode(String _s, String _enc)