123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- using System;
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
- using Vinno.DataManager.Utilities;
- using Vinno.DataTypes;
- namespace WingAIDiagnosisService.URMManage
- {
- public class ResultDataIOHelper
- {
- public static void WriteArrayToFile(string filePath, NativeArray array, int length)
- {
- using (BinaryWriter writer = new BinaryWriter(File.Open(filePath, FileMode.Create)))
- {
- unsafe
- {
- double* data = (double*)array.Start.ToPointer();
- for (int i = 0; i < length; i++)
- {
- writer.Write(data[i]);
- }
- }
- }
- }
- public static void WriteByteArrayToFile(string filePath, NativeArray array, int length)
- {
- using (BinaryWriter writer = new BinaryWriter(File.Open(filePath, FileMode.Create)))
- {
- unsafe
- {
- byte* data = (byte*)array.Start.ToPointer();
- for (int i = 0; i < length; i++)
- {
- writer.Write(data[i]);
- }
- }
- }
- }
- public static bool WriteURMPointsToFile(string filepath, URMPoint[] points)
- {
- try
- {
- using (FileStream stream = new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.None))
- {
- BinaryFormatter formatter = new BinaryFormatter();
- formatter.Serialize(stream, points);
- }
- return true;
- }
- catch (Exception e)
- {
- // Console.WriteLine("An error occurred while writing to file: " + e.Message);
- return false;
- }
- }
- public static bool WriteURMPointNumsToFile(string filepath, int[] nums)
- {
- try
- {
- using (FileStream stream = new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.None))
- {
- BinaryFormatter formatter = new BinaryFormatter();
- formatter.Serialize(stream, nums);
- }
- return true;
- }
- catch (Exception e)
- {
- // Console.WriteLine("An error occurred while writing to file: " + e.Message);
- return false;
- }
- }
- public static NativeArray ReadDoubleArrayFromFile(string filePath, int length)
- {
- NativeArray array = new NativeArray(length * 8);
- using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
- {
- using (BinaryReader reader = new BinaryReader(stream))
- {
- unsafe
- {
- double* data = (double*)array.Start.ToPointer();
- for (int i = 0; i < length; i++)
- {
- data[i] = reader.ReadDouble();
- }
- }
- reader.Close(); // 在这里手动关闭BinaryReader对象
- }
- }
- // using (BinaryReader reader = new BinaryReader(File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)))
- // {
- // unsafe
- // {
- // double* data = (double*)array.Start.ToPointer();
- // for (int i = 0; i < length; i++)
- // {
- // data[i] = reader.ReadDouble();
- // }
- // }
- // }
- return array;
- }
- public static NativeArray ReadByteArrayFromFile(string filePath, int length)
- {
- NativeArray array = new NativeArray(length);
- using (BinaryReader reader = new BinaryReader(File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)))
- {
- unsafe
- {
- byte* data = (byte*)array.Start.ToPointer();
- for (int i = 0; i < length; i++)
- {
- data[i] = reader.ReadByte();
- }
- }
- }
- return array;
- }
- public static URMPoint[] ReadURMPointsFromFile(string filepath)
- {
- try
- {
- using (FileStream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read))
- {
- BinaryFormatter formatter = new BinaryFormatter();
- return (URMPoint[])formatter.Deserialize(stream);
- }
- }
- catch (Exception e)
- {
- // Console.WriteLine("An error occurred while reading from file: " + e.Message);
- return null;
- }
- }
- public static int[] ReadURMPointNumsFromFile(string filepath)
- {
- try
- {
- using (FileStream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read))
- {
- BinaryFormatter formatter = new BinaryFormatter();
- return (int[])formatter.Deserialize(stream);
- }
- }
- catch (Exception e)
- {
- // Console.WriteLine("An error occurred while reading from file: " + e.Message);
- return null;
- }
- }
- }
- }
|