Here you can find the source of base64(byte[] buf)
Parameter | Description |
---|---|
buf | The byte array to encode. |
public static String base64(byte[] buf)
//package com.java2s; /*//from w w w . jav a2s .co m * Copyright (C) 2012 McEvoy Software Ltd * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ public class Main { /** The characters for Base64 encoding. */ public static final String BASE_64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; /** * Base-64 encode a byte array, returning the returning string. * * <p>Note that this method exists merely to be compatible with the * challenge-response authentication method of rsyncd. It is * <em>not</em> technincally a Base-64 encoder. * * @param buf The byte array to encode. * @return <tt>buf</tt> encoded in Base64. */ public static String base64(byte[] buf) { int bitOffset, byteOffset, index = 0; int bytes = (buf.length * 8 + 5) / 6; StringBuilder out = new StringBuilder(bytes); for (int i = 0; i < bytes; i++) { byteOffset = (i * 6) / 8; bitOffset = (i * 6) % 8; if (bitOffset < 3) { index = (buf[byteOffset] >>> (2 - bitOffset)) & 0x3f; } else { index = (buf[byteOffset] << (bitOffset - 2)) & 0x3f; if (byteOffset + 1 < buf.length) { index |= (buf[byteOffset + 1] & 0xff) >>> (8 - (bitOffset - 2)); } } out.append(BASE_64.charAt(index)); } return out.toString(); } }