Here you can find the source of base64encode(String str)
public static String base64encode(String str)
//package com.java2s; //License from project: Open Source License import java.io.UnsupportedEncodingException; public class Main { static String base64alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-."; public static String base64encode(String str) { byte[] utf8; try {//from ww w. ja v a 2 s . c o m utf8 = str.getBytes("UTF-8"); } catch (UnsupportedEncodingException ex) { throw new InternalError(ex.toString()); } StringBuffer sb = new StringBuffer((utf8.length + 2) / 3 * 4); int i; int len = utf8.length; for (i = 0; i < len - 2; i += 3) { int value = (utf8[i] & 0xff) << 16 | (utf8[i + 1] & 0xff) << 8 | (utf8[i + 2] & 0xff); sb.append(base64alphabet.charAt(value >> 18)) .append(base64alphabet.charAt((value >> 12) & 0x3f)) .append(base64alphabet.charAt((value >> 6) & 0x3f)) .append(base64alphabet.charAt(value & 0x3f)); } if (i < len) { int value = utf8[i++] & 0xff; sb.append(base64alphabet.charAt(value >> 2)); if (i < len) { value = (value << 8) + (utf8[i++] & 0xff); sb.append(base64alphabet.charAt((value >> 4) & 0x3f)); sb.append(base64alphabet.charAt((value << 2) & 0x3f)); } else { sb.append(base64alphabet.charAt((value << 4) & 0x3f)); sb.append('='); } sb.append('='); } return sb.toString(); } }