Example #1
0
        /// <summary>
        /// Serializes an object
        /// </summary>
        /// <param name="stream">The target stream</param>
        /// <param name="instance">The object to serialize</param>
        /// <param name="flags">One or more <see cref="SerializedFlags"/> options</param>
        public static void Serialize <T>(
            Stream stream,
            T instance,
            SerializerFlags flags,
            CompressionImplementation compression
            )
        {
            IPrimitiveWriter writer = SerializerFactory.GetWriter(stream);

            //  Check parameters
            Debug.Assert(stream != null, "Input stream is null");
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (instance == null)
            {
                return; // Nothing to do
            }

            Serialize <T>(writer, instance, flags);

            //  Compress result if requested
            if ((flags & SerializerFlags.Compress) != 0)
            {
                byte[] bytes = GetBytes(stream, (int)stream.Length);

                bytes = Compressor.GetInstance().Compress(bytes, compression);

                stream.Seek(0, SeekOrigin.Begin);
                stream.SetLength(bytes.Length);
                stream.Write(bytes, 0, (int)bytes.Length);
            }
        }
Example #2
0
 public static T Deserialize <T>(Stream stream, SerializerFlags flags, CompressionImplementation compression) where T : new()
 {
     return(Deserialize <T>(
                GetReader(stream, flags, compression),
                flags
                ));
 }
Example #3
0
        /// <summary>
        /// Serializes an object
        /// </summary>
        /// <param name="instance">The object to serialize</param>
        /// <param name="flags">One or more <see cref="SerializedFlags"/> options</param>
        /// <param name="compression">Compression method to use</param>
        /// <returns>The serialized object</returns>
        public static byte[] Serialize <T>(T instance, SerializerFlags flags, CompressionImplementation compression)
        {
            MemoryStream stream = new MemoryStream();

            Serialize(stream, instance, flags, compression);
            return(GetBytes(stream, (int)stream.Length));
        }
Example #4
0
 /// <summary>
 /// Serializes an object
 /// </summary>
 /// <param name="instance">The object to serialize</param>
 /// <param name="useCompression">True to compress the serialized object</param>
 /// <param name="compression">Compression method to use</param>
 /// <returns>The serialized object</returns>
 public static byte[] Serialize <T>(T instance, bool useCompression, CompressionImplementation compression)
 {
     return(Serialize(
                instance,
                useCompression ? SerializerFlags.Compress : SerializerFlags.Default,
                compression
                ));
 }
Example #5
0
 /// <summary>
 /// Serializes an object
 /// </summary>
 /// <param name="instance">The object to serialize</param>
 /// <param name="flags">One or more <see cref="SerializedFlags"/> options</param>
 /// <param name="compression">Compression method to use</param>
 /// <returns>The serialized object</returns>
 public static byte[] Serialize <T>(T instance, SerializerFlags flags, CompressionImplementation compression)
 {
     using (var item = _memoryPool.Borrow())
     {
         var stream = item.Item;
         Serialize(stream, instance, flags, compression);
         return(stream.ToArray());
     }
 }
Example #6
0
        private static byte[] InternalCompress(byte[] bytes, bool useHeader, CompressionImplementation compressionImplementation)
        {
            switch (compressionImplementation)
            {
            case CompressionImplementation.ManagedZLib:
                return(ManagedZLibWrapper.Compress(bytes, zLibCompressionAmount, useHeader));

            default:
                throw new ApplicationException(string.Format("Unknown compression implementation {0}", compressionImplementation));
            }
        }
Example #7
0
        public static object Deserialize(Stream stream, SerializerFlags flags, CompressionImplementation compression, Type instanceType)
        {
            TypeSerializationInfo typeInfo = null;
            TypeSerializationArgs args     = new TypeSerializationArgs();
            object instance = Activator.CreateInstance(instanceType);

            args.Flags  = flags;
            args.Reader = GetReader(stream, flags, compression);

            typeInfo = TypeSerializationInfo.GetTypeInfo(instanceType);
            typeInfo.Deserialize(ref instance, args);

            return(instance);
        }
Example #8
0
        static IPrimitiveReader GetReader(Stream stream, SerializerFlags flags, CompressionImplementation compression)
        {
            //  Decompress source stream if necessary
            if ((flags & SerializerFlags.Compress) != 0)
            {
                byte[] bytes = GetBytes(stream, (int)stream.Length);

                bytes = Compressor.GetInstance().Decompress(bytes, compression);
                MemoryStream ms = new MemoryStream(bytes);

                stream = ms;
            }

            return(SerializerFactory.GetReader(stream));
        }
