Here you can find the source of encode(byte[] data)
public static char[] encode(byte[] data)
//package com.java2s; //License from project: Open Source License import java.io.BufferedInputStream; import java.io.BufferedWriter; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.Writer; public class Main { private static char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" .toCharArray();//from w w w . j a va2 s . c o m public static String encode(String data) { return new String(encode(data.getBytes())); } public static char[] encode(byte[] data) { char[] out = new char[((data.length + 2) / 3) * 4]; for (int i = 0, index = 0; i < data.length; i += 3, index += 4) { boolean quad = false; boolean trip = false; int val = (0xFF & (int) data[i]); val <<= 8; if ((i + 1) < data.length) { val |= (0xFF & (int) data[i + 1]); trip = true; } val <<= 8; if ((i + 2) < data.length) { val |= (0xFF & (int) data[i + 2]); quad = true; } out[index + 3] = alphabet[(quad ? (val & 0x3F) : 64)]; val >>= 6; out[index + 2] = alphabet[(trip ? (val & 0x3F) : 64)]; val >>= 6; out[index + 1] = alphabet[val & 0x3F]; val >>= 6; out[index + 0] = alphabet[val & 0x3F]; } return out; } public static void encode(File file) throws IOException { if (!file.exists()) { System.exit(0); } else { byte[] decoded = readBytes(file); char[] encoded = encode(decoded); writeChars(file, encoded); } file = null; } private static byte[] readBytes(File file) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] b = null; InputStream fis = null; InputStream is = null; try { fis = new FileInputStream(file); is = new BufferedInputStream(fis); int count = 0; byte[] buf = new byte[16384]; while ((count = is.read(buf)) != -1) { if (count > 0) { baos.write(buf, 0, count); } } b = baos.toByteArray(); } finally { try { if (fis != null) fis.close(); if (is != null) is.close(); if (baos != null) baos.close(); } catch (Exception e) { System.out.println(e); } } return b; } private static void writeChars(File file, char[] data) throws IOException { Writer fos = null; Writer os = null; try { fos = new FileWriter(file); os = new BufferedWriter(fos); os.write(data); } finally { try { if (os != null) os.close(); if (fos != null) fos.close(); } catch (Exception e) { e.printStackTrace(); } } } }