【发布时间】:2012-03-18 00:25:37
【问题描述】:
我在 dll 中有一个类,它解析文件并返回代表 FAT 图像(或任何 其他)的 Stream
我的问题是当有任何 other 图像时,该类在流的开头创建大约 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();
现在,我的问题是:
我怎样才能比上面的代码更有效、更快地删除空值?
【问题讨论】: