Save time by Serializing and Compressing Objects before transporting from one layer to another

The following code describes the helper methods to Compress a Serialized object using the GZipStream class. The compressed object can be deserailized and uncompressed in the Client side by using the DeSerialize functions. It can be used when you have to transport large objects like datasets from client to the Server or vice-vers. This approximately reduces the object size atleast to a magnitude of 1/10 th.

Please note:
1. Relevant log comments are added in the code, so that the user can add the appropriate logging statements.
2. You can use the "elapsedTime.Elapsed" value to determine the time taken for the Action







using
System;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO.Compression;
using System.Data;
using System.Diagnostics;

namespace Helpers.Serailization
{
    /// <summary>
    /// Helper class for Serializing and Compressing activities
    /// </summary>
    public class SerializationHelper
    {
         /// <summary>
        /// Binary Serailizes an object
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static byte[] BinarySerialize(object obj)
        {
             if (obj == null) return null;

            MemoryStream stream = new MemoryStream();
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            binaryFormatter.Serialize(stream, obj);

            stream.Seek(0, 0);
             byte[] result = stream.ToArray();

             stream.Close();

             return result;
        }

         /// <summary>
        /// Binary Serailizes an object and compresses it
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <param name="requiredCompress">if set to <c>true</c> [required compress].</param>
        /// <returns></returns>
        public static byte[] BinarySerialize(object obj, bool requiredCompress)
        {

            Stopwatch elapsedTime = new Stopwatch();
             elapsedTime.Start();
             byte[] data = null;

             if (requiredCompress)
            {
                data = Compress(BinarySerialize(obj));
             }
             else
            {
                data = BinarySerialize(obj);
             }

             elapsedTime.Stop();
             //Log the elapsed time
            return data;
        }

         /// <summary>
        /// Binary DeSerailizes an object
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        public static object BinaryDeserialize(byte[] data)
        {
             if (data == null || data.Length == 0) return null;

            BinaryFormatter binaryFormatter = new BinaryFormatter();
            MemoryStream stream = new MemoryStream();
            stream.Write(data, 0, data.Length);
            stream.Flush();
            stream.Seek(0, 0);
             return binaryFormatter.Deserialize(stream);
         }

        /// <summary>
        /// DeSerailizes an uncompressed object
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="requiredUnCompress">if set to <c>true</c> [required un compress].</param>
        /// <returns></returns>
        public static object BinaryDeserialize(byte[] data, bool requiredUnCompress)
        {
            Stopwatch elapsedTime = new Stopwatch();
             elapsedTime.Start();
             object result = null;

             if (requiredUnCompress)
            {
                result = BinaryDeserialize(DeCompress(data));
             }
             else
            {
                result = BinaryDeserialize(data);
             }

             elapsedTime.Stop();
             //log the time taken to DeSerailize

             return result;
        }

         /// <summary>
        /// Compresses a byte array
        /// </summary>
        /// <param name="bytData"></param>
        /// <returns></returns>
        public static byte[] Compress(byte[] bytData)
        {
             try
            {
                 //Log the Size of uncompressed data : bytData.Length.ToString()

                MemoryStream ms = new MemoryStream();
                Stream s = new GZipStream(ms, CompressionMode.Compress);
                s.Write(bytData, 0, bytData.Length);
                 s.Close();
                 byte[] compressedData = (byte[])ms.ToArray();

                 //Log the Size of compressed data : bytData.Length.ToString()

                 return compressedData;
             }
             catch { return null; }
        }

         /// <summary>
        /// DeCompresses a byte array
        /// </summary>
        /// <param name="bytInput"></param>
        /// <returns></returns>
        public static byte[] DeCompress(byte[] bytInput)
        {
             byte[] writeData = new byte[4096];
            MemoryStream memStream = new MemoryStream();
            Stream s2 = new GZipStream(new MemoryStream(bytInput), CompressionMode.Decompress);
             int size;

             while ((size = s2.Read(writeData, 0, writeData.Length)) > 0)
                memStream.Write(writeData, 0, size);
             memStream.Flush();

             return memStream.ToArray();
        }
    }
}
By [)ia6l0 iii   Popularity  (1616 Views)