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

namespace Core.Mes.Common
{
    public class Utility
    {
        #region dataset to byte[]
        /// <summary>
        /// 序列化数据集
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static byte[] SerializeDataSet(DataSet ds)
        {
            byte[] buffer = null;
            ds.RemotingFormat = SerializationFormat.Binary;
            MemoryStream ms = new MemoryStream();
            IFormatter bf = new BinaryFormatter();
            bf.Serialize(ms, ds);
            buffer = ms.ToArray();
            ms.Close();
            ms.Dispose();
            ms = null;
            ds.Dispose();
            ds = null;
            return buffer;
        }

        /// <summary>
        /// 反序列化数据集
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static DataSet ReSerializable(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream(buffer);
            IFormatter bf = new BinaryFormatter();
            object obj = bf.Deserialize(ms);
            DataSet ds = (DataSet)obj;
            ms.Close();
            ms.Dispose();
            return ds;
        }

        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static byte[] SerializeObject(Object obj)
        {
            byte[] buffer = null;
            MemoryStream ms = new MemoryStream();
            IFormatter bf = new BinaryFormatter();
            bf.Serialize(ms, obj);
            buffer = ms.ToArray();
            ms.Close();
            ms.Dispose();
            ms = null;
            return buffer;
        }
        /// <summary>
        /// 反序列化对象
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static Object ReSerializeObject(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream(buffer);
            IFormatter bf = new BinaryFormatter();
            object obj = bf.Deserialize(ms);
            ms.Close();
            ms.Dispose();
            return obj;
        }
        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static byte[] SerializeAndCompressObject(Object obj)
        {
            byte[] buffer = null;
            MemoryStream ms = new MemoryStream();
            IFormatter bf = new BinaryFormatter();
            bf.Serialize(ms, obj);
            buffer = ms.ToArray();
            ms.Close();
            ms.Dispose();
            ms = null;
            return CompressBytes(buffer);
        }
        /// <summary>
        /// 反序列化对象
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static Object ReSerializeCompressedObject(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream(Decompress(buffer));
            IFormatter bf = new BinaryFormatter();
            object obj = bf.Deserialize(ms);
            ms.Close();
            ms.Dispose();
            return obj;
        }
        #endregion

        #region 压缩、解压缩
        //压缩字节
        public static byte[] CompressBytes(byte[] bytes)
        {   /*  1.创建压缩的数据流 
                2.设定compressStream为存放被压缩的文件流,并设定为压缩模式
                3.将需要压缩的字节写到被压缩的文件流
            */
            using (MemoryStream compressStream = new MemoryStream())
            {
                using (GZipStream zipStream = new GZipStream(compressStream, CompressionMode.Compress))
                    zipStream.Write(bytes, 0, bytes.Length);
                return compressStream.ToArray();
            }
        }


        //解压缩字节
        public static byte[] Decompress(byte[] bytes)
        {
            /*  1.创建被压缩的数据流
                2.创建zipStream对象，并传入解压的内存流
                3.创建目标流
                4.zipStream拷贝到目标流
                5.返回目标流输出字节
            * */
            byte[] buffer = new byte[4096];//定义数据缓冲  
            using (MemoryStream compressStream = new MemoryStream(bytes))
            {
                using (GZipStream zipStream = new GZipStream(compressStream, CompressionMode.Decompress))
                {
                    using (MemoryStream resultStream = new MemoryStream())
                    {
                        int offset = 0;
                        while ((offset = zipStream.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            resultStream.Write(buffer, 0, offset);
                        }
                        return resultStream.ToArray();
                    }
                }
            }

        }

        //////////////////////////////////////////////////////////////////////////            
        ///读取Stream
        //////////////////////////////////////////////////////////////////////////
        public static int ReadAllBytesFromStream(Stream stream, byte[] buffer)
        {
            int offset = 0;
            int totalCount = 0;
            while (true)
            {
                int bytesRead = stream.Read(buffer, offset, 100);
                if (bytesRead == 0)
                {
                    break;
                }
                offset += bytesRead;
                totalCount += bytesRead;
            }
            return totalCount;
        }

        #endregion
    }
}
