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.