我在dll中有一个类,它解析一个文件并返回一个流,它表示FAT图像(或任何其他)
我的问题是,当有任何其他映像时,类在流的开头创建大约3702 (平均)空字节。
因此,我必须首先编辑流,然后将其保存到文件中。
我已经有密码了,但效果很慢。
注意: fts是返回的FileStream。
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();现在,我的问题是:
如何比上面的代码更有效和更快地删除空值?
发布于 2012-03-17 16:19:48
与其将字节推到列表中,您可以简单地遍历流,直到找到第一个非空字节,然后使用Array.Copy复制数组。
我会想到这样的事情(未经测试的代码):
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.https://stackoverflow.com/questions/9751512
复制相似问题