Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DescriptorRegistryValues"/> class.
        /// </summary>
        /// <param name="path">The file path.</param>
        /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
        internal DescriptorRegistryValues(string path)
        {
            if (path is null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            Dictionary <string, DescriptorRegistryItem> persistentItems = new Dictionary <string, DescriptorRegistryItem>(StringComparer.Ordinal);
            Dictionary <string, DescriptorRegistryItem> sessionItems    = new Dictionary <string, DescriptorRegistryItem>(StringComparer.Ordinal);
            bool isOldFormat = false;

            using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                ReadOnlyDictionary <string, DescriptorRegistryItem> values = null;

                if (DescriptorRegistryFileHeader.TryCreate(fs, out DescriptorRegistryFileHeader header))
                {
                    if (header.FileVersion == 1)
                    {
                        long dataLength = fs.Length - fs.Position;

                        byte[] data = new byte[dataLength];

                        fs.ProperRead(data, 0, data.Length);

                        using (MemoryStream ms = new MemoryStream(data))
                        {
                            values = PSFilterPdn.DataContractSerializerUtil.Deserialize <ReadOnlyDictionary <string, DescriptorRegistryItem> >(ms);
                        }
                    }
                }
                else
                {
                    fs.Position = 0;

                    SelfBinder      binder = new SelfBinder();
                    BinaryFormatter bf     = new BinaryFormatter()
                    {
                        Binder = binder
                    };
                    values      = (ReadOnlyDictionary <string, DescriptorRegistryItem>)bf.Deserialize(fs);
                    isOldFormat = true;
                }

                if (values != null && values.Count > 0)
                {
                    foreach (KeyValuePair <string, DescriptorRegistryItem> item in values)
                    {
                        persistentItems.Add(item.Key, item.Value);
                    }
                }
            }

            persistedValues = new ReadOnlyDictionary <string, DescriptorRegistryItem>(persistentItems);
            sessionValues   = new ReadOnlyDictionary <string, DescriptorRegistryItem>(sessionItems);
            dirty           = isOldFormat;
        }
Пример #2
0
            public static bool TryCreate(Stream stream, out DescriptorRegistryFileHeader header)
            {
                header = null;

                bool result = false;

                if (stream.Length > 8)
                {
                    byte[] headerBytes = new byte[8];

                    stream.ProperRead(headerBytes, 0, headerBytes.Length);

                    if (CheckHeaderSignature(headerBytes))
                    {
                        header = new DescriptorRegistryFileHeader(BitConverter.ToInt32(headerBytes, 4));
                        result = true;
                    }
                }

                return(result);
            }