zoukankan      html  css  js  c++  java
  • Fast Binary File Reading with C#

    public static TestStruct FromFileStream(FileStream fs) {     //Create Buffer      byte[] buff = new byte[Marshal.SizeOf(typeof(TestStruct))];      int amt = 0;      //Loop until we've read enough bytes (usually once)       while(amt < buff.Length)         amt += fs.Read(buff, amt, buff.Length-amt); //Read bytes       //Make sure that the Garbage Collector doesn't move our buffer       GCHandle handle = GCHandle.Alloc(buff, GCHandleType.Pinned);     //Marshal the bytes      TestStruct s =        (TestStruct)Marshal.PtrToStructure(handle.AddrOfPinnedObject(),        typeof(TestStruct));      handle.Free();//Give control of the buffer back to the GC       return s }
    public static TestStruct FromBinaryReaderBlock(BinaryReader br) {     //Read byte array      byte[] buff = br.ReadBytes(Marshal.SizeOf(typeof(TestStruct)));     //Make sure that the Garbage Collector doesn't move our buffer       GCHandle handle = GCHandle.Alloc(buff, GCHandleType.Pinned);     //Marshal the bytes      TestStruct s =        (TestStruct)Marshal.PtrToStructure(handle.AddrOfPinnedObject(),       typeof(TestStruct));     handle.Free();//Give control of the buffer back to the GC       return s; }
    public static TestStruct FromBinaryReaderField(BinaryReader br) {      TestStruct s = new TestStruct();//New struct       s.longField = br.ReadInt64();//Fill the first field       s.byteField = br.ReadByte();//Fill the second field       s.byteArrayField = br.ReadBytes(16);//...       s.floatField = br.ReadSingle();//...       return s; }
  • 相关阅读:
    Cookies的实际存储位置
    搭建Git本地服务器
    在Tomcat中部署war
    Server.xml配置解析
    Tomcat配置详解,配置文件server.xml详解
    将centos7打造成桌面系统
    英语词汇大全
    商场/超市常见英语标识
    商务英语词汇大全
    常用繁体字大全
  • 原文地址:https://www.cnblogs.com/free5ddvd/p/2230919.html
Copyright © 2011-2022 走看看