Class and byte arrays are serialized and converted to each other, including whether to compress data.

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. IO;
Using system. runtime. serialization;
Using system. runtime. serialization. formatters. Binary;
Using system. Io. compression;

Namespace dal
{
Public class classandbyte
{

/// <Summary>
/// Convert the serializable Class Object To byte []
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// <Param name = "OBJ"> </param>
/// <Returns> </returns>
Public static byte [] objecttobytes <t> (t obj)
{
Try
{
Using (memorystream MS = new memorystream ())
{
Iformatter formatter = new binaryformatter ();
Formatter. serialize (MS, OBJ );
Return Ms. getbuffer ();
}
}
Catch (exception ex)
{
Throw ex;
}
}

/**/
/// <Summary>
/// Restore a serialized byte [] Array
/// </Summary>
/// <Param name = "bytes"> </param>
/// <Returns> </returns>
Public static t bytestoobject <t> (byte [] arrbytes)
{
Try
{
Using (memorystream MS = new memorystream (arrbytes ))
{
Iformatter formatter = new binaryformatter ();
Object OBJ = formatter. deserialize (MS );
T = (t) OBJ;
Return T;
}
}
Catch (exception ex)
{
Throw ex;
}
}
/// <Summary>
/// Convert a class object to a byte array. You can set the iscompress parameter to determine whether the data is compressed.
/// </Summary>
/// <Typeparam name = "T"> Object Type </typeparam>
/// <Param name = "OBJ"> object to be converted to byte </param>
/// <Param name = "iscompress"> whether to compress data </param>
/// <Returns> converted byte array </returns>
Public static byte [] objecttobytes <t> (t obj, bool iscompress)
{
If (! Iscompress)
{
Return objecttobytes <t> (OBJ );
}
Else
{
Try
{
Using (memorystream MS = new memorystream ())
{
Iformatter formatter = new binaryformatter ();
Formatter. serialize (MS, OBJ );
Byte [] arrbyte = Ms. getbuffer ();
Byte [] arrbytecompress = compress (arrbyte );
Return arrbytecompress;
}
}
Catch (exception ex)
{
Throw ex;
}
}

}

/// <Summary>
/// Convert the byte array to an object. If the data is compressed, The iscompress value must be true.
/// </Summary>
/// <Typeparam name = "T"> Object Type </typeparam>
/// <Param name = "arrbytes"> array to be converted </param>
/// <Param name = "iscompress"> has the data been compressed? </param>
/// <Returns> byte array converted Class Object </returns>
Public static t bytestoobject <t> (byte [] arrbytes, bool iscompress)
{
If (! Iscompress)
{
Return bytestoobject <t> (arrbytes );
}
Else
{
Try
{
Byte [] arrbytedecompress = decompress (arrbytes );
Using (memorystream MS = new memorystream (arrbytedecompress ))
{
Iformatter formatter = new binaryformatter ();
Object OBJ = formatter. deserialize (MS );
T = (t) OBJ;
Return T;
}
}
Catch (exception ex)
{
Throw ex;
}
}
}
//-------------------------------------------------------------------------------
/// <Summary>
/// Convert a serializable object to a byte array
/// </Summary>
/// <Param name = "O"> Object </param>
/// <Returns> returns the relevant array </returns>
Protected static byte [] objecttobytearray (Object OBJ)
{
Using (memorystream MS = new memorystream ())
{
Iformatter BF = new binaryformatter ();
BF. serialize (MS, OBJ );
Return Ms. toarray ();
}
}
/// <Summary>
/// Restore the byte array converted from a serializable object to an object
/// </Summary>
/// <Param name = "B"> byte array </param>
/// <Returns> Related Object </returns>
Protected static object bytearraytoobject (byte [] arrbytes)
{
Using (memorystream MS = new memorystream (arrbytes, 0, arrbytes. Length ))
{
Binaryformatter BF = new binaryformatter ();
Return BF. deserialize (MS );
}
}
/// <Summary>
/// Compress data
/// </Summary>
/// <Param name = "data"> </param>
/// <Returns> </returns>
Public static byte [] compress (byte [] data)
{
Byte [] bdata;
Memorystream MS = new memorystream ();
Gzipstream stream = new gzipstream (MS, compressionmode. Compress, true );
Stream. Write (data, 0, Data. Length );
Stream. Close ();
Stream. Dispose ();
// You must disable the stream to return Ms streaming data. Otherwise, the data is incomplete.
// And the decompression method stream. Read (buffer, 0, buffer. Length) will return 0
Bdata = Ms. toarray ();
Ms. Close ();
Ms. Dispose ();
Return bdata;
}

/// <Summary>
/// Decompress the data
/// </Summary>
/// <Param name = "data"> </param>
/// <Returns> </returns>
Public static byte [] decompress (byte [] data)
{
Byte [] bdata;
Memorystream MS = new memorystream ();
Ms. Write (data, 0, Data. Length );
Ms. Position = 0;
Gzipstream stream = new gzistream (MS, compressionmode. decompress, true );
Byte [] buffer = new byte [1024];
Memorystream temp = new memorystream ();
Int READ = stream. Read (buffer, 0, buffer. Length );
While (read> 0)
{
Temp. Write (buffer, 0, read );
Read = stream. Read (buffer, 0, buffer. Length );
}
// You must disable the stream to return Ms streaming data. Otherwise, the data is incomplete.
Stream. Close ();
Stream. Dispose ();
Ms. Close ();
Ms. Dispose ();
Bdata = temp. toarray ();
Temp. Close ();
Temp. Dispose ();
Return bdata;
}
}
}

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.