예제 #1
0
        public static void Serialize(this IDictionary <short, Field> fields, Compression compression, Stream stream)
        {
            foreach (var field in fields)
            {
                byte[] keyBytes = BitConverter.GetBytes(field.Key);
                byte[] valBytes;
                string toStore = field.Value.Store ? field.Value.Value : string.Empty;

                if (compression == Compression.GZip)
                {
                    valBytes = Deflator.Compress(Encoding.GetBytes(toStore));
                }
                else if (compression == Compression.Lz)
                {
                    valBytes = QuickLZ.compress(Encoding.GetBytes(toStore), 1);
                }
                else
                {
                    valBytes = Encoding.GetBytes(toStore);
                }

                byte[] valLengthBytes = BitConverter.GetBytes(valBytes.Length);

                if (!BitConverter.IsLittleEndian)
                {
                    Array.Reverse(keyBytes);
                    Array.Reverse(valBytes);
                    Array.Reverse(valLengthBytes);
                }

                stream.Write(keyBytes, 0, sizeof(short));
                stream.Write(valLengthBytes, 0, sizeof(int));
                stream.Write(valBytes, 0, valBytes.Length);
            }
        }
예제 #2
0
        public static byte[] Serialize(this IList <Field> fields, Compression compression)
        {
            using (var stream = new MemoryStream())
            {
                foreach (var field in fields)
                {
                    byte[] keyBytes       = Encoding.GetBytes(field.Key);
                    byte[] keyLengthBytes = BitConverter.GetBytes((short)keyBytes.Length);
                    byte[] valBytes;
                    string toStore = field.Store ? field.Value : string.Empty;

                    if (compression == Compression.GZip)
                    {
                        valBytes = Deflator.Compress(Encoding.GetBytes(toStore));
                    }
                    else if (compression == Compression.QuickLz)
                    {
                        valBytes = QuickLZ.compress(Encoding.GetBytes(toStore), 1);
                    }
                    else
                    {
                        valBytes = Encoding.GetBytes(toStore);
                    }

                    byte[] valLengthBytes = BitConverter.GetBytes(valBytes.Length);

                    if (!BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(keyLengthBytes);
                        Array.Reverse(keyBytes);
                        Array.Reverse(valBytes);
                        Array.Reverse(valLengthBytes);
                    }

                    stream.Write(keyLengthBytes, 0, sizeof(short));
                    stream.Write(keyBytes, 0, keyBytes.Length);
                    stream.Write(valLengthBytes, 0, sizeof(int));
                    stream.Write(valBytes, 0, valBytes.Length);
                }
                return(stream.ToArray());
            }
        }