网络——Base64Encode(转:自定义Base64编码器——Base64Encode)
来源:互联网 发布:江西广电网络宽带投诉 编辑:程序博客网 时间:2024/06/02 05:30
【0】README
0.1) 本文source code 转自 core java volume 2 , 旨在了解 如何定义一个 编码器, 如Base64Encode ;
0.2)注意: 区别自定义的 Base64Encode 和 java.util.Base64 编码器
0.3)for souce code, please visit 自定义 Base64Encode 源代码
【1】 Base64Encode 编码器源代码如下:
package com.corejava.chapter3;import java.io.ByteArrayOutputStream;import java.io.FilterOutputStream;import java.io.IOException;import java.io.OutputStream;public class Base64Encode{/** * Computes the Base64 encoding of a string. * @param s a string * @return the Base64 encoding of s */ public static String base64Encode(String s) { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); Base64OutputStream out = new Base64OutputStream(bOut); try { out.write(s.getBytes()); out.flush(); } catch (IOException e) { } return bOut.toString(); }}/** * This stream filter converts a stream of bytes to their Base64 encoding. * * Base64 encoding encodes 3 bytes into 4 characters. |11111122|22223333|33444444| Each set of 6 * bits is encoded according to the toBase64 map. If the number of input bytes is not a multiple of * 3, then the last group of 4 characters is padded with one or two = signs. Each output line is at * most 76 characters. */class Base64OutputStream extends FilterOutputStream{ private static char[] toBase64 = { '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', '+', '/' }; private int col = 0; private int i = 0; private int[] inbuf = new int[3]; /** * Constructs the stream filter. * @param out the stream to filter */ public Base64OutputStream(OutputStream out) { super(out); } public void write(int c) throws IOException { inbuf[i] = c; i++; if (i == 3) { if (col >= 76) { super.write('\n'); col = 0; } super.write(toBase64[(inbuf[0] & 0xFC) >> 2]); super.write(toBase64[((inbuf[0] & 0x03) << 4) | ((inbuf[1] & 0xF0) >> 4)]); super.write(toBase64[((inbuf[1] & 0x0F) << 2) | ((inbuf[2] & 0xC0) >> 6)]); super.write(toBase64[inbuf[2] & 0x3F]); col += 4; i = 0; } } public void flush() throws IOException { if (i > 0 && col >= 76) { super.write('\n'); col = 0; } if (i == 1) { super.write(toBase64[(inbuf[0] & 0xFC) >> 2]); super.write(toBase64[(inbuf[0] & 0x03) << 4]); super.write('='); super.write('='); } else if (i == 2) { super.write(toBase64[(inbuf[0] & 0xFC) >> 2]); super.write(toBase64[((inbuf[0] & 0x03) << 4) | ((inbuf[1] & 0xF0) >> 4)]); super.write(toBase64[(inbuf[1] & 0x0F) << 2]); super.write('='); } }}
0 0
- 网络——Base64Encode(转:自定义Base64编码器——Base64Encode)
- BASE64编码与解码(Base64Encode,Base64Decode)
- Base64Encode编码函数
- Base64Encode编码函数(另一个)
- Base64Encode With 12Bits-CoderTable
- c# 加密与解密 Base64Encode
- javascript下base64Encode编码,支…
- android通过base64encode方式发送图片到服务端
- 常用VBS小工具:iconv,unix2dos/dos2unix,base64Encode/Decode
- android通过base64encode方式发送图片到服务端
- BASE64Encode在MYeclipse中无法倒包的问题
- 网络传输——Base64详解
- 网络传输——Base64详解
- STM32单片机—编码器测速
- Speex手册(四)——命令行编码器/解码器和Speex编码器API(1)
- 深度学习笔记:稀疏自编码器(4)——稀疏自编码器代码练习
- 利用keras搭建自编码器网络——脑机接口
- 深度学习基础(八)—— 稀疏自编码器
- maven 安装配置
- 曲折的ubuntu14.04下用c的api去连接mysql
- iOS WebView 清除缓存和Cookie
- CentOs6.7环境下rpm包安装mysql5.7
- github学习(3)
- 网络——Base64Encode(转:自定义Base64编码器——Base64Encode)
- Wunder Fund Round 2016 D. Hamiltonian Spanning Tree(贪心+dp)
- POST方式提交XML调用接口
- BASIC-10 十进制转十六进制
- USACO-Section 3.1 Humble Numbers(DP)
- cocos2dx 3.3 pluginx tools文件夹分析
- 通过StoryBoard创建UIViewController
- 起伏地形环境轮式机器人运动控制研究
- eclipse中hql语句出现 unexpected token: 8报错