Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible bug in IOStream #10

Open
CheloXL opened this issue May 4, 2015 · 2 comments
Open

Possible bug in IOStream #10

CheloXL opened this issue May 4, 2015 · 2 comments

Comments

@CheloXL
Copy link

CheloXL commented May 4, 2015

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
Copy link
Owner

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
Copy link
Author

CheloXL commented May 5, 2015

Yes, that looks correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants