【項目分析】利用C#改寫JAVA中的Base64.DecodeBase64以及Inflater解碼

來源:互聯網
上載者:User

最近進行中專案服務的移植工作,即將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版一樣的,程式得到實現。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.