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