ResultDataIOHelper.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.IO;
  3. using System.Runtime.Serialization.Formatters.Binary;
  4. using Vinno.DataManager.Utilities;
  5. using Vinno.DataTypes;
  6. namespace WingAIDiagnosisService.URMManage
  7. {
  8. public class ResultDataIOHelper
  9. {
  10. public static void WriteArrayToFile(string filePath, NativeArray array, int length)
  11. {
  12. using (BinaryWriter writer = new BinaryWriter(File.Open(filePath, FileMode.Create)))
  13. {
  14. unsafe
  15. {
  16. double* data = (double*)array.Start.ToPointer();
  17. for (int i = 0; i < length; i++)
  18. {
  19. writer.Write(data[i]);
  20. }
  21. }
  22. }
  23. }
  24. public static void WriteByteArrayToFile(string filePath, NativeArray array, int length)
  25. {
  26. using (BinaryWriter writer = new BinaryWriter(File.Open(filePath, FileMode.Create)))
  27. {
  28. unsafe
  29. {
  30. byte* data = (byte*)array.Start.ToPointer();
  31. for (int i = 0; i < length; i++)
  32. {
  33. writer.Write(data[i]);
  34. }
  35. }
  36. }
  37. }
  38. public static bool WriteURMPointsToFile(string filepath, URMPoint[] points)
  39. {
  40. try
  41. {
  42. using (FileStream stream = new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.None))
  43. {
  44. BinaryFormatter formatter = new BinaryFormatter();
  45. formatter.Serialize(stream, points);
  46. }
  47. return true;
  48. }
  49. catch (Exception e)
  50. {
  51. // Console.WriteLine("An error occurred while writing to file: " + e.Message);
  52. return false;
  53. }
  54. }
  55. public static bool WriteURMPointNumsToFile(string filepath, int[] nums)
  56. {
  57. try
  58. {
  59. using (FileStream stream = new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.None))
  60. {
  61. BinaryFormatter formatter = new BinaryFormatter();
  62. formatter.Serialize(stream, nums);
  63. }
  64. return true;
  65. }
  66. catch (Exception e)
  67. {
  68. // Console.WriteLine("An error occurred while writing to file: " + e.Message);
  69. return false;
  70. }
  71. }
  72. public static NativeArray ReadDoubleArrayFromFile(string filePath, int length)
  73. {
  74. NativeArray array = new NativeArray(length * 8);
  75. using (BinaryReader reader = new BinaryReader(File.Open(filePath, FileMode.Open)))
  76. {
  77. unsafe
  78. {
  79. double* data = (double*)array.Start.ToPointer();
  80. for (int i = 0; i < length; i++)
  81. {
  82. data[i] = reader.ReadDouble();
  83. }
  84. }
  85. }
  86. return array;
  87. }
  88. public static NativeArray ReadByteArrayFromFile(string filePath, int length)
  89. {
  90. NativeArray array = new NativeArray(length);
  91. using (BinaryReader reader = new BinaryReader(File.Open(filePath, FileMode.Open)))
  92. {
  93. unsafe
  94. {
  95. byte* data = (byte*)array.Start.ToPointer();
  96. for (int i = 0; i < length; i++)
  97. {
  98. data[i] = reader.ReadByte();
  99. }
  100. }
  101. }
  102. return array;
  103. }
  104. public static URMPoint[] ReadURMPointsFromFile(string filepath)
  105. {
  106. try
  107. {
  108. using (FileStream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read))
  109. {
  110. BinaryFormatter formatter = new BinaryFormatter();
  111. return (URMPoint[])formatter.Deserialize(stream);
  112. }
  113. }
  114. catch (Exception e)
  115. {
  116. // Console.WriteLine("An error occurred while reading from file: " + e.Message);
  117. return null;
  118. }
  119. }
  120. public static int[] ReadURMPointNumsFromFile(string filepath)
  121. {
  122. try
  123. {
  124. using (FileStream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read))
  125. {
  126. BinaryFormatter formatter = new BinaryFormatter();
  127. return (int[])formatter.Deserialize(stream);
  128. }
  129. }
  130. catch (Exception e)
  131. {
  132. // Console.WriteLine("An error occurred while reading from file: " + e.Message);
  133. return null;
  134. }
  135. }
  136. }
  137. }