Ejemplo 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);
        }
Ejemplo 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)
        {
            byte[] intBytes = new byte[4];
            stream.Read(intBytes, 0, 4);
            int startOfTags = BitConverter.ToInt32(intBytes, 0);

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

            RuntimeTypeModel typeModel = TagIndexSerializer.CreateTypeModel();

            byte[] stringTableBytes = new byte[startOfTags - 8];
            stream.Read(stringTableBytes, 0, stringTableBytes.Length);
            MemoryStream  memoryStream = new MemoryStream(stringTableBytes);
            List <string> strings      = typeModel.Deserialize(memoryStream, null, typeof(List <string>)) as List <string>;

            memoryStream.Dispose();

            byte[] tagsIndexBytes = new byte[endOfFile - startOfTags];
            stream.Read(tagsIndexBytes, 0, tagsIndexBytes.Length);
            memoryStream = new MemoryStream(tagsIndexBytes);
            List <KeyValuePair <uint, List <KeyValuePair <uint, uint> > > > tagsIndexTableList = typeModel.Deserialize(memoryStream, null,
                                                                                                                       typeof(List <KeyValuePair <uint, List <KeyValuePair <uint, uint> > > >)) as List <KeyValuePair <uint, List <KeyValuePair <uint, uint> > > >;

            memoryStream.Dispose();

            Dictionary <uint, SimpleTagsCollection> tagsIndexList = new Dictionary <uint, SimpleTagsCollection>();

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

            return(new TagsIndexReadonly(tagsIndexList));
        }