Possible bug in IOStream
Opened this issue · 2 comments
CheloXL commented
Hi,
Just checking the code, I see on https://github.com/csharptest/CSharpTest.Net.Collections/blob/master/src/CSharpTest.Net.Collections/IO/IOStream.cs#L68 that you are initializing bytesRead with 0. I believe the correct value should be "offset". Without that, the offset argument is meaningless in the context of that method.
csharptest commented
Let's take another look at the method in question:
public static int ReadChunk(Stream io, byte[] bytes, int offset, int length)
{
// So far we have read 0 bytes
int bytesRead = 0;
// A variable to track how much we read on each call to Stream.Read
int len = 0;
// While bytesRead is less than the requested length to read in 'length'
// And while Stream.Read does not return 0 bytes read
// BUG: Reads into bytes at an offset of bytesRead not (offset + bytesRead)
while (length > bytesRead && 0 != (len = io.Read(bytes, bytesRead, length - bytesRead)))
// Accumulate the number of bytes we have read
bytesRead += len;
// Return the number of bytes actually read
return bytesRead;
}
So the correct method should read:
public static int ReadChunk(Stream io, byte[] bytes, int offset, int length)
{
int bytesRead = 0;
int len = 0;
while (length > bytesRead && 0 != (len = io.Read(bytes, offset + bytesRead, length - bytesRead)))
bytesRead += len;
return bytesRead;
}
Does that look correct to you???
CheloXL commented
Yes, that looks correct.