Blocking Stream.Read
-
Hello, I'm using
Stream.Read(byte[] buffer, int offset, int count)
. Is there an alternative to that method (or a property to set) so that the method won't return until all count is read (or end of stream is reached)? Or should I do something like this:int n = 0, readCount = 0;
while ((n = myStream.Read(buffer, readCount, countToRead - readCount)) > 0)
readCount += n; -
Hello, I'm using
Stream.Read(byte[] buffer, int offset, int count)
. Is there an alternative to that method (or a property to set) so that the method won't return until all count is read (or end of stream is reached)? Or should I do something like this:int n = 0, readCount = 0;
while ((n = myStream.Read(buffer, readCount, countToRead - readCount)) > 0)
readCount += n;No, you can't force it to read the number of bytes that you request. You have to use a loop with the Read method if you want to read the entire stream. You should check if the return value from the method is zero, not only if the total number of bytes has reached the expected. If the stream happens to be smaller than you expect, your code will go into an eternal loop.
Despite everything, the person most likely to be fooling you next is yourself.
-
No, you can't force it to read the number of bytes that you request. You have to use a loop with the Read method if you want to read the entire stream. You should check if the return value from the method is zero, not only if the total number of bytes has reached the expected. If the stream happens to be smaller than you expect, your code will go into an eternal loop.
Despite everything, the person most likely to be fooling you next is yourself.
Yes, I edited the post and checked the return value, probably at the same time you were writing. :) Thanks.