"Project Analysis" uses C # to rewrite Base64.decodebase64 and inflater decoding in Java

Source: Internet
Author: User

Original: "Project Analysis" uses C # to rewrite Base64.decodebase64 and inflater decoding in Java

A recent migration of project Services is underway to port the Java Services program to the Dotnet platform.

In a Java program, there is an HTTP request data header that contains a BASE64 encoded string, for example:

ejyvjmengdama1fpbmjnikp3zzzepaa1plmxy10sddqbqr54ww5athg7zxjya0myr9p7bpfnk/uqjcj06y7jfhwax3ahha==

Now you need to convert the string to the original string, and the original string contains many important information.

Let's look at how Java implements this program:

String str= "...";
System.out.println (NewString (Ziputil.decompressbytearray (Base64.decodebase64 (Str.getbytes () )));

Where Base64 is a class in the Commons-codec-1.3.jar package. This package mainly includes core algorithms, such as MD5,SHA1 and so on, as well as some conventional cryptographic decryption algorithms.

The Ziputil.decompressbytearray method is implemented as follows:

Code Public Static byte[] Decompressbytearray (byteAbyte0[])
{
Inflater Inflater= NewInflater ();
Inflater.setinput (ABYTE0);
Bytearrayoutputstream Bytearrayoutputstream= NewBytearrayoutputstream (abyte0.length);
byteabyte1[]= New byte[1024x768];
while (!inflater.finished ())
Try
{
intI=inflater.inflate (abyte1);
Bytearrayoutputstream.write (Abyte1,0, i);
}
Catch(Dataformatexception dataformatexception) {}
Try
{
Bytearrayoutputstream.close ();
}
Catch(IOException ioexception) {}
returnBytearrayoutputstream.tobytearray ();
}

The results are as follows:

This gets the data format with a certain agreement, this is the project development, there is no need to say much.

Now let's look at how C # should implement it:

Code [Test]
Public voidBase64test ()
{
stringBasestr= "ejyvjmengdama1fpbmjnikp3zzzepaa1plmxy10sddqbqr54ww5athg7zxjya0myr9p7bpfnk/uqjcj06y7jfhwax3ahha==";
//Base64 decoding
            byte[] basebytes=convert.frombase64string (BASESTR);
//Inflater Decompression
            stringResultStr=Decompress (basebytes);

Console.WriteLine (RESULTSTR);
}

/// <summary>
///Inflater Decompression
/// </summary>
/// <param name= "basebytes" ></param>
/// <returns></returns>
         Public stringDecompress (byte[] basebytes)
{
stringResultStr= string. Empty;
using(MemoryStream MemoryStream= NewMemoryStream (basebytes))
{
using(Inflaterinputstream inf= NewInflaterinputstream (MemoryStream))
{
using(MemoryStream buffer= NewMemoryStream ())
{
byte[] Result= New byte[1024x768];

intReslen;
while((Reslen=INF. Read (Result,0, result. Length))> 0)
{
Buffer. Write (Result,0, Reslen);
}
ResultStr=Encoding.Default.GetString (Result);
}
}
}
returnResultStr;
}

The Inflaterinputstream class is derived from the ICSharpCode.SharpZipLib.dll.

The results are as follows:

It can be found that the results are the same as in the Java version, and the program is implemented.

"Project Analysis" uses C # to rewrite Base64.decodebase64 and inflater decoding in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.