private static void ProcessTransferBuffer() { const string oldTransferBin = "D:\\EWSTest\\OldTransfer.bin"; const string oldTransferTxt = "D:\\EWSTest\\OldTransfer.txt"; const string newTransferBin = "D:\\EWSTest\\NewTransfer.bin"; const string newTransferTxt = "D:\\EWSTest\\NewTransfer.txt"; const string outputFile = "D:\\EWSTest\\Output.bin"; const string templateFile = "D:\\EWSTest\\Template.bin"; if (File.Exists(oldTransferBin)) File.Delete(oldTransferBin); if (File.Exists(oldTransferTxt)) File.Delete(oldTransferTxt); if (File.Exists(newTransferBin)) File.Delete(newTransferBin); if (File.Exists(newTransferTxt)) File.Delete(newTransferTxt); if (File.Exists(outputFile)) File.Delete(outputFile); var length = 0; foreach (byte[] temp in allBuffer) { length += temp.Length; } byte[] buffer = new byte[length]; length = 0; foreach (byte[] temp in allBuffer) { Array.Copy(temp, 0, buffer, length, temp.Length); length += temp.Length; } using (FileStream fileStream = new FileStream(oldTransferBin, FileMode.OpenOrCreate)) { fileStream.Write(buffer, 0, buffer.Length); } var split = "\t"; int index = 0; using (StreamWriter writer = new StreamWriter(oldTransferTxt)) { writer.Write("Num"); writer.Write(split); writer.Write("Hex"); writer.Write(split); writer.Write("Dec"); writer.Write(split); writer.Write("Ch"); writer.WriteLine(); foreach (byte b in buffer) { writer.Write(index++); writer.Write(split); writer.Write(b.ToString("X2")); writer.Write(split); writer.Write((int)b); writer.Write(split); writer.Write((char)b); writer.WriteLine(); } } FTBufferRead reader = new FTBufferRead(allBuffer); FTStreamParseContext.NewContext(reader); FTMessageTreeRoot root = new FTMessageTreeRoot(); using (FTStreamParseContext.Instance) { root.Parse(); } using (IFTStreamWriter writer = new FTStreamWriter(newTransferBin)) { root.GetByteData(writer); } index = 0; using (FileStream readerAfterWriter = new FileStream(newTransferBin, FileMode.Open)) { using (StreamWriter writer = new StreamWriter(newTransferTxt)) { writer.Write("Num"); writer.Write(split); writer.Write("Hex"); writer.Write(split); writer.Write("Dec"); writer.Write(split); writer.Write("Ch"); writer.WriteLine(); while (true) { if (readerAfterWriter.Length == readerAfterWriter.Position) { break; } var b = readerAfterWriter.ReadByte(); writer.Write(index++); writer.Write(split); writer.Write(b.ToString("X2")); writer.Write(split); writer.Write((int)b); writer.Write(split); writer.Write((char)b); writer.WriteLine(); } } } using (FileStream readerCom = new FileStream(oldTransferBin, FileMode.Open)) { using (FileStream readerAfterWriter = new FileStream(newTransferBin, FileMode.Open)) { int readed = 0; while (true) { if (readerCom.Length == readerCom.Position || readerAfterWriter.Length == readerAfterWriter.Position) { break; } var b1 = readerCom.ReadByte(); var b2 = readerAfterWriter.ReadByte(); readed++; if (b1 != b2) { throw new NotSupportedException(); } } } } IList<IFTTransferUnit> allTransferUnit = new List<IFTTransferUnit>(); root.GetAllTransferUnit(allTransferUnit); int allUnitCount = 0; foreach(IFTTransferUnit unit in allTransferUnit) { allUnitCount += unit.BytesCount; } if (allUnitCount != root.BytesCount) throw new NotSupportedException(); using (FileStream outputStream = new FileStream(outputFile, FileMode.CreateNew)) { using (FTFileStream ftFileStream = new FTFileStream(templateFile)) { using (BinaryWriter writer = new BinaryWriter(outputStream)) { ftFileStream.WriteConfigs(writer); ftFileStream.WriteTransferData(allTransferUnit, writer); } } } }
private void GenerateStreamByMessage(IList<IFTTransferUnit> allTransferUnit) { using (FileStream outputStream = new FileStream(outputFile, FileMode.CreateNew)) { using (BinaryWriter writer = new BinaryWriter(outputStream)) { using (FTFileStream ftFileStream = new FTFileStream(templateFile, writer)) { ftFileStream.WriteConfigs(); ftFileStream.WriteTransferData(allTransferUnit); } } } }