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版一样的,程序得到实现。

  • 相关阅读:
    开发周记
    开发日记03
    开发日记01
    MVC实例应用
    MVC概述
    23种设计模式简述
    xx系统属性分析
    淘宝网质量属性
    架构漫谈阅读笔记
    浅谈软件架构师工作流程
  • 原文地址:https://www.cnblogs.com/Alex80/p/6641937.html
Copyright © 2011-2022 走看看