工作正常 -它BufferedStream
不会刷新:直到它关闭stream.Length
;8
IE
// note these should really use "using" statements of similar
FileStream stream = new FileStream("test", FileMode.Create);
BufferedStream buff = new BufferedStream(stream, 8);
BinaryWriter writer = new BinaryWriter(buff);
writer.Write(1);
writer.Write(2);
writer.Write(3);
Console.WriteLine(stream.Length); // 8
Console.WriteLine(buff.Length); // 12
原因buff.Length
是12
因为那是底层流的长度。基本上,BufferedStream
只需重新暴露下面的流,并确保它不会错过缓冲数据,Flush()
如果有缓冲数据,它会添加:
public override long Length
{
get
{
this.EnsureNotClosed();
if (this._writePos > 0)
{
this.FlushWrite();
}
return this._stream.Length;
}
}
因此:
Console.WriteLine(stream.Length); // 8
Console.WriteLine(buff.Length); // 12
Console.WriteLine(stream.Length); // 12
请注意,FileStream
也可以有自己的缓冲