public final class AbBase64 {

    /** The Constant base64EncodeChars. */
private static final char[] base64EncodeChars = 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', '+', '/'}; /** The Constant base64DecodeChars. */
private static final byte[] base64DecodeChars = new byte[] {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1,
-1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1}; /**
* Encode.
*
* @param str the str
* @param charsetName the charset name
* @return the string
*/
public static final String encode(String str, String charsetName) {
return encode(str, charsetName, 0);
} /**
* Encode.
*
* @param str the str
* @param charsetName the charset name
* @param width the width
* @return the string
*/
public static final String encode(String str, String charsetName, int width) {
byte[] data = null;
try {
data = str.getBytes(charsetName);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
int length = data.length;
int size = (int) Math.ceil(length * 1.36);
int splitsize = width > 0 ? size / width : 0;
StringBuffer sb = new StringBuffer(size + splitsize);
int r = length % 3;
int len = length - r;
int i = 0;
int c;
while (i < len) {
c = (0x000000ff & data[i++]) << 16 | (0x000000ff & data[i++]) << 8 | (0x000000ff & data[i++]);
sb.append(base64EncodeChars[c >> 18]);
sb.append(base64EncodeChars[c >> 12 & 0x3f]);
sb.append(base64EncodeChars[c >> 6 & 0x3f]);
sb.append(base64EncodeChars[c & 0x3f]);
}
if (r == 1) {
c = 0x000000ff & data[i++];
sb.append(base64EncodeChars[c >> 2]);
sb.append(base64EncodeChars[(c & 0x03) << 4]);
sb.append("==");
} else if (r == 2) {
c = (0x000000ff & data[i++]) << 8 | (0x000000ff & data[i++]);
sb.append(base64EncodeChars[c >> 10]);
sb.append(base64EncodeChars[c >> 4 & 0x3f]);
sb.append(base64EncodeChars[(c & 0x0f) << 2]);
sb.append("=");
}
if (splitsize > 0) {
char split = '\n';
for (i = width; i < sb.length(); i++) {
sb.insert(i, split);
i += width;
}
}
return sb.toString();
} /**
* Decode.
*
* @param str the str
* @param charsetName the charset name
* @return the string
*/
public static final String decode(String str, String charsetName) {
byte[] data = null;
try {
data = str.getBytes(charsetName);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
int len = data.length;
ByteArrayOutputStream buf = new ByteArrayOutputStream((int) (len * 0.67));
int i = 0;
int b1, b2, b3, b4;
while (i < len) {
do {
if (i >= len) {
b1 = -1;
break;
}
b1 = base64DecodeChars[data[i++]];
} while (i < len && b1 == -1);
if (b1 == -1) {
break;
}
do {
if (i >= len) {
b2 = -1;
break;
}
b2 = base64DecodeChars[data[i++]];
} while (i < len && b2 == -1);
if (b2 == -1) {
break;
}
buf.write((int) ((b1 << 2) | ((b2 & 0x30) >>> 4)));
do {
if (i >= len) {
b3 = -1;
break;
}
b3 = data[i++];
if (b3 == 61) {
b3 = -1;
break;
}
b3 = base64DecodeChars[b3];
} while (i < len && b3 == -1);
if (b3 == -1) {
break;
}
buf.write((int) (((b2 & 0x0f) << 4) | ((b3 & 0x3c) >>> 2)));
do {
if (i >= len) {
b4 = -1;
break;
}
b4 = data[i++];
if (b4 == 61) {
b4 = -1;
break;
}
b4 = base64DecodeChars[b4];
} while (b4 == -1);
if (b4 == -1) {
break;
}
buf.write((int) (((b3 & 0x03) << 6) | b4));
}
try {
return buf.toString(charsetName);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
}

author 还如一梦中

Base64工具类的更多相关文章

  1. Base64工具类并发问题!

    为减少对象创建次数,一般会做如下编码: public class EncodeUtils { private static BASE64Encoder encoder; private static ...

  2. Java开发工具类集合

    Java开发工具类集合 01.MD5加密工具类 import java.security.MessageDigest; import java.security.NoSuchAlgorithmExce ...

  3. ios Base64编解码工具类及使用

    为了避免明码传递http内容,可以用base64编码后传输,收到方再解码,也方便了2进制数据的字符串式传输. 对于ios来说,google给提供了一个很好的工具类,方便进行base64编解码,当然也可 ...

  4. Base64加密解密工具类

    使用Apache commons codec类Base64进行加密解密 maven依赖 <dependency> <groupId>commons-codec</grou ...

  5. Android—关于自定义对话框的工具类

    开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函 ...

  6. App开发流程之加密工具类

    科技优家 2016-09-08 18:10 从这篇记录开始,记录的都算是干货了,都是一些编程日常的积累. 我建议先将基础的工具加入项目,后续的开发效率会呈指数增长.如果在专注功能开发过程中,才发现缺少 ...

  7. Java的各种工具类

    下面是java的各种工具,包括获取时间和时间比较,检验集合和字符串是否为空和长度大小等等 1 import java.io.BufferedReader; import java.io.File; i ...

  8. java中常用的工具类(三)

    继续分享java中常用的一些工具类.前两篇的文章中有人评论使用Apache 的lang包和IO包,或者Google的Guava库.后续的我会加上的!谢谢支持IT江湖 一.连接数据库的综合类       ...

  9. IP工具类-自己动手做个ip解析器

    IP工具类-自己动手做个ip解析器 一.资料准备 导入依赖包:

随机推荐

  1. 【技术贴】Eclipse 右键打开当前文件所在文件夹

    1.使用插件,百度:OpenExplorer_1.5.0.v201108051513.jar 2.默认情况下使用eclipse打开当前文件所在文件夹很麻烦,需要右键点击 Package Explore ...

  2. maven 根据不同的环境打war包-->资源文件的处理方式

    发现犯的错误: 1. 指定了testResource 文件夹与resource 为同一个文件夹.导致不论在resource 里面如何过滤文件,都不起作用.资源文件本来就是共享的.不必这样指定. 2. ...

  3. csu 10月 月赛 A 题

    Welcome to CSU OnlineJudge Problem A: Small change Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 15 ...

  4. 【BZOJ 2005】[Noi2010]能量采集 (容斥原理| 欧拉筛+ 分块)

    能量采集 Description 栋栋有一块长方形的地,他在地上种了一种能量植物,这种植物可以采集太阳光的能量.在这些植物采集能量后,栋栋再使用一个能量汇集机器把这些植物采集到的能量汇集到一起. 栋栋 ...

  5. [topcoder]IncreasingSubsequences

    http://community.topcoder.com/stat?c=problem_statement&pm=7753&rd=10672 http://community.top ...

  6. 如何使用mysql的grant命令(详解)

    grant命令的基本格式 grant 权限 on 数据库对象 to 用户 实例一:在任意ip地址登陆的common_user用户可以对testdb数据库里的数据进行查询操作.插入操作.更新操作.删除操 ...

  7. Lea指令计算地址(用于四则混合运算),附上一个函数调用例子及其反汇编代码,很清楚

    比如你用local在栈上定义了一个局部变量LocalVar,你知道实际的指令是什么么?一般都差不多像下面的样子:     push   ebp     mov   esp,   ebp     sub ...

  8. Android ContentProvider和getContentResolver

    安卓系统中的数据库SqlLite操作和java中mysql的数据库操作很不一样,造成这样的原因是因为在安卓中数据库是属于进程的不存在数据库客户端,也不存在数据库服务器. 关于SqlLite数据库的文章 ...

  9. perl 对象

    唯一标识: 很明显,一个%employee 是不够的,每个雇员都要求有一个唯一标识和他或她自己的属性集合. 你可以动态的分配这个数据结构,也可以返回一个指向局部数据结构的引用 Vsftp:/root/ ...

  10. javascript外部使用

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...