ResultDataIOHelper.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
  76. {
  77. using (BinaryReader reader = new BinaryReader(stream))
  78. {
  79. unsafe
  80. {
  81. double* data = (double*)array.Start.ToPointer();
  82. for (int i = 0; i < length; i++)
  83. {
  84. data[i] = reader.ReadDouble();
  85. }
  86. }
  87. reader.Close(); // 在这里手动关闭BinaryReader对象
  88. }
  89. }
  90. // using (BinaryReader reader = new BinaryReader(File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)))
  91. // {
  92. // unsafe
  93. // {
  94. // double* data = (double*)array.Start.ToPointer();
  95. // for (int i = 0; i < length; i++)
  96. // {
  97. // data[i] = reader.ReadDouble();
  98. // }
  99. // }
  100. // }
  101. return array;
  102. }
  103. public static NativeArray ReadByteArrayFromFile(string filePath, int length)
  104. {
  105. NativeArray array = new NativeArray(length);
  106. using (BinaryReader reader = new BinaryReader(File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)))
  107. {
  108. unsafe
  109. {
  110. byte* data = (byte*)array.Start.ToPointer();
  111. for (int i = 0; i < length; i++)
  112. {
  113. data[i] = reader.ReadByte();
  114. }
  115. }
  116. }
  117. return array;
  118. }
  119. public static URMPoint[] ReadURMPointsFromFile(string filepath)
  120. {
  121. try
  122. {
  123. using (FileStream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read))
  124. {
  125. BinaryFormatter formatter = new BinaryFormatter();
  126. return (URMPoint[])formatter.Deserialize(stream);
  127. }
  128. }
  129. catch (Exception e)
  130. {
  131. // Console.WriteLine("An error occurred while reading from file: " + e.Message);
  132. return null;
  133. }
  134. }
  135. public static int[] ReadURMPointNumsFromFile(string filepath)
  136. {
  137. try
  138. {
  139. using (FileStream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read))
  140. {
  141. BinaryFormatter formatter = new BinaryFormatter();
  142. return (int[])formatter.Deserialize(stream);
  143. }
  144. }
  145. catch (Exception e)
  146. {
  147. // Console.WriteLine("An error occurred while reading from file: " + e.Message);
  148. return null;
  149. }
  150. }
  151. }
  152. }