private void BuildPacket(BigEndianWriter writer) { byte[] data = writer.Data; writer.Clear(); int messageLenghtType = ComputeTypeLen(data.Length); short header = ComputeStaticHeader((int)MessageId, messageLenghtType); writer.WriteShort(header); switch (messageLenghtType) { case 1: writer.WriteByte((byte)data.Length); break; case 2: writer.WriteShort((short)data.Length); break; case 3: writer.WriteByte((byte)(data.Length >> 16 & 255)); writer.WriteShort((short)(data.Length & 65535)); break; } writer.WriteBytes(data); }
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(); }