Here you can find the source of base64EncodeGeneric(String digits, byte[] data)
public static String base64EncodeGeneric(String digits, byte[] data)
//package com.java2s; //License from project: Open Source License public class Main { private static final char[] HexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', }; public static String base64EncodeGeneric(String digits, byte[] data) { if (data == null) throw new IllegalArgumentException("'data' can't be null"); if (digits == null) throw new IllegalArgumentException("'digits' can't be null"); if (digits.length() != 64) throw new IllegalArgumentException("'digits' must be 64 characters long: " + jq(digits)); int numGroupsOfThreeInputBytes = (data.length + 2) / 3; int numOutputChars = numGroupsOfThreeInputBytes * 4; StringBuilder buf = new StringBuilder(numOutputChars); // Do chunks of three bytes at a time. int i = 0; while ((i + 3) <= data.length) { int b1 = ((int) data[i++]) & 0xff; int b2 = ((int) data[i++]) & 0xff; int b3 = ((int) data[i++]) & 0xff; int d1 = b1 >>> 2; int d2 = ((b1 & 0x3) << 4) | (b2 >>> 4); int d3 = ((b2 & 0xf) << 2) | (b3 >>> 6); int d4 = b3 & 0x3f; buf.append(digits.charAt(d1)); buf.append(digits.charAt(d2)); buf.append(digits.charAt(d3)); buf.append(digits.charAt(d4)); }// w ww .jav a 2 s . c om // Do the leftover bytes (either 1 or 2) int remaining = data.length - i; if (remaining == 0) { // All done. } else if (remaining == 1) { int b1 = ((int) data[i++]) & 0xff; int d1 = b1 >>> 2; int d2 = (b1 & 0x3) << 4; buf.append(digits.charAt(d1)); buf.append(digits.charAt(d2)); buf.append("=="); } else if (remaining == 2) { int b1 = ((int) data[i++]) & 0xff; int b2 = ((int) data[i++]) & 0xff; int d1 = b1 >>> 2; int d2 = ((b1 & 0x3) << 4) | (b2 >>> 4); int d3 = ((b2 & 0xf) << 2); buf.append(digits.charAt(d1)); buf.append(digits.charAt(d2)); buf.append(digits.charAt(d3)); buf.append('='); } else { throw new AssertionError("data.length: " + data.length + ", i: " + i); } return buf.toString(); } /** Shorthand for {@link #javaQuotedLiteral}. */ public static String jq(String value) { return javaQuotedLiteral(value); } /** * Given a string, returns the representation of that string * as a Java string literal. */ public static String javaQuotedLiteral(String value) { StringBuilder b = new StringBuilder(value.length() * 2); b.append('"'); for (int i = 0; i < value.length(); i++) { char c = value.charAt(i); switch (c) { case '"': b.append("\\\""); break; case '\\': b.append("\\\\"); break; case '\n': b.append("\\n"); break; case '\r': b.append("\\t"); break; case '\t': b.append("\\r"); break; case '\0': b.append("\\000"); break; // Inserting '\0' isn't safe if there's a digit after default: if (c >= 0x20 && c <= 0x7e) { b.append(c); } else { int h1 = (c >> 12) & 0xf; int h2 = (c >> 8) & 0xf; int h3 = (c >> 4) & 0xf; int h4 = c & 0xf; b.append("\\u"); b.append(hexDigit(h1)); b.append(hexDigit(h2)); b.append(hexDigit(h3)); b.append(hexDigit(h4)); } break; } } b.append('"'); return b.toString(); } public static char hexDigit(int i) { return HexDigits[i]; } }