Esempio n. 1
0
        public void OnBeforeSerialize()
        {
            if (!_dirty)
            {
                return;
            }

            _dirty = false;
            if (Data == null || Data.Length == 0)
            {
                _compressedData = null;
                return;
            }

            var rawData = new byte[Data.Length * SerializationUtilites.SizeOf <T>()];

            Buffer.BlockCopy(Data, 0, rawData, 0, rawData.Length);

            using (var compressedStream = new MemoryStream())
                using (var zipStream = new GZipStream(compressedStream, CompressionMode.Compress))
                {
                    zipStream.Write(rawData, 0, rawData.Length);
                    zipStream.Close();
                    _compressedData = compressedStream.ToArray();
                }
        }
Esempio n. 2
0
        public void OnAfterDeserialize()
        {
            if (_compressedData == null || _compressedData.Length == 0)
            {
                //Data = new T[Width * Height];
                return;
            }
            using (var compressedMs = new MemoryStream(_compressedData))
            {
                using (var decompressedMs = new MemoryStream())
                {
                    using (var gzs = new GZipStream(compressedMs, CompressionMode.Decompress))
                    {
                        gzs.CopyTo(decompressedMs);
                    }
                    using (var binaryStream = new BinaryReader(decompressedMs))
                    {
                        binaryStream.BaseStream.Position = 0;
                        var pos       = 0;
                        var counter   = 0;
                        var length    = (int)decompressedMs.Length;
                        var typeSize  = SerializationUtilites.SizeOf <T>();
                        var arraySize = length / typeSize;
                        if (Data == null || Data.Length != arraySize)
                        {
                            Data = new T[arraySize];
                        }
                        while (pos < length)
                        {
                            try
                            {
                                Data[counter] = ReadFromStream(binaryStream);
                            }
                            catch (Exception)
                            {
                                throw;
                            }

                            counter++;
                            pos += typeSize;
                        }
                    }
                }
            }
        }