Ejemplo n.º 1
0
        /// <summary>
        /// Stop editing the d2o file, flush the file and dispose ressources
        /// </summary>
        public void EndWriting()
        {
            lock (m_writingSync)
            {
                m_writer.Seek(0, SeekOrigin.Begin);

                m_writing      = false;
                m_needToBeSync = false;

                WriteHeader();

                foreach (var obj in m_objects)
                {
                    if (!m_indexTable.ContainsKey(obj.Key))
                    {
                        m_indexTable.Add(obj.Key, (int)m_writer.BaseStream.Position);
                    }
                    else
                    {
                        m_indexTable[obj.Key] = (int)m_writer.BaseStream.Position;
                    }


                    WriteObject(m_writer, obj.Value, obj.Value.GetType());
                }

                WriteIndexTable();
                WriteClassesDefinition();

                m_writer.Dispose();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Stop editing the d2o file, flush the file and dispose ressources
        /// </summary>
        public void EndWriting()
        {
            lock (writingSync)
            {
                writer.Seek(0, SeekOrigin.Begin);

                writing      = false;
                needToBeSync = false;

                WriteHeader();

                foreach (var obj in objects)
                {
                    if (!indexTable.ContainsKey(obj.Key))
                    {
                        indexTable.Add(obj.Key, (int)writer.BaseStream.Position);
                    }
                    else
                    {
                        indexTable[obj.Key] = (int)writer.BaseStream.Position;
                    }

                    WriteObject(obj.Value, obj.Value.GetType());
                }

                WriteIndexTable();
                WriteClassesDefinition();

                File.WriteAllBytes(Filename, writer.Data);
                writer.Dispose();
            }
        }
Ejemplo n.º 3
0
        public void Save(string uri)
        {
            using (var writer = new BigEndianWriter(new StreamWriter(uri).BaseStream))
            {
                var indexTable = new BigEndianWriter();
                writer.Seek(4, SeekOrigin.Begin);

                foreach (var index in indexes)
                {
                    indexTable.WriteInt(index.Key);
                    indexTable.WriteInt((int)writer.Position);
                    writer.WriteUTF(index.Value);
                }

                var indexLen = (int)indexTable.Position;

                foreach (var index in textIndexes)
                {
                    indexTable.WriteUTF(index.Key);
                    indexTable.WriteInt((int)writer.Position);
                    writer.WriteUTF(index.Value);
                }

                var indexPos = (int)writer.Position;

                /* write index at end */
                var indexData = indexTable.Data;
                writer.WriteInt(indexLen);
                writer.WriteBytes(indexData);

                /* write index pos at begin */
                writer.Seek(0, SeekOrigin.Begin);
                writer.WriteInt(indexPos);
            }
        }
Ejemplo n.º 4
0
        public void Save(string uri)
        {
            using (var writer = new BigEndianWriter(new StreamWriter(uri).BaseStream))
            {
                var indexTable = new BigEndianWriter();
                writer.Seek(4, SeekOrigin.Begin);
                int i = 0;
                foreach (var index in indexes)
                {
                    indexTable.WriteInt(index.Key);
                    indexTable.WriteBoolean(unDiacriticalIndex.ContainsKey(index.Key));
                    indexTable.WriteInt((int)writer.Position);
                    writer.WriteUTF(index.Value);
                    if (unDiacriticalIndex.ContainsKey(index.Key))
                    {
                        indexTable.WriteInt((int)writer.Position);
                        writer.WriteUTF(testUndial[index.Key]);
                    }
                    i++;
                }

                var indexLen = (int)indexTable.Data.Length;

                var indexTextTable = new BigEndianWriter();
                foreach (var index in textIndexes)
                {
                    indexTextTable.WriteUTF(index.Key);
                    indexTextTable.WriteInt((int)writer.Position);
                    writer.WriteUTF(index.Value);
                    i++;
                }

                var indexTextSortTable = new BigEndianWriter();
                foreach (var index in _textSortIndex)
                {
                    indexTextSortTable.WriteInt((int)index.Key);
                    i++;
                }

                var indexPos = (int)writer.Position;

                /* write index at end */
                var indexData = indexTable.Data;
                writer.WriteInt(indexLen);
                writer.WriteBytes(indexData);
                writer.WriteInt((int)indexTextTable.Data.Length);
                writer.WriteBytes(indexTextTable.Data);
                writer.WriteInt((int)indexTextSortTable.Data.Length);
                writer.WriteBytes(indexTextSortTable.Data);

                /* write index pos at begin */
                writer.Seek(0, SeekOrigin.Begin);
                writer.WriteInt(indexPos);
            }
        }
Ejemplo n.º 5
0
        public void Save(string uri)
        {
            using (var writer = new BigEndianWriter(new StreamWriter(uri).BaseStream))
            {
                var indexTable = new BigEndianWriter();
                writer.Seek(4, SeekOrigin.Begin);

                foreach (var index in m_indexes)
                {
                    indexTable.WriteInt(index.Key);
                    indexTable.WriteInt((int)writer.Position);
                    writer.WriteUTF(index.Value);
                }

                var indexLen = (int)indexTable.Position;

                foreach (var index in m_textIndexes)
                {
                    indexTable.WriteUTF(index.Key);
                    indexTable.WriteInt((int)writer.Position);
                    writer.WriteUTF(index.Value);
                }

                var indexPos = (int)writer.Position;

                /* write index at end */
                var indexData = indexTable.Data;
                writer.WriteInt(indexLen);
                writer.WriteBytes(indexData);

                /* write index pos at begin */
                writer.Seek(0, SeekOrigin.Begin);
                writer.WriteInt(indexPos);
            }
        }
Ejemplo n.º 6
0
        public void Send(NetworkMessage message, bool encrypt = true)
        {
            BigEndianWriter writer = new BigEndianWriter();

            message.Pack(writer);
            byte[] data = writer.Data;
            if (encrypt)
            {
                writer.Seek(0);
                writer.WriteUInt((uint)writer.Data.Length);
                data = writer.Data;
                this.keyPairEncryption.Encrypt(ref data, 0, writer.Data.Length);
            }
            else
            {
                writer.Seek(0);
                writer.WriteUInt((uint)writer.Data.Length);
            }

            Send(data);
            Console.WriteLine(string.Format("[SND] {0} -> {1}", IP, message));
        }
Ejemplo n.º 7
0
        public void Save(string uri)
        {
            using (var contentWriter = new BigEndianWriter(new StreamWriter(uri).BaseStream))
            {
                var headerWriter = new BigEndianWriter();
                contentWriter.Seek(4, SeekOrigin.Begin);

                // first table = index table
                foreach (var index in m_entries.Where(x => x.Value.Text != null))
                {
                    headerWriter.WriteInt(index.Key);
                    headerWriter.WriteBoolean(index.Value.UseUndiactricalText);

                    headerWriter.WriteInt((int)contentWriter.Position);
                    contentWriter.WriteUTF(index.Value.Text);

                    if (index.Value.UseUndiactricalText)
                    {
                        headerWriter.WriteInt((int)contentWriter.Position);
                        contentWriter.WriteUTF(index.Value.UnDiactricialText);
                    }
                }

                var indexLen = (int)headerWriter.Position;

                // second table = text index table
                headerWriter.WriteInt(0); // placeholder for text index table len

                foreach (var index in m_textEntries.Where(x => x.Value.Text != null))
                {
                    headerWriter.WriteUTF(index.Key);
                    headerWriter.WriteInt((int)contentWriter.Position);
                    contentWriter.WriteUTF(index.Value.Text);
                }

                var textIndexLen   = headerWriter.Position - indexLen - 4;
                var searchTablePos = headerWriter.Position;
                headerWriter.Seek(indexLen);
                headerWriter.WriteInt((int)textIndexLen); // write table len
                headerWriter.Seek((int)searchTablePos);

                // third table = search index table
                headerWriter.WriteInt(0); // placeholder for table len
                var sortedEntries = m_entries.Values.OrderBy(x => x.Text);
                foreach (var entry in sortedEntries)
                {
                    headerWriter.WriteInt(entry.Key);
                }

                var searchTableLen = headerWriter.Position - searchTablePos - 4;
                headerWriter.Seek((int)searchTablePos);
                headerWriter.WriteInt((int)searchTableLen); // write table len

                var indexPos = (int)contentWriter.Position;

                /* write index at end */
                byte[] indexData = headerWriter.Data;
                contentWriter.WriteInt(indexLen);
                contentWriter.WriteBytes(indexData);

                /* write index pos at begin */
                contentWriter.Seek(0, SeekOrigin.Begin);
                contentWriter.WriteInt(indexPos);
            }
        }
Ejemplo n.º 8
0
        public void Save(string uri)
        {
            File.Delete(uri);
            var writer = new BigEndianWriter();

            writer.Seek(4, SeekOrigin.Begin);

            var newIndexes     = new Dictionary <int, long>();
            var newTextIndexes = new Dictionary <string, long>();

            foreach (var oldIndex in _indexes)
            {
                newIndexes.Add(oldIndex.Key, writer.Position);
                writer.WriteUTF(GetText(oldIndex.Key));
            }
            foreach (var oldIndex in _textIndexes)
            {
                newTextIndexes.Add(oldIndex.Key, writer.Position);
                writer.WriteUTF(GetText(oldIndex.Key));
            }

            var oldPos = writer.Position;//end of the strings and start of the indexes

            writer.Seek(0, SeekOrigin.Begin);
            writer.WriteInt((int)oldPos);
            writer.Seek((int)oldPos, SeekOrigin.Begin);

            var subWriter = new BigEndianWriter();

            foreach (var newIndex in newIndexes)
            {
                Tuple <bool, int> newUnDiacriticalIndex;
                _unDiacriticalIndexes.TryGetValue(newIndex.Key, out newUnDiacriticalIndex);

                subWriter.WriteInt(newIndex.Key);                    //id
                subWriter.WriteBoolean(newUnDiacriticalIndex.Item1); //dialectCriticalText
                subWriter.WriteInt((int)newIndex.Value);             //position

                if (newUnDiacriticalIndex.Item1)
                {
                    subWriter.WriteInt(newUnDiacriticalIndex.Item2);
                }
            }
            var subData = subWriter.Data;

            writer.WriteInt(subData.Length);
            writer.WriteBytes(subData);

            subWriter.Clear();
            foreach (var textIndex in newTextIndexes)
            {
                subWriter.WriteUTF(textIndex.Key);
                subWriter.WriteInt((int)textIndex.Value);
            }
            subData = subWriter.Data;
            writer.WriteInt(subData.Length);
            writer.WriteBytes(subData);

            subWriter.Clear();
            foreach (var textSortIndex in newIndexes)
            {
                subWriter.WriteInt(textSortIndex.Key);
            }
            subData = subWriter.Data;
            writer.WriteInt(subData.Length);
            writer.WriteBytes(subData);

            File.WriteAllBytes(uri, writer.Data);
            writer.Dispose();
        }