예제 #1
0
        public static int Serialize(this IDictionary <short, Field> fields, Compression compression, Stream stream)
        {
            var size = 0;

            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);

                size += (keyBytes.Length + valLengthBytes.Length + valBytes.Length);
            }
            return(size);
        }
예제 #2
0
        public static IList <Field> DeserializeFields(Stream stream, int size, Compression compression, IDictionary <short, string> keyIndex)
        {
            var read   = 0;
            var fields = new List <Field>();

            while (read < size)
            {
                var keyIdBytes = new byte[sizeof(short)];

                stream.Read(keyIdBytes, 0, sizeof(short));

                if (!BitConverter.IsLittleEndian)
                {
                    Array.Reverse(keyIdBytes);
                }

                var keyId = BitConverter.ToInt16(keyIdBytes, 0);

                string key = keyIndex[keyId];

                var valLengthBytes = new byte[sizeof(int)];

                stream.Read(valLengthBytes, 0, sizeof(int));

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

                int valLength = BitConverter.ToInt32(valLengthBytes, 0);

                byte[] valBytes = new byte[valLength];

                stream.Read(valBytes, 0, valLength);

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

                string value;

                if (compression == Compression.GZip)
                {
                    value = Encoding.GetString(Deflator.Deflate(valBytes));
                }
                else if (compression == Compression.Lz)
                {
                    value = Encoding.GetString(QuickLZ.decompress(valBytes));
                }
                else
                {
                    value = Encoding.GetString(valBytes);
                }

                read += sizeof(short) + sizeof(int) + valBytes.Length;

                fields.Add(new Field(key, value));
            }
            return(fields);
        }