利用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的方法实现为:
代码 public static byte[] decompressByteArray(byte abyte0[])
{
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#该如何实现它:
代码 [Test]
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解码
原文:[项目分析]利用C#改写JAVA中的Base64.DecodeBase64以及Inflater解码 最近正在进行项目服务的移植工作,即将JAVA服务的程序移植到DotNet平台中. 在JAVA程 ...
- 利用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 ...
随机推荐
- leetcode笔记:Same Tree
一. 题目描写叙述 Given two binary trees, write a function to check if they are equal or not. Two binary tre ...
- Search in Rotated Sorted Array II leetcode java
题目: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would ...
- WCF 添加 RESTful 支持,适用于 IIS、Winform、cmd 宿主
You can expose the service in two different endpoints. the SOAP one can use the binding that support ...
- 齐次坐标(Homogeneous Coordinates)
原文:http://blog.163.com/m_note/blog/static/208197045201272341230195/ 齐次坐标(Homogeneous Coordinates) 问题 ...
- xhEditor在线编辑器使用实例
使用xhEditor的最大好处就是不用去处理烦人的HTML标签问题,研究了一天,记录备用 前台HTML: <%@ Page Language="C#" AutoEventWi ...
- iOS Strong 和 weak
iOS 5 中对属性的设置新增了strong 和weak关键字来修饰属性(iOS 5 之前不支持ARC) strong 用来修饰强引用的属性: @property (strong) SomeClass ...
- nl命令(转)
原文:http://www.cnblogs.com/peida/archive/2012/11/01/2749048.html nl命令在linux系统中用来计算文件中行号.nl 可以将输出的文件内容 ...
- VScode-Go can't load package: package .: no buildable Go source files in
在VScode中调试Go程序时提示: can't load package: package .: no buildable Go source files in d:\my_workspace\go ...
- TQ2440之定时器中断0——volatile关键字的重要作用
近日,在学习<ARM处理器裸机开发实战--机制而非策略>一书,在TQ2440开发板上,按照书中实例以及光盘配套程序源代码进行Timer0中断试验,编译成功后烧写到开发板上,没有任何反应,反 ...
- Php与Erlang的Socket通信
一般来说网络通讯经常使用的方式有2种:文本通讯和二进制通讯. php与erlang之间实现文本通讯比較简单.这里就不做讨论,本文主要讨论的是php与erlang实现二进制通讯的实现方法.实现过程 ...