quark

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  49 随笔 :: 10 文章 :: 40 评论 :: 19万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

.NET4.0 + MemoryMapping + ReadByte()

该方法的思路主要是通过内存映射的原理,访问文件内容,由于在.net环境下不能一次性映射太大的文件,所以仍然采用分块映射的方式:

主要代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/// <summary>
/// MemoryMapping + ReadByte()
/// </summary>
unsafe static void CalulateLine_MemoryMapping_ReadByte(uint oneBlockSize)
{
    const string FILE_MAPPING_NAME = "~MappingTemp";
    const int LINE_MIN_SIZE = 30;
 
    long lineCount = 0;
 
    IntPtr fileHandle = ShareMemory.CreateFile(
        FILE_NAME,
        ShareMemory.GENERIC_READ | ShareMemory.GENERIC_WRITE,
        FileShare.Read | FileShare.Write,
        IntPtr.Zero,
        FileMode.Open,
        ShareMemory.FILE_ATTRIBUTE_NORMAL | ShareMemory.FILE_FLAG_SEQUENTIAL_SCAN,
        IntPtr.Zero);
 
    uint fileSize = ShareMemory.GetFileSize(fileHandle, IntPtr.Zero);
 
    if (ShareMemory.INVALID_HANDLE_VALUE != (int)fileHandle)
    {
        IntPtr mappingHandle = ShareMemory.CreateFileMapping(
            (int)fileHandle,
            IntPtr.Zero,
            ShareMemory.PAGE_READWRITE,
            0,
            0,
            FILE_MAPPING_NAME);
        if (mappingHandle != IntPtr.Zero)
        {
            uint mapFlag = 0;
 
            while (mapFlag <= fileSize)
            {
                uint eachMappingSize = oneBlockSize;
                if (fileSize - mapFlag < oneBlockSize)
                {
                    eachMappingSize = fileSize - mapFlag;
                }
 
                IntPtr pHead = ShareMemory.MapViewOfFile(
                mappingHandle,
                (uint)(ShareMemory.FILE_MAP_READ),
                0,
                mapFlag,
                eachMappingSize);
 
                int lastError = ShareMemory.GetLastError();
 
                if (pHead != IntPtr.Zero)
                {
 
                    long flag = 0;
                    while (flag < eachMappingSize)
                    {
                        //byte* pbHead= (byte*)pHead;
                        //byte temp = *(pbHead + flag);
 
                        byte temp = Marshal.ReadByte((IntPtr)((int)pHead + flag));
 
                        if (temp == 0x0D)
                        {
                            lineCount++;
                            flag += LINE_MIN_SIZE;
                        }
                        flag++;
                    }
 
                    ShareMemory.UnmapViewOfFile(pHead);
                }
                mapFlag += oneBlockSize;
            }
 
            ShareMemory.CloseHandle(mappingHandle);
        }
        ShareMemory.CloseHandle(fileHandle);
 
    }
}

测试结果:

MemoryMapping ReadByte()

.NET4.0 + MemoryMapping + Unsafe

使用unsafe代码,就是在上面代码的基础上,做了一些简单的修改。

 

1
2
byte* pbHead = (byte*)pHead;
byte temp = *(pbHead + flag);
1
  

测试结果:

MemoryMapping Unsafe

posted on   QuarkZ  阅读(554)  评论(0编辑  收藏  举报
编辑推荐:
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
阅读排行:
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞
点击右上角即可分享
微信分享提示