Here you can find the source of Base64Encode(byte[] input, boolean addLineBreaks)
public static String Base64Encode(byte[] input, boolean addLineBreaks)
//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(); } }