【项目分析】利用C#改写JAVA中的Base64.DecodeBase64以及Inflater解码
原文:【项目分析】利用C#改写JAVA中的Base64.DecodeBase64以及Inflater解码
最近正在进行项目服务的移植工作,即将JAVA服务的程序移植到DotNet平台中。
在JAVA程序中,有个HTTP请求数据头中,包含一个BASE64编码的字符串,例如:
eJyVjMENgDAMA1fpBMjnIkp3ZzZEpAa1PLmXY10sDdqBqr54Ww5AthG7zxJYa0MYr9p7bPFnK/uqjCj06y7JfHwAX3AhhA==
现在需要将这个字符串转化成原始字符串,原始字符串包含许多重要的信息。
我们来看下JAVA是如何实现这个程序的:
String str = "……";System.out.println(new String(ZipUtil.decompressByteArray(Base64.decodeBase64(str.getBytes()))));
其中Base64为commons-codec-1.3.jar包中的一个类。这个包主要包括核心的算法,比如MD5,SHA1等等,还有一些常规加密解密的算法。
而ZipUtil.decompressByteArray的方法实现为:
代码
{
Inflater inflater = new Inflater();
inflater.setInput(abyte0);
ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream(abyte0.length);
byte abyte1[] = new byte[1024];
while (!inflater.finished())
try
{
int i = inflater.inflate(abyte1);
bytearrayoutputstream.write(abyte1, 0, i);
}
catch (DataFormatException dataformatexception) { }
try
{
bytearrayoutputstream.close();
}
catch (IOException ioexception) { }
return bytearrayoutputstream.toByteArray();
}
得出的结果为:

这个得到具有一定协议的数据格式,这是项目制定的,这里不必多说。
现在我们来看下C#该如何实现它:
代码
public void Base64Test()
{
string baseStr = "eJyVjMENgDAMA1fpBMjnIkp3ZzZEpAa1PLmXY10sDdqBqr54Ww5AthG7zxJYa0MYr9p7bPFnK/uqjCj06y7JfHwAX3AhhA==";
// Base64解码
byte[] baseBytes = Convert.FromBase64String(baseStr);
// Inflater解压
string resultStr = Decompress(baseBytes);
Console.WriteLine(resultStr);
}
/// <summary>
/// Inflater解压
/// </summary>
/// <param name="baseBytes"></param>
/// <returns></returns>
public string Decompress(byte[] baseBytes)
{
string resultStr = string.Empty;
using (MemoryStream memoryStream = new MemoryStream(baseBytes))
{
using (InflaterInputStream inf = new InflaterInputStream(memoryStream))
{
using (MemoryStream buffer = new MemoryStream())
{
byte[] result = new byte[1024];
int resLen;
while ((resLen = inf.Read(result, 0, result.Length)) > 0)
{
buffer.Write(result, 0, resLen);
}
resultStr = Encoding.Default.GetString(result);
}
}
}
return resultStr;
}
其中InflaterInputStream的类来自于ICSharpCode.SharpZipLib.dll中。
得出的结果为:

可以发现得到的结果是和JAVA版一样的,程序得到实现。
【项目分析】利用C#改写JAVA中的Base64.DecodeBase64以及Inflater解码的更多相关文章
- 利用C#改写JAVA中的Base64.DecodeBase64以及Inflater解码
最近正在进行项目服务的移植工作,即将JAVA服务的程序移植到DotNet平台中. 在JAVA程序中,有个HTTP请求数据头中,包含一个BASE64编码的字符串,例如: eJyVjMENgDAMA1fp ...
- 利用aspose-words 实现 java中word转pdf文件
利用aspose-words 实现 java中word转pdf文件 首先下载aspose-words-15.8.0-jdk16.jar包 引入jar包,编写Java代码 package test; ...
- netty系列之:java中的base64编码器
简介 什么是Base64编码呢?在回答这个问题之前,我们需要了解一下计算机中文件的分类,对于计算机来说文件可以分为两类,一类是文本文件,一类是二进制文件. 对于二进制文件来说,其内容是用二进制来表示的 ...
- javascript中的Base64.UTF8编码与解码详解
javascript中的Base64.UTF8编码与解码详解 本文给大家介绍的是javascript中的Base64.UTF8编码与解码的函数源码分享以及使用范例,十分实用,推荐给小伙伴们,希望大家能 ...
- 在LoadRunner中进行Base64的编码和解码
<Base64 Encode/Decode for LoadRunner>这篇文章介绍了如何在LoadRunner中对字符串进行Base64的编码和解码: http://ptfrontli ...
- Java中使用BASE64加密&解密
package com.bao.tools.encryption; import java.io.IOException; import org.junit.Test; import sun.misc ...
- java 中使用Base64
byte[] cipherData = Base64.encodeBase64(plainText.getBytes()); //默认不换行 byte[] cipherData = Base64.en ...
- Java中的BASE64
located in rt.jar... public class sun.misc.BASE64Encoder extends sun.misc.CharacterEncoder{ //.. } p ...
- 利用正则表达式判断Java中的秒钟、分钟、小时、日、月是否符合规则
// 定义校验规则 Pattern patRule = Pattern.compile("判断规则"); // 校验结果 patRule.matcher("判断的对象&q ...
随机推荐
- ajax的简单操作
项目需要,简单研究了下ajax 需要在html中引入js文件 编写js函数 function testAjax() { $.ajax({ type: 'get', //请求方式 get/post ur ...
- ZOJ 1093 Monkey and Banana (LIS)解题报告
ZOJ 1093 Monkey and Banana (LIS)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- BZOJ 1599: [Usaco2008 Oct]笨重的石子( 枚举 )
直接枚举 ------------------------------------------------------------------------------- #include<cst ...
- 用pc浏览器打开手机页面
原理是仿造http包头,下面已chrome为例. 方法:运行->输入如下手机操作系统对应的代码即可. 安卓: chrome.exe --user-agent="Mozilla/5.0 ...
- [LeetCode]题解(python):015-3Sum
题目来源: https://leetcode.com/problems/3sum/ 题意分析: 这道题目是输入一个数组nums.找出所有的3个数使得这3个数之和为0.要求1.输出的3个数按小到大排序, ...
- javascript单元测试(转)
1. 什么是单元测试 在计算机编程中,单元测试(又称为模块测试)是针对程序模块(软件设计的最小单位)来进行正确性检验的测试工作.程序单元是应用的最小可测试部件.在过程化编程中,一个单元就是单 ...
- Linux android studio :'tools.jar' seems to be not in Android Studio classpath.
问题: 'tools.jar' seems to be not in Android Studio classpath.Please ensure JAVA_HOME points to JDK ra ...
- CSS实现背景图尺寸不随浏览器大小而变化的两种方法
一些网站的首页背景图尺寸不随浏览器缩放而变化,本例使用CSS 实现背景图尺寸不随浏览器缩放而变化,方法一. 把图片作为background,方法二使用img标签.喜欢的朋友可以看看 一些网站的首页 ...
- 百度——地图API——IOS v2.0.2—入门篇
建议高手飘过------ 本以为一个地图的helloworld很简单.实际使用却不是那么回事.就想把地图的头文件和静态库添加到文件中.应该就能用了. baidu提供的xcode工程是支持ios4.3的 ...
- 【JQuery Plugin】WdatePicker
<div class="timeSelect reportDate"> <span>查询时间:</span> <input type=&q ...