zoukankan      html  css  js  c++  java
  • 【项目分析】利用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(); 
    }

    得出的结果为:

    image

    这个得到具有一定协议的数据格式,这是项目制定的,这里不必多说。

    现在我们来看下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中。

    得出的结果为:

    image

    可以发现得到的结果是和JAVA版一样的,程序得到实现。

  • 相关阅读:
    发布一个扩展Repeater的模板控件,带自动分页功能
    webservice 测试窗体只能用于来自本地计算机的请求
    FCKeditor编辑器中设置默认文本行高和字体大小
    程序员的个人性格
    程序设计模式的有趣解释-追MM
    集锦一
    UML简介(原创)
    一位IT从业人员的心路历程
    一个初级测试工程师的工作总结
    "与熊共舞"(转载)
  • 原文地址:https://www.cnblogs.com/liping13599168/p/1961853.html
Copyright © 2011-2022 走看看