The read method returns the number of bytes returned, which may be less than the number of bytes requested. Normally when you read a file, you will get all the bytes that you ask for (unless you reach the end of the file), however, you can't count on it always being that way.
It's possible that the system will make a difference between data that is immediately available and data that needs time to be retrieved, so that it will return the data currently available right away, start reading more data in the background and expect you to request the rest of the data in another call. AFAIK it doesn't do this currently, but it's a reasonable future scenario.
You should get the result of the Read
method and use that to determine how much data you got. You shouldn't read it into the buffer at the location of offset
, then you can't read a file that is larger than the buffer. Alternatively, you can declare an array to hold the entire stream, then you would read the data into the location of offset
.
You should also handle the situation where the Read
method returns zero, which means that there is no more data to read. This normally doesn't happen until you reach the end of the file, but if it would it would throw your code into an eternal loop.
byte[] data = new byte[30];
int numBytesToRead = (int)fStream.Length;
int offset = 0;
//reading
while (numBytesToRead > 0) {
int len = fStream.Read(data, 0, data.Length);
offset += len;
numBytesToRead -= len;
if (len == 0 && numBytesToRead > 0) {
// error: unexpected end of file
}
//do something with the data (len bytes)
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…