I am a little confused as to the steps of reading a file into buffer gradually.
from the MSDN docs
public abstract int Read(
byte[] buffer,
int offset,
int count
)
source from C# Examples
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
try
{
int length = (int)fileStream.Length; // get file length
buffer = new byte[length]; // create buffer
int count; // actual number of bytes read
int sum = 0; // total number of bytes read
// read until Read method returns 0 (end of the stream has been reached)
while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
sum += count; // sum is a buffer offset for next reading
can I say that the line fileStream.Read(buffer, sum, length - sum)
reads as "Read fileStream
from sum
(offset) to length - sum
(total bytes to read) into buffer
". OK so at the start, when sum
= 0, I will effectively read the whole fileStream into buffer in 1 short, but I think this is not the case. Maybe Read()
reads whatever it possibly can into buffer? then returns so that you can Read()
it again? I am a little confused.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…