Here you can find the source of encodeBase64(String data)
Parameter | Description |
---|---|
data | a String to encode. |
Parameter | Description |
---|---|
UnsupportedEncodingException | an exception |
public static String encodeBase64(String data) throws UnsupportedEncodingException
//package com.java2s; /**//from w ww .j a va2s. c o m * Converts a line of text into an array of lower case words using a * BreakIterator.wordInstance(). <p> * * This method is under the Jive Open Source Software License and was * written by Mark Imbriaco. * * @param text a String of text to convert into an array of words * @return text broken up into an array of words. */ import java.io.UnsupportedEncodingException; public class Main { private static final int fillchar = '='; private static final String cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/"; /** * Encodes a String as a base64 String. * * @param data a String to encode. * @return a base64 encoded String. * @throws UnsupportedEncodingException */ public static String encodeBase64(String data) throws UnsupportedEncodingException { return encodeBase64(data.getBytes("GBK")); } /** * Encodes a byte array into a base64 String. * * @param data a byte array to encode. * @return a base64 encode String. */ public static String encodeBase64(byte[] data) { int c; int len = data.length; StringBuffer ret = new StringBuffer(((len / 3) + 1) * 4); for (int i = 0; i < len; ++i) { c = (data[i] >> 2) & 0x3f; ret.append(cvt.charAt(c)); c = (data[i] << 4) & 0x3f; if (++i < len) c |= (data[i] >> 4) & 0x0f; ret.append(cvt.charAt(c)); if (i < len) { c = (data[i] << 2) & 0x3f; if (++i < len) c |= (data[i] >> 6) & 0x03; ret.append(cvt.charAt(c)); } else { ++i; ret.append((char) fillchar); } if (i < len) { c = data[i] & 0x3f; ret.append(cvt.charAt(c)); } else { ret.append((char) fillchar); } } return ret.toString(); } }