Esempio n. 1
0
        /// <summary>
        /// Does the actual serialization of the given data structures.
        /// </summary>
        private static void Serialize(int begin, Stream stream, List <KeyValuePair <uint, List <KeyValuePair <uint, uint> > > > tagIndex,
                                      ObjectTable <string> stringTable)
        {
            RuntimeTypeModel typeModel = TagIndexSerializer.CreateTypeModel();

            // move until after the index (index contains two int's, startoftagindex, endoffile).
            stream.Seek(begin + 8, SeekOrigin.Begin);

            // serialize string table.
            List <string> strings = new List <string>();

            for (uint id = 0; id < stringTable.Count; id++)
            {
                strings.Add(stringTable.Get(id));
            }
            stringTable.Clear();
            stringTable = null;
            typeModel.Serialize(stream, strings);
            long startOfTagsIndex = stream.Position - begin;

            // serialize tagindex.
            typeModel.Serialize(stream, tagIndex);
            long endOfFile = stream.Position - begin;

            // write index.
            stream.Seek(begin, SeekOrigin.Begin);
            stream.Write(BitConverter.GetBytes((int)startOfTagsIndex), 0, 4); // write start position of tagindex.
            stream.Write(BitConverter.GetBytes((int)endOfFile), 0, 4);        // write size of complete file.

            stream.Seek(begin + endOfFile, SeekOrigin.Begin);
        }
Esempio n. 2
0
        /// <summary>
        /// Deserializes a tags index from the given stream.
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static ITagsCollectionIndexReadonly Deserialize(Stream stream)
        {
            var tagsIndexList = new Dictionary <uint, TagsCollection>();

            var intBytes = new byte[4];

            stream.Read(intBytes, 0, 4);
            var startOfTags = BitConverter.ToInt32(intBytes, 0);

            stream.Read(intBytes, 0, 4);
            var endOfFile = BitConverter.ToInt32(intBytes, 0);

            var typeModel        = TagIndexSerializer.CreateTypeModel();
            var stringTableBytes = new byte[startOfTags - 8];

            stream.Read(stringTableBytes, 0, stringTableBytes.Length);
            var memoryStream = new MemoryStream(stringTableBytes);
            var strings      = typeModel.Deserialize(memoryStream, null, typeof(List <string>)) as List <string>;

            memoryStream.Dispose();

            var tagsIndexBytes = new byte[endOfFile - startOfTags];

            stream.Read(tagsIndexBytes, 0, tagsIndexBytes.Length);
            memoryStream = new MemoryStream(tagsIndexBytes);
            var tagsIndexTableList = typeModel.Deserialize(memoryStream, null,
                                                           typeof(List <KeyValuePair <uint, List <KeyValuePair <uint, uint> > > >)) as List <KeyValuePair <uint, List <KeyValuePair <uint, uint> > > >;

            memoryStream.Dispose();

            for (int idx = 0; idx < tagsIndexTableList.Count; idx++)
            {
                var            serializedTagsCollection = tagsIndexTableList[idx];
                TagsCollection tagsCollection           = null;
                if (serializedTagsCollection.Value != null)
                {
                    tagsCollection = new TagsCollection(serializedTagsCollection.Value.Count);
                    foreach (var pair in serializedTagsCollection.Value)
                    {
                        tagsCollection.Add(
                            new Tag(strings[(int)pair.Key], strings[(int)pair.Value]));
                    }
                }
                else
                {
                    tagsCollection = new TagsCollection();
                }
                tagsIndexList.Add(serializedTagsCollection.Key, tagsCollection);
            }

            return(new TagsIndexReadonly(tagsIndexList));
        }