public FTMessageTreeRoot Parser() { FTBufferRead reader = new FTBufferRead(_allBytes); FTMessageTreeRoot root = new FTMessageTreeRoot(); using (IFTStreamReader streamReader = new FTStreamReaderForPage(reader)) { root.Parse(streamReader); } return root; }
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 GenerateMessageBin(FTMessageTreeRoot root) { using (IFTStreamWriter writer = new FTStreamWriter(newTransferBin)) { root.GetByteData(writer); } }
private void ProcessTransferBuffer() { 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; } GenerateTransferBinFile(buffer, oldTransferBin); GenerateTransferTxtFile(buffer, oldTransferTxt); FTBufferRead reader = new FTBufferRead(allBuffer); FTStreamParseContext.NewContext(reader); FTMessageTreeRoot root = new FTMessageTreeRoot(); using (FTStreamParseContext.Instance) { root.Parse(); } GenerateMessageBin(root); GenerateMessageTransferTxtFile(); 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++; Assert.AreNotSame(b1, b2, string.Format("In index [2], oldByte:[0] is not equal newByte [1]", b1, b2, readed)); } } } IList<IFTTransferUnit> allTransferUnit = new List<IFTTransferUnit>(); root.GetAllTransferUnit(allTransferUnit); int allUnitCount = 0; foreach (IFTTransferUnit unit in allTransferUnit) { allUnitCount += unit.BytesCount; } Assert.AreNotSame(allUnitCount, root.BytesCount, string.Format("allUnitCountByte:[0] is not equal message all Bytes:[1]", allUnitCount, root.BytesCount)); Assert.AreNotSame(allUnitCount, length, string.Format("after parse, the bytes count:[0] is not equal old bin file length:[1].", allUnitCount, length)); GenerateStreamByMessage(allTransferUnit); }