代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
最快的 Base64 编码/解码 算法
作者:
妖魔舞
/ 发布于
2012/11/19
/
597
最快的 Base64 编码/解码 算法
import java.util.Arrays; import java.io.UnsupportedEncodingException; /** * One of the <b>fastest</b> Base64 encoder/decoder implementations. Base64 * encoding is defined in RFC 2045. */ public class Base64 { private static final char[] CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" .toCharArray(); private static final int[] INV = new int[256]; static { Arrays.fill(INV, -1); for (int i = 0, iS = CHARS.length; i < iS; i++) { INV[CHARS[i]] = i; } INV['='] = 0; } /** * Returns Base64 characters, a clone of used array. */ public static char[] getAlphabet() { return CHARS.clone(); } // ---------------------------------------------------------------- char[] public static char[] encodeToChar(String s) { try { return encodeToChar(s.getBytes("UTF-8"), false); } catch (UnsupportedEncodingException ignore) { return null; } } public static char[] encodeToChar(byte[] arr) { return encodeToChar(arr, false); } /** * Encodes a raw byte array into a BASE64 char[]. */ public static char[] encodeToChar(byte[] arr, boolean lineSeparator) { int len = arr != null ? arr.length : 0; if (len == 0) { return new char[0]; } int evenlen = (len / 3) * 3; int cnt = ((len - 1) / 3 + 1) << 2; int destLen = cnt + (lineSeparator ? (cnt - 1) / 76 << 1 : 0); char[] dest = new char[destLen]; for (int s = 0, d = 0, cc = 0; s < evenlen;) { int i = (arr[s++] & 0xff) << 16 | (arr[s++] & 0xff) << 8 | (arr[s++] & 0xff); dest[d++] = CHARS[(i >>> 18) & 0x3f]; dest[d++] = CHARS[(i >>> 12) & 0x3f]; dest[d++] = CHARS[(i >>> 6) & 0x3f]; dest[d++] = CHARS[i & 0x3f]; if (lineSeparator && (++cc == 19) && (d < (destLen - 2))) { dest[d++] = '\r'; dest[d++] = '\n'; cc = 0; } } int left = len - evenlen; // 0 - 2. if (left > 0) { int i = ((arr[evenlen] & 0xff) << 10) | (left == 2 ? ((arr[len - 1] & 0xff) << 2) : 0); dest[destLen - 4] = CHARS[i >> 12]; dest[destLen - 3] = CHARS[(i >>> 6) & 0x3f]; dest[destLen - 2] = left == 2 ? CHARS[i & 0x3f] : '='; dest[destLen - 1] = '='; } return dest; } /** * Decodes a BASE64 encoded char array. */ public byte[] decode(char[] arr) { int length = arr.length; if (length == 0) { return new byte[0]; } int sndx = 0, endx = length - 1; int pad = arr[endx] == '=' ? (arr[endx - 1] == '=' ? 2 : 1) : 0; int cnt = endx - sndx + 1; int sepCnt = length > 76 ? (arr[76] == '\r' ? cnt / 78 : 0) << 1 : 0; int len = ((cnt - sepCnt) * 6 >> 3) - pad; byte[] dest = new byte[len]; int d = 0; for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) { int i = INV[arr[sndx++]] << 18 | INV[arr[sndx++]] << 12 | INV[arr[sndx++]] << 6 | INV[arr[sndx++]]; dest[d++] = (byte) (i >> 16); dest[d++] = (byte) (i >> 8); dest[d++] = (byte) i; if (sepCnt > 0 && ++cc == 19) { sndx += 2; cc = 0; } } if (d < len) { int i = 0; for (int j = 0; sndx <= endx - pad; j++) { i |= INV[arr[sndx++]] << (18 - j * 6); } for (int r = 16; d < len; r -= 8) { dest[d++] = (byte) (i >> r); } } return dest; } // ---------------------------------------------------------------- byte public static byte[] encodeToByte(String s) { try { return encodeToByte(s.getBytes("UTF-8"), false); } catch (UnsupportedEncodingException ignore) { return null; } } public static byte[] encodeToByte(byte[] arr) { return encodeToByte(arr, false); } /** * Encodes a raw byte array into a BASE64 byte[]. */ public static byte[] encodeToByte(byte[] arr, boolean lineSep) { int len = arr != null ? arr.length : 0; if (len == 0) { return new byte[0]; } int evenlen = (len / 3) * 3; int cnt = ((len - 1) / 3 + 1) << 2; int destlen = cnt + (lineSep ? (cnt - 1) / 76 << 1 : 0); byte[] dest = new byte[destlen]; for (int s = 0, d = 0, cc = 0; s < evenlen;) { int i = (arr[s++] & 0xff) << 16 | (arr[s++] & 0xff) << 8 | (arr[s++] & 0xff); dest[d++] = (byte) CHARS[(i >>> 18) & 0x3f]; dest[d++] = (byte) CHARS[(i >>> 12) & 0x3f]; dest[d++] = (byte) CHARS[(i >>> 6) & 0x3f]; dest[d++] = (byte) CHARS[i & 0x3f]; if (lineSep && ++cc == 19 && d < destlen - 2) { dest[d++] = '\r'; dest[d++] = '\n'; cc = 0; } } int left = len - evenlen; if (left > 0) { int i = ((arr[evenlen] & 0xff) << 10) | (left == 2 ? ((arr[len - 1] & 0xff) << 2) : 0); dest[destlen - 4] = (byte) CHARS[i >> 12]; dest[destlen - 3] = (byte) CHARS[(i >>> 6) & 0x3f]; dest[destlen - 2] = left == 2 ? (byte) CHARS[i & 0x3f] : (byte) '='; dest[destlen - 1] = '='; } return dest; } /** * Decodes a BASE64 encoded byte array. */ public static byte[] decode(byte[] arr) { int length = arr.length; if (length == 0) { return new byte[0]; } int sndx = 0, endx = length - 1; int pad = arr[endx] == '=' ? (arr[endx - 1] == '=' ? 2 : 1) : 0; int cnt = endx - sndx + 1; int sepCnt = length > 76 ? (arr[76] == '\r' ? cnt / 78 : 0) << 1 : 0; int len = ((cnt - sepCnt) * 6 >> 3) - pad; byte[] dest = new byte[len]; int d = 0; for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) { int i = INV[arr[sndx++]] << 18 | INV[arr[sndx++]] << 12 | INV[arr[sndx++]] << 6 | INV[arr[sndx++]]; dest[d++] = (byte) (i >> 16); dest[d++] = (byte) (i >> 8); dest[d++] = (byte) i; if (sepCnt > 0 && ++cc == 19) { sndx += 2; cc = 0; } } if (d < len) { int i = 0; for (int j = 0; sndx <= endx - pad; j++) { i |= INV[arr[sndx++]] << (18 - j * 6); } for (int r = 16; d < len; r -= 8) { dest[d++] = (byte) (i >> r); } } return dest; } // ---------------------------------------------------------------- string public static String encodeToString(String s) { try { return new String(encodeToChar(s.getBytes("UTF-8"), false)); } catch (UnsupportedEncodingException ignore) { return null; } } public static String decodeToString(String s) { try { return new String(decode(s), "UTF-8"); } catch (UnsupportedEncodingException ignore) { return null; } } public static String encodeToString(byte[] arr) { return new String(encodeToChar(arr, false)); } /** * Encodes a raw byte array into a BASE64 String. */ public static String encodeToString(byte[] arr, boolean lineSep) { return new String(encodeToChar(arr, lineSep)); } /** * Decodes a BASE64 encoded string. */ public static byte[] decode(String s) { int length = s.length(); if (length == 0) { return new byte[0]; } int sndx = 0, endx = length - 1; int pad = s.charAt(endx) == '=' ? (s.charAt(endx - 1) == '=' ? 2 : 1) : 0; int cnt = endx - sndx + 1; int sepCnt = length > 76 ? (s.charAt(76) == '\r' ? cnt / 78 : 0) << 1 : 0; int len = ((cnt - sepCnt) * 6 >> 3) - pad; byte[] dest = new byte[len]; int d = 0; for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) { int i = INV[s.charAt(sndx++)] << 18 | INV[s.charAt(sndx++)] << 12 | INV[s.charAt(sndx++)] << 6 | INV[s.charAt(sndx++)]; dest[d++] = (byte) (i >> 16); dest[d++] = (byte) (i >> 8); dest[d++] = (byte) i; if (sepCnt > 0 && ++cc == 19) { sndx += 2; cc = 0; } } if (d < len) { int i = 0; for (int j = 0; sndx <= endx - pad; j++) { i |= INV[s.charAt(sndx++)] << (18 - j * 6); } for (int r = 16; d < len; r -= 8) { dest[d++] = (byte) (i >> r); } } return dest; } public static void main(String[] args) { long s = System.nanoTime(); String str = "The quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dog"; for(int i=0;i<100000;i++) Base64.encodeToByte(str); long e = System.nanoTime(); System.out.println(e - s); } }
试试其它关键字
Base64
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
妖魔舞
贡献的其它代码
(
1
)
.
最快的 Base64 编码/解码 算法
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3