Search code examples
streamreaderreadblock

ArgumentException while reading using readblock streamreader


I am trying to calculate row count from a large file based on presence of a certain character and would like to use StreamReader and ReadBlock - below is my code.

 protected virtual long CalculateRowCount(FileStream inStream, int bufferSize)
        {
            long rowCount=0;
            String line;

            inStream.Position = 0;

            TextReader reader = new StreamReader(inStream);

            char[] block = new char[4096];
            const int blockSize = 4096;
            int indexer = 0;
            int charsRead = 0;
            long numberOfLines = 0;
            int count = 1;
            do
            {

                charsRead = reader.ReadBlock(block, indexer, block.Length * count);
                indexer += blockSize ;

                numberOfLines = numberOfLines + string.Join("", block).Split(new string[] { "&ENDE" }, StringSplitOptions.None).Length;
                count ++;

            } while (charsRead == block.Length);//charsRead !=0


            reader.Close();
            fileRowCount = rowCount;
            return rowCount;

        }

But I get error

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.

I am not sure what is wrong... Can you help. Thanks ahead!


Solution

  • For one, read the StreamReader.ReadBlock() documentation carefully http://msdn.microsoft.com/en-us/library/system.io.streamreader.readblock.aspx and compare with what you're doing:

    • The 2nd argument (indexer) should be within the range of the block you're passing in, but you're passing something that will probably exceed it after one iteration. Since it looks like you want to reuse the memory block, pass 0 here.
    • The 3rd argument (count) indicates how many bytes to read into your memory block; passing something larger than the block size might not work (depends on implementation)
    • ReadBlock() returns the number of bytes actually read, but you increment indexer as if it will always return the size of the block exactly (most of the time, it won't)