Dataset, object, and byte [] binary conversion and compression plus non-compression

Source: Internet
Author: User

From: http://hi.baidu.com/chenoung/item/fd80dc0a92209d374ac4a3cc

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

Namespace commonclass
{
/// <Summary>
/// Skill: Basic Data Operations
Public class kcdataformatter
{
Public kcdataformatter (){}
/// <Summary>
/// Format the dataset into a byte array byte []
/// </Summary>
/// <Param name = "dsoriginal"> DataSet object </param>
/// <Returns> byte array </returns>
Public static byte [] getbinaryformatdata (Dataset dsoriginal)
{
Byte [] binarydataresult = NULL;
Memorystream memstream = new memorystream ();
Iformatter brformatter = new binaryformatter ();
Dsoriginal. remotingformat = serializationformat. Binary;
Brformatter. serialize (memstream, dsoriginal );
Binarydataresult = memstream. toarray ();
Memstream. Close ();
Memstream. Dispose ();
Return binarydataresult;
}
/// <Summary>
/// Format the dataset into a byte array byte [] and the data has been compressed.
/// </Summary>
/// <Param name = "dsoriginal"> DataSet object </param>
/// <Returns> byte array </returns>
Public static byte [] getbinaryformatdatacompress (Dataset dsoriginal)
{
Byte [] binarydataresult = NULL;
Memorystream memstream = new memorystream ();
Iformatter brformatter = new binaryformatter ();
Dsoriginal. remotingformat = serializationformat. Binary;
Brformatter. serialize (memstream, dsoriginal );
Binarydataresult = memstream. toarray ();
Memstream. Close ();
Memstream. Dispose ();
Return compress (binarydataresult );
}
/// <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;
}
/// <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>
/// Deserialize byte arrays into dataset objects
/// </Summary>
/// <Param name = "binarydata"> byte array </param>
/// <Returns> DataSet object </returns>
Public static dataset retrievedataset (byte [] binarydata)
{
Dataset DS = NULL;
Memorystream memstream = new memorystream (binarydata, true );
// Byte [] BS = memstream. getbuffer ();

// Memstream. Write (BS, 0, BS. Length );
// Memstream. Seek (0, seekorigin. Begin );
Iformatter brformatter = new binaryformatter ();
DS = (Dataset) brformatter. deserialize (memstream );
Return Ds;
}
/// <Summary>
/// Decompress the byte array and serialize it into a DataSet object
/// </Summary>
/// <Param name = "binarydata"> byte array </param>
/// <Returns> DataSet object </returns>
Public static dataset retrievedatasetdecompress (byte [] binarydata)
{
Dataset dsoriginal = NULL;
Memorystream memstream = new memorystream (decompress (binarydata ));
Iformatter brformatter = new binaryformatter ();
Object OBJ = brformatter. deserialize (memstream );
Dsoriginal = (Dataset) OBJ;
Return dsoriginal;
}
/// <Summary>
/// Format the object into a byte array []
/// </Summary>
/// <Param name = "dsoriginal"> Object </param>
/// <Returns> byte array </returns>
Public static byte [] getbinaryformatdata (Object dsoriginal)
{
Byte [] binarydataresult = NULL;
Memorystream memstream = new memorystream ();
Iformatter brformatter = new binaryformatter ();
Brformatter. serialize (memstream, dsoriginal );
Binarydataresult = memstream. toarray ();
Memstream. Close ();
Memstream. Dispose ();
Return binarydataresult;
}
/// <Summary>
/// Format objec into a byte array byte [] and compress it
/// </Summary>
/// <Param name = "dsoriginal"> Object </param>
/// <Returns> byte array </returns>
Public static byte [] getbinaryformatdatacompress (Object dsoriginal)
{
Byte [] binarydataresult = NULL;
Memorystream memstream = new memorystream ();
Iformatter brformatter = new binaryformatter ();
Brformatter. serialize (memstream, dsoriginal );
Binarydataresult = memstream. toarray ();
Memstream. Close ();
Memstream. Dispose ();
Return compress (binarydataresult );
}
/// <Summary>
/// Deserialize byte arrays into object objects
/// </Summary>
/// <Param name = "binarydata"> byte array </param>
/// <Returns> Object </returns>
Public static object retrieveobject (byte [] binarydata)
{
Memorystream memstream = new memorystream (binarydata );
Iformatter brformatter = new binaryformatter ();
Object OBJ = brformatter. deserialize (memstream );
Return OBJ;
}
/// <Summary>
/// Decompress the byte array and deserialize it into an object
/// </Summary>
/// <Param name = "binarydata"> byte array </param>
/// <Returns> Object </returns>
Public static object retrieveobjectdecompress (byte [] binarydata)
{
Memorystream memstream = new memorystream (decompress (binarydata ));
Iformatter brformatter = new binaryformatter ();
Object OBJ = brformatter. deserialize (memstream );
Return OBJ;
}
///// <Summary>
//// Decrypt the configuration file and read it into xmldoc.
///// </Summary>
// Public static xmlnode decryptconfigfile (string filepath)
//{
// Filestream FS = new filestream (filepath, filemode. Open );
// Xmldocument m_xmldoc = new xmldocument ();
// Binaryformatter formatter = NULL;
// Try
//{
// Formatter = new binaryformatter ();
/// Deserialize the hashtable from the file and
/// Assign the reference to the local variable.
// M_xmldoc.loadxml (kcencrypt. decrypt (string) formatter. deserialize (FS )));
// Return m_xmldoc.documentelement;
//}
// Catch (serializationexception E)
//{
// Console. writeline ("failed to deserialize. Reason:" + E. Message );
// Throw;
//}
// Finally
//{
// Fs. Close ();
// FS = NULL;
//}
//}
///// <Summary>
//// Encrypt the key before encrypting the file characters
///// </Summary>
// Public static void encryptconfigfile (string filepath, string Str)
//{
// Filestream FS = new filestream (filepath, filemode. Create );
// Binaryformatter formatter = new binaryformatter ();
// Try
//{
// Formatter. serialize (FS, kcencrypt. Encrypt (STR ));
//}
// Catch (serializationexception E)
//{
// Console. writeline ("failed to serialize. Reason:" + E. Message );
// Throw;
//}
// Finally
//{
// Fs. Close ();
// FS = NULL;
//}
//}
}
}

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.