首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从流的开头删除空字节

从流的开头删除空字节
EN

Stack Overflow用户
提问于 2012-03-17 16:08:00
回答 1查看 1.4K关注 0票数 0

我在dll中有一个类,它解析一个文件并返回一个流,它表示FAT图像(或任何其他)

我的问题是,当有任何其他映像时,类在流的开头创建大约3702 (平均)空字节。

因此,我必须首先编辑流,然后将其保存到文件中。

我已经有密码了,但效果很慢。

注意: fts是返回的FileStream。

代码语言:javascript
复制
BufferedStream bfs = new BufferedStream(fts);
BinaryReader bbr = new BinaryReader(bfs);
byte[] all_bytes = bbr.ReadBytes((int)fts.Length);

List<byte> nls = new List<byte>();
int index = 0;

foreach (byte bbrs in all_bytes)
{
    if (bbrs == 0x00)
    {
        index++;
        nls.Add(bbrs);
    }
    else
    {
        break;
    }
}

byte[] nulls = new byte[nls.Count];
nulls = nls.ToArray();

//File.WriteAllBytes(outputDir + "Nulls.bin", nulls);
long siz = fts.Length - index;
byte[] file = new byte[siz];

bbr.BaseStream.Position = index;
file = bbr.ReadBytes((int)siz);

bbr.Close();
bfs.Close();
fts.Close();

bfs = null;
fts = null;

fts = new FileStream(outputDir + "Image.bin", FileMode.Create, FileAccess.Write);
bfs = new BufferedStream(fts);

bfs.Write(file, 0, (int)siz);

bfs.Close();
fts.Close();

现在,我的问题是:

如何比上面的代码更有效和更快地删除空值?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-17 16:19:48

与其将字节推到列表中,您可以简单地遍历流,直到找到第一个非空字节,然后使用Array.Copy复制数组。

我会想到这样的事情(未经测试的代码):

代码语言:javascript
复制
int index = 0;
int currByte = 0;

while ((currByte = bbrs.ReadByte()) == 0x00)
{
    index++;
}

// now currByte and everything to the end of the stream are the bytes you want.
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9751512

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档