标签:osi mod pac oid lis for ++ tac 处理
最近有个需求,将内存和帧数数据序列化到二进制文件。为了节省内存,采用了100个对象的数组,每次存满就序列化到文件,然后归零继续存,以追加的性式继续序列化到原有文件。
这样相当于多个数组对象存在了同一个序列化文件。反序列化的时候需要处理一下。思路是先反序列化出来一个,然后修改文件读取的Offset,继续反序列化下一个,直到文件末尾。
1 namespace ProfilterDataNS 2 { 3 [Serializable] 4 public class ProfilterData 5 { 6 public float fps=0; 7 public float memory=0; 8 } 9 }
1 using System.Collections; 2 using System.Collections.Generic; 3 using ProfilterDataNS; 4 using System.IO; 5 using System.Runtime.Serialization.Formatters.Binary; 6 7 public class DataCache{ 8 string _filePath="profilterData.bin"; 9 int _limitNum=100; 10 int _index=0; 11 ProfilterData [] _cacheArr;//=new ProfilterData[limitNum]; 12 13 private BinaryFormatter binFormat = new BinaryFormatter();//创建二进制序列化器 14 15 public DataCache(int limitNum,string filePath) 16 { 17 _index=0; 18 _filePath=filePath; 19 _limitNum=limitNum; 20 _cacheArr=new ProfilterData[limitNum]; 21 for(int i=0;i<_cacheArr.Length;i++) 22 { 23 _cacheArr[i]=new ProfilterData(); 24 } 25 FileStream fStream = new FileStream(_filePath,FileMode.Create); 26 fStream.Close(); 27 } 28 /// <summary> 29 /// 添加数据 30 /// </summary> 31 /// <param name="fps"></param> 32 /// <param name="memory"></param> 33 public void Append(float fps,float memory) 34 { 35 if(_index==_limitNum-1) 36 { 37 WriteData(_cacheArr); 38 } 39 ProfilterData profData=_cacheArr[_index]; 40 profData.fps=fps; 41 profData.memory=memory; 42 _index++; 43 } 44 45 46 /// <summary> 47 /// 立即结算 48 /// </summary> 49 public void SettleAtOnce() 50 { 51 if(_index==0) 52 return; 53 54 ProfilterData [] tempArr=new ProfilterData[_index]; 55 for(int i=0;i<tempArr.Length;i++) 56 { 57 tempArr[i]=_cacheArr[i]; 58 } 59 WriteData(tempArr); 60 } 61 62 /// <summary> 63 /// 写入数据 64 /// </summary> 65 private void WriteData(ProfilterData [] arr) 66 { 67 using (FileStream fStream = new FileStream(_filePath,FileMode.Append,FileAccess.Write)) 68 { 69 binFormat.Serialize(fStream, arr); 70 fStream.Close(); 71 } 72 _index=0; 73 } 74 }
1 using System.Collections; 2 using System.Collections.Generic; 3 using ProfilterDataNS; 4 using System.IO; 5 using System.Runtime.Serialization.Formatters.Binary; 6 7 public class AnalyzeData{ 8 public static List<ProfilterData> Analyze(string filePath) 9 { 10 MemoryStream ms = new MemoryStream(); 11 BinaryFormatter binFormat = new BinaryFormatter();//创建二进制序列化器 12 13 List<ProfilterData> profList=new List<ProfilterData>(); 14 15 using (FileStream fs= File.OpenRead("profilterData.bin")) 16 { 17 int offset=0; 18 while(fs.Position<fs.Length) 19 { 20 ProfilterData []dataArr=(ProfilterData[])binFormat.Deserialize(fs); 21 profList.AddRange(dataArr); 22 binFormat.Serialize(ms, dataArr);//序列化到内存流中 23 byte[] bt = ms.ToArray(); 24 offset=bt.Length;//获取偏移量 25 } 26 } 27 return profList; 28 } 29 }
标签:osi mod pac oid lis for ++ tac 处理
原文地址:https://www.cnblogs.com/luxishi/p/9172407.html