I wrote an extension method as following
public static void ReadBlock(this Stream stream, Action<string> action, int bufferSize = 8192)
{
var buffer = new byte[bufferSize];
while (true)
{
var count = stream.Read(buffer, 0, bufferSize);
if (count == 0) break;
var text = Encoding.ASCII.GetString(buffer);
action(text);
}
}
The problem I have is the last block which is often less than the size of the buffer is not read. and the code returns the n-1 block again (the last buffer).
Can anyone help me to read a stream block by block in the correct way based on buffer size?
Encoding.ASCII.GetString(buffer, 0, count)