[Project Analysis] uses C # To rewrite base64.decodebase64 and Inflater Decoding in Java.

Source: Internet
Author: User

The project service is being transplanted recently.ProgramTo the DOTNET platform.

In a Java program, an HTTP request data header contains a base64 encoded string, for example:

Required/uqjcj06y7jfhwax3ahha =

Now you need to convert this string to the original string, which contains a lot of important information.

Let's take a look at how Java implements this program:

String Str =   " ...... " ;
System. Out. println ( New String (ziputil. decompressbytearray (base64.decodebase64 (Str. getbytes ()))));

Base64 is a class in the commons-codec-1.3.jar package. This package mainly includes the coreAlgorithmSuch as MD5 and sha1, and some common encryption and decryption algorithms.

The ziputil. decompressbytearray method is implemented as follows:

Code

Public   Static   Byte [] Decompressbytearray ( Byte Abyte0 [])
{
Inflater =   New Inflater ();
Inflater. setinput (abyte0 );
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 ){}
Try  
{
Bytearrayoutputstream. Close ();
}
Catch (Ioexception ){}
Return Bytearrayoutputstream. tobytearray ();
}

The result is:

This is a data format with a certain protocol, which is developed by the project.

Now let's take a look at how C # implements it:

Code

[Test]
Public   Void Base64test ()
{
String Basestr =   " Required/uqjcj06y7jfhwax3ahha = " ;
// Base64 Decoding
Byte [] Basebytes = Convert. frombase64string (basestr );
// Inflater Decompression
String Resultstr = Decompress (basebytes );

Console. writeline (resultstr );
}

///   <Summary>
/// Inflater Decompression
///   </Summary>
///   <Param name = "basebytes"> </param>
///   <Returns> </returns>
Public   String Decompress ( Byte [] Basebytes)
{
String Resultstr =   String . Empty;
Using (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;
}

The inflaterinputstream class comes from icsharpcode. sharpziplib. dll.

The result is:

We can find that the result is the same as that of Java, and the program is implemented.

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.