C# .NET:当 FileStream 缓冲区填满时,BinaryWriter 是否会刷新?
我遇到了一些微不足道的事情,但当我缓冲的数据达到 FileStream 缓冲区的大小时,数据似乎被刷新到磁盘(从 FileStream 的缓冲区中)。
//use the FileStream buffer to actually buffer the data to be written, so segments are written as desired.
FileStream writeStream = new FileStream(filename, FileMode.Append, FileAccess.Write, FileShare.None, CommandOperationBufferSize);
BinaryWriter binWriter = new BinaryWriter(writeStream);
byte[] FullSize = new byte[CommandOperationTotalSize];
//the BinaryWriter will flush when the FileStream buffer is hit
binWriter.Write(FullSize); //DATA FLUSHES TO DISK HERE!
//if wait, wait five seconds
if (CommandOperation == "writewait" || CommandOperation == "appendwait")
{
Thread.Sleep(5000);
writeStream.Flush();
Thread.Sleep(5000);
}
writeStream.Close();
writeStream.Dispose();
binWriter.Close();
谁能证实情况确实如此?当 FileStream 的缓冲区已满时,FileStream 的缓冲区是实际的 .Flush() 吗?
我之所以这么问,是因为如果我将 CommandOperationTotalSize 设置为 1MB,并将 CommandOperationBufferSize 设置为 64KB,则当缓冲区填满时,数据将刷新到磁盘。
听起来好像我回答了我自己的问题,但奇怪的是 FileStream 缓冲区不会只是溢出?但也许 API 开发人员只是想表现得友善一些?
谢谢,
马特
I'm coming across something trivial, but it appears that data is flushed to disk (out of the FileStream's buffer) when the data I'm buffering hits the size of the FileStream's buffer.
//use the FileStream buffer to actually buffer the data to be written, so segments are written as desired.
FileStream writeStream = new FileStream(filename, FileMode.Append, FileAccess.Write, FileShare.None, CommandOperationBufferSize);
BinaryWriter binWriter = new BinaryWriter(writeStream);
byte[] FullSize = new byte[CommandOperationTotalSize];
//the BinaryWriter will flush when the FileStream buffer is hit
binWriter.Write(FullSize); //DATA FLUSHES TO DISK HERE!
//if wait, wait five seconds
if (CommandOperation == "writewait" || CommandOperation == "appendwait")
{
Thread.Sleep(5000);
writeStream.Flush();
Thread.Sleep(5000);
}
writeStream.Close();
writeStream.Dispose();
binWriter.Close();
Can anyone confirm that this is the case? That the FileStream's buffer is actual .Flush() when the FileStream's buffer is filled?
I ask because it appears that if I set CommandOperationTotalSize to 1MB, and set the CommandOperationBufferSize to 64KB, data is flushed to disk when the buffer is filled.
Sounds like I answered my own question, but it seems odd that the FileStream buffer wouldn't just overflow? But maybe the API developers are trying to be nice?
Thanks,
Matt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以很容易地假设缓冲区溢出是不可能的。如果是这种情况,该类将很难使用,因为 FileStream 根本没有属性来告诉您当前正在缓冲多少内容。
缓冲区仅用于减少对本机 Windows WriteFile() 调用的调用次数。当您写入少量数据(例如一次一个字节)时很重要。如果您没有明确指定缓冲区大小,那么它将使用 4096 字节的缓冲区。这很好,很少需要其他东西。任何写入都会由文件系统缓存进一步缓冲。使用 FileOptions.WriteThrough 时应仅考虑非标准大小
You can readily assume that overflowing the buffer is not possible. The class would be rather hard to use if that was the case, given that FileStream has no properties at all to tell you how much is currently being buffered.
The buffer is only there to reduce the number of calls to the native Windows WriteFile() call. Important when you write small amounts of data, say one byte at a time. If you don't explicitly specify the buffer size then it will use a buffer of 4096 bytes. Which is fine, it is very rare to need something else. Any writes are further buffered by the file system cache. You should only consider a non-standard size when you use FileOptions.WriteThrough