Example #9
0
 /// <summary>
 /// Compress bytes using the supplied compression implementation and optional header.
 /// </summary>
 public byte[] Compress(byte[] bytes, bool useHeader, CompressionImplementation compressionImplementation)
 {
     return(InternalCompress(bytes, useHeader, compressionImplementation));
 }
Example #10
0
 /// <summary>
 /// Compress bytes using the supplied compression implementation and no header.
 /// </summary>
 public byte[] Compress(byte[] bytes, CompressionImplementation compressionImplementation)
 {
     return(InternalCompress(bytes, false, compressionImplementation));
 }
Example #11
0
        private static byte[] InternalDecompress(byte[] bytes, bool useHeader, CompressionImplementation compressionImplementation)
        {
            switch (compressionImplementation)
            {
            case CompressionImplementation.ManagedZLib:
                byte[] decompressed = null;
                try
                {
                    decompressed = ManagedZLibWrapper.Decompress(bytes, useHeader);
                }
                catch (Exception e)
                {
                    if (useHeader)
                    {
                        if (e.Message.Contains("Invalid GZip header"))
                        {
                            //we're probably trying to decompress data that has no header with the header flag. if it throws again, just let it go
                            log.WarnFormat("Tried to decompress using header and got error {0}. Attempting decompress with no header.",
                                           e.Message);
                            try
                            {
                                decompressed = ManagedZLibWrapper.Decompress(bytes, false);
                            }
                            catch (Exception e2)
                            {
                                if (e2.Message.Contains("Z_DATA_ERROR"))                                         //then the data is actually invalid; it may not be compressed at all. we'll try just returning the data as it was
                                {
                                    log.WarnFormat("Tried to decompress with no header after getting error with header and got error {0}. Returning bytes as they were.", e2.Message);
                                    decompressed = bytes;
                                }
                                else
                                {
                                    throw;
                                }
                            }
                        }
                        else
                        {
                            throw;
                        }
                    }
                    else
                    {
                        if (e.Message.Contains("Z_DATA_ERROR"))
                        {
                            //probably the inverse; tried to decompress data with a header without one.
                            log.WarnFormat("Tried to decompress with no header and got error {0}. Attempting decompress with header.",
                                           e.Message);

                            try
                            {
                                decompressed = ManagedZLibWrapper.Decompress(bytes, true);
                            }
                            catch (Exception e2)
                            {
                                if (e2.Message.Contains("Invalid GZip header"))
                                {
                                    log.WarnFormat("Tried to decompress with header after getting error with header and got error {0}. Returning byte as they were.", e2.Message);
                                    decompressed = bytes;
                                }
                                else
                                {
                                    throw;
                                }
                            }
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
                return(decompressed);

            default:
                throw new ApplicationException(string.Format("Unknown compression implementation {0}", compressionImplementation));
            }
        }
Example #12
0
 /// <summary>
 /// Compress bytes using the supplied compression implementation and optional header.
 /// </summary> 
 public byte[] Compress(byte[] bytes, bool useHeader, CompressionImplementation compressionImplementation)
 {
     return InternalCompress(bytes, useHeader, compressionImplementation);
 }
Example #13
0
 /// <summary>
 /// Compress bytes using the supplied compression implementation and no header.
 /// </summary> 
 public byte[] Compress(byte[] bytes, CompressionImplementation compressionImplementation)
 {
     return InternalCompress(bytes, false, compressionImplementation);
 }
Example #14
0
 private byte[] InternalDecompress(byte[] bytes, bool useHeader, CompressionImplementation compressionImplementation)
 {
     switch (compressionImplementation)
     {
         case CompressionImplementation.ManagedZLib:
             return ManagedZLib.Decompress(bytes, useHeader);                    
         default:
             throw new ApplicationException(string.Format("Unknown compression implementation {0}", compressionImplementation));
     }            
 }
Example #15
0
        /// <summary>
        /// Deserializes an object
        /// </summary>
        /// <param name="stream">The source stream</param>
        /// <param name="instance">The instance to receive the deserialized properties</param>
        /// <param name="flags">One or more <see cref="SerializedFlags"/> options</param>
        /// <param name="compression">Compression method to use</param>
        public static bool Deserialize <T>(Stream stream, T instance, SerializerFlags flags, CompressionImplementation compression)
        {
            //  Check args
            Debug.Assert(stream != null, "Input stream is null");
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            return(Deserialize <T>(
                       GetReader(stream, flags, compression),
                       instance,
                       flags
                       ));
        }
Example #16
0
 public static void Serialize <T>(Stream stream, T instance, bool useCompression, CompressionImplementation compression)
 {
     Serialize(
         stream,
         instance,
         useCompression ? SerializerFlags.Compress : SerializerFlags.Default,
         compression
         );
 }