Filestream " Offset and length were out of bounds.. "

Posted on 2009-12-22
Last Modified: 2012-05-08
I'm getting an error when reading files - System.ArgumentException: Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection. Can anyone tell from this what I should be changing? it breaks on read = fs.Read(buffer, 0, Utility.chunkSize);

 using (FileStream fs = this.File.OpenRead())
                    //get full hash first.
                    this.FileLength = fs.Length;

                    //setup progress bar.
                    this.Steps = (int)(this.FileLength / (long)Utility.chunkSize);
                    this.uxProgress.Minimum = 0;
                    this.uxProgress.Maximum = this.Steps;

                    int read = 0;
                    byte[] buffer = null;

                    if (fs.Length <= Utility.chunkSize)
                        buffer = new byte[(int)fs.Length];
                        buffer = new byte[Utility.chunkSize];

                    read = fs.Read(buffer, 0, Utility.chunkSize);

                    this.FilePosition += read;

                    req.Chunk = buffer;
                    req.ChunkSize = buffer.Length;
                    req.Hash = Utility.GetSHA256Hash(buffer);


Question by:boukaka

    Author Comment

    I just noticed this only happens when a file is smaller than Utility.chunkSize (which is set to 102400), I can set it to something like (10000) but I'd rather fix the code to work no matter what size the file is...
    LVL 11

    Accepted Solution

    this line:
         read = fs.Read(buffer, 0, Utility.chunkSize);
    should be:
         read = fs.Read(buffer, 0, buffer.Length);

    in order to ensure you never try to put more data in your buffer than your buffer's actual size.
    LVL 2

    Expert Comment


    You misjudged the parameter values it seems...

    It must be like this...
    read=fs.Read(buffer, Index, bufferSize)
    where you can utilize the Length property of the array to retrieve the maximum capacity of the array.

    Abel Allen

    Author Closing Comment

    Ah I see, thank you!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now