自己写的一个base64转换用途Java程序
2013-03-04 15:38
309 查看
看看代码
结果可以作为url组件, 并且保持相同于源字节数组大小关系 (按从0偏移处比较, 鸡蛋大头开始, 不知道喜欢大头的多还是小头的多)。
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.StringWriter; import java.util.Arrays; import java.util.HashMap; import java.util.Map; public class MyBase64 { private static final char[] set = ".0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz" .toCharArray(); /* * 结尾表示填充0x0字符数目 */ static final char blank = '-'; /* * 间隔三个字符分段 */ static final int step = 3; /* * 源数组结尾填0 */ static final byte fill = 0x0; private static MyBase64A myBase64 = null; public static MyBase64A getInstance() { if (myBase64 == null) { synchronized (MyBase64A.class) { if (myBase64 == null) { myBase64 = new MyBase64A(); } } } return myBase64; // return threadLocal.get(); } private static Map<Character, Integer> initMap() { Map<Character, Integer> hashMap = new HashMap<Character, Integer>( set.length); for (int i = 0; i < set.length; i++) { hashMap.put(set[i], i); } return hashMap; } private final Map<Character, Integer> map; private MyBase64A() { map = initMap(); } public byte[] reverseTrans(String string) throws IOException { char[] chars = string.toCharArray(); final int step = 4; int approximation = chars.length * 3 / 4; ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream( approximation); long swap; char c1, c2, c3, c4; byte b1, b2, b3; int begin = 0; int end = chars.length; int offset = begin; while (offset + 3 < end) { c1 = chars[offset]; c2 = chars[offset + 1]; c3 = chars[offset + 2]; c4 = chars[offset + 3]; swap = (map.get(c1) & 0xff) << 18 | (map.get(c2) & 0xff) << 12 | (map.get(c3) & 0xff) << 6 | (map.get(c4) & 0xff); b1 = (byte) ((swap >> 16) & 0xff); b2 = (byte) ((swap >> 8) & 0xff); b3 = (byte) (swap & 0xff); byteArrayOutputStream.write(b1); byteArrayOutputStream.write(b2); byteArrayOutputStream.write(b3); offset += step; } // logger.debug(end - offset); byte[] bytes = Arrays.copyOf(byteArrayOutputStream.toByteArray(), byteArrayOutputStream.size() - (end - offset)); return bytes; } /** * * @param source * 总之是一个字节队列。 * @return url相容的 自制 base64编码 */ public String trans(byte[] source) { int begin = 0, end = source.length; /* * (source.length + 2) / 3 处以三,余数上限取整。 + 2 , 可能追加至多两个填充占位符。 */ int approximation = (source.length + 2) / 3 * 4 + 2; StringWriter stringWriter = new StringWriter(approximation); int offset; long swap; byte a, b, c; int num1, num2, num3, num4; int app = 0; offset = begin; while (offset < end) { a = source[offset]; if (offset + 1 < end) { b = source[offset + 1]; } else { b = fill; app++; } if (offset + 2 < end) { c = source[offset + 2]; } else { c = fill; app++; } swap = ((a & 0xff) << 16) | ((b & 0xff) << 8) | c & 0xff; // logger.debug(String.format("%x", swap)); num1 = (int) ((swap >> 18) & 63); num2 = (int) ((swap >> 12) & 63); num3 = (int) ((swap >> 6) & 63); num4 = (int) (swap & 63); stringWriter.append(set[num1]); stringWriter.append(set[num2]); stringWriter.append(set[num3]); stringWriter.append(set[num4]); offset += step; } if (app == 1) { stringWriter.append(blank); } if (app == 2) { stringWriter.append(blank); stringWriter.append(blank); } return stringWriter.toString(); } }
结果可以作为url组件, 并且保持相同于源字节数组大小关系 (按从0偏移处比较, 鸡蛋大头开始, 不知道喜欢大头的多还是小头的多)。
相关文章推荐
- 自己编写的一个基于java的socket网络聊天程序
- 自己转换Java程序给BlackBerry使用
- Java GUI编程之(四) 一个温度转换程序
- 用java写一个给自己QQ邮箱发一封电子邮件的程序
- java程序中,将一个足够大的正整数,转换为二进制方法
- 用java编写一个程序,将浮点数转换成人民币读法字符串
- java自己写的一个小程序
- java程序---将一个浮点数转换成人民币读法
- 黑马程序员——一个最基本的小程序以及自己对Java的认识
- 一个自己编写的xml和JAVA对象之间的转换工具,xml和对象轻松转换(simple-xml2object)
- 自己写的一个洗扑克牌的java程序
- json数据与JAVA数据的转换 jsonJavaBean.netApache 自己编写了一个工具类,处理页面提交json格式数据到后台,再进行处理成JAVA对象数据 1、DTO:Data T
- 如何将一个用utf-8编码的文本用java程序转换成ANSI编码的文本
- 自己封装的一个Java版图片工具,具备压缩,伸缩变换,透明处理,格式转换等功能.
- 如何用Java自己写一个.exe程序
- Java基础---Java---网络编程---TCP的传输、客户端和服务端的互访、建立一个文本转换器、编写一个聊天程序
- 编写一个Java程序,计算半径为3.0的圆周长和面积并输出结果
- 用INNO SETUP做安装程序时,如何自动注册一个自己开发的DLL文件??
- 一个tomcat下执行java程序报错问题的解决
- java中的关于Date类中的一个程序