读取内存映射文件或者知道它的大小correctely阅读 [英] Read memory mapped file or knowing its size to read it correctely

查看:228
本文介绍了读取内存映射文件或者知道它的大小correctely阅读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个问题上,

<一个href="http://stackoverflow.com/questions/14953393/read-all-contents-of-memory-mapped-file-or-memory-mapped-view-accessor-without-k">Read映射视图访问内存映射文件或内存不知道它的大小所有内容

还有一个问题,(INT)stream.Length 不给我正确的长度,它,而给使用的内部缓冲区的大小!我需要刷新这个问题,因为它是非常pressing。

there is a problem, the (int)stream.Length is not giving me the correct length, it rather gives the size of the internal buffer used! I need to refresh this question because it is very pressing.

主要问题是:

我需要类似的东西ReadToEnd或ReadAllBytes阅读全部   在MemoryMappedFile的使用MappedViewAccessor如果我的内容   不知道它的大小,我该怎么办呢?

I need something similar to ReadToEnd or ReadAllBytes to read all of the contents of the MemoryMappedFile using the MappedViewAccessor if I don't know the size of it, how can I do it?

我已经寻找它,我已经看到了这个问题,但它不是   我要寻找的东西:

I have searched for it, I have seen this question, but it is not the thing I am looking for:

我如何能快速读取内存映射文件的字节在.NET?

How can I quickly read bytes from a memory mapped file in .NET?

旧的答案是:

酒吧

public static ReadMMFAllBytes(string fileName)
{
    using (var mmf = MemoryMappedFile.OpenExisting(fileName))
    {
        using (var stream = mmf.CreateViewStream())
        {
            using (BinaryReader binReader = new BinaryReader(stream))
            {
                return binReader.ReadBytes((int)stream.Length));
            }
        }
    }
}

在这个问题:

内存映射文件长度

有确切的问题没有确切的答案!问题是关于比标题别的东西。

There is no exact answer of the exact question! the question is about something else than the title.

推荐答案

最好的办法是在各种各样的固定长度的头信息中发送第一,而不是仅仅流原始字节。通过这种方式,第一个斑点,你读的是一致的长度,它给你,你需要阅读可变长度其余的信息。

The best approach is to send over a fixed-length header of sorts first, rather than just streaming raw bytes. This way the first blob you read is a consistent length and it gives you the info you need to read the variable-length remainder.

在简单的情况下,你的记录可以很简单,比如有书面第一长度字段,其次是有效载荷(你的字节)。根据您的需求,您可以将数据添加到像记录类型,版本等标题。

In the simplest case, your record could be as simple as having a length field written first, followed by the payload (your bytes). Depending on your needs, you can add data to the header like record type, version, etc.

这篇关于读取内存映射文件或者知道它的大小correctely阅读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