如何填写为0xFF字节一个MemoryStream? [英] How to fill a MemoryStream with 0xFF bytes?

查看:284
本文介绍了如何填写为0xFF字节一个MemoryStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的MemoryStream

这是在运行时从文件创建,然后将的MemoryStream 编辑和一些字节被删除。

Then the MemoryStream is edited and some bytes are removed.

现在我必须保持恒定的文件大小,所以我必须填写的MemoryStream 为0xFF字节。

Now I have to maintain a Constant Filesize so I have to fill the MemoryStream with 0xFF bytes..

什么是做这个工作的最快方法?

What is the Fastest way to Do this Operation?

我知道,我总是可以遍历的MemoryStream 尺寸,并添加为0xFF的,但我需要知道一个更快,更有效的方式来做到这一点!

I know, that I always can loop through the MemoryStream sizes and add 0xFF's but I need to know a faster and more efficient way to do it!

推荐答案

如果你有很多要写入的字节流中,它可能是更有效写一个数组,而不是单独的每个字节:

If you have many bytes to write to the stream, it may be more efficient to write a array rather than each byte individually:

static void Fill(this Stream stream, byte value, int count)
{
    var buffer = new byte[64];
    for (int i = 0; i < buffer.Length; i++)
    {
        buffer[i] = value;
    }
    while (count > buffer.Length)
    {
        stream.Write(buffer, 0, buffer.Length);
        count -= buffer.Length;
    }
    stream.Write(buffer, 0, count);
}

这篇关于如何填写为0xFF字节一个MemoryStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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