BufferedStream
工作正常 - 它不刷新:stream.Length
是8
直到它關閉;即
// 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
也可以有自己的緩衝區
這裏是什麼? –
它應該是buff,sry – asdas