java Base64 编码操作

package com.hujl.oauth.signature;

/**
 * 完成Base64 编码操作
 * 也可以用android.util.Base64下的类实现
 * @author janrone
 * 
 */
public class BASE64 {
	private static final char last2byte = (char) Integer
			.parseInt("00000011", 2);
	private static final char last4byte = (char) Integer
			.parseInt("00001111", 2);
	private static final char last6byte = (char) Integer
			.parseInt("00111111", 2);
	private static final char lead6byte = (char) Integer
			.parseInt("11111100", 2);
	private static final char lead4byte = (char) Integer
			.parseInt("11110000", 2);
	private static final char lead2byte = (char) Integer
			.parseInt("11000000", 2);
	private static final char[] encodeTable = new char[] { 'A', 'B', 'C', 'D',
			'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
			'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
			'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
			'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3',
			'4', '5', '6', '7', '8', '9', '+', '/' };

	/**
	 * Base64 encoding.
	 * 
	 * @param from
	 *            The src data.
	 * @return
	 */
	public static String encode(byte[] from) {
		StringBuffer to = new StringBuffer((int) (from.length * 1.34) + 3);
		int num = 0;
		char currentByte = 0;
		for (int i = 0; i < from.length; i++) {
			num = num % 8;
			while (num < 8) {
				switch (num) {
				case 0:
					currentByte = (char) (from[i] & lead6byte);
					currentByte = (char) (currentByte >>> 2);
					break;
				case 2:
					currentByte = (char) (from[i] & last6byte);
					break;
				case 4:
					currentByte = (char) (from[i] & last4byte);
					currentByte = (char) (currentByte << 2);
					if ((i + 1) < from.length) {
						currentByte |= (from[i + 1] & lead2byte) >>> 6;
					}
					break;
				case 6:
					currentByte = (char) (from[i] & last2byte);
					currentByte = (char) (currentByte << 4);
					if ((i + 1) < from.length) {
						currentByte |= (from[i + 1] & lead4byte) >>> 4;
					}
					break;
				}
				to.append(encodeTable[currentByte]);
				num += 6;
			}
		}
		if (to.length() % 4 != 0) {
			for (int i = 4 - to.length() % 4; i > 0; i--) {
				to.append("=");
			}
		}
		return to.toString();
	}
}

Java中的Base64编码可以使用多种方式进行。在Java 8中,可以使用java.util包下的Base64类来处理Base64编码和解码。可以通过以下步骤进行编码和解码: 1. 首先,创建一个Base64编码器和解码器对象: ``` final Base64.Encoder encoder = Base64.getEncoder(); final Base64.Decoder decoder = Base64.getDecoder(); ``` 2. 接下来,准备需要编码的文本,将其转换为字节数组: ``` final String text = "字串文字"; final byte[] textByte = text.getBytes("UTF-8"); ``` 3. 进行编码,将字节数组转换为Base64字符串: ``` final String encodedText = encoder.encodeToString(textByte); System.out.println(encodedText); ``` 4. 进行解码,将Base64字符串转换为字节数组,并重新转换为文本: ``` System.out.println(new String(decoder.decode(encodedText), "UTF-8")); ``` 关于Base64编码和解码,还有其他方式可以实现。在早期的Java版本中,可以使用sun.misc套件下的BASE64Encoder和BASE64Decoder类。这种方式的缺点是效率较低。示例代码如下: ``` final BASE64Encoder encoder = new BASE64Encoder(); final BASE64Decoder decoder = new BASE64Decoder(); final String text = "字串文字"; final byte[] textByte = text.getBytes("UTF-8"); //编码 final String encodedText = encoder.encode(textByte); System.out.println(encodedText); //解码 System.out.println(new String(decoder.decodeBuffer(encodedText), "UTF-8")); ``` 另外,还可以使用org.apache.commons.codec.binary.Base64包下的Base64类进行编码和解码。这种方式相比sun包更加精简,并且执行效率更高。示例代码如下: ``` final Base64 base64 = new Base64(); final String text = "字串文字"; final byte[] textByte = text.getBytes("UTF-8"); //编码 final String encodedText = base64.encodeToString(textByte); System.out.println(encodedText); //解码 System.out.println(new String(base64.decode(encodedText), "UTF-8")); ``` 总而言之,Java中有多种方式可以进行Base64编码和解码,包括java.util包下的Base64类、sun.misc包下的BASE64Encoder和BASE64Decoder类以及org.apache.commons.codec.binary.Base64包下的Base64类。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值