I've got a class I need to read a data from. It's derived from TextReader (particularly FilterReader from http://www.codeproject.com/KB/cs/IFilter.aspx ). ReadToEnd() hangs when reading large files. ReadLine() doesn't work at all, so the only way to read in chunks is using .Read() function. MSDN suggests using Peek() and check if it returns -1 to detect if no more data is available. Unfortunately, FilterReader doesn't implement it, and it always returns -1. Is there any other way I can read all available content in chunks from this file?
A:
If the standard methods ReadToEnd
and ReadLine
hang, then clearly there’s a pretty severe bug in the FilterReader
implementation. Find it and fix it before using this in production code.
If for some reason you can’t fix it, write a new FilterReaderFixed
class which wraps a FilterReader
and implements Peek
correctly (by actually reading a character and storing it in a private field, and remembering to return it when Read
is called).
Timwi
2010-10-31 11:28:16
I'm looking into the original code. Just added the following block: public override int Peek() { return base.Peek(); }What do you mean by "remembering to return it when Read is called"?
Sphynx
2010-10-31 12:14:42
Please see the new question at http://stackoverflow.com/questions/4063272/fix-bug-in-filterreader-derived-from-textreader, thanks
Sphynx
2010-10-31 13:29:29
@Sphynx: Sorry, I’m not going to work for you and spoonfeed you the solution. Programming requires thinking.
Timwi
2010-10-31 16:02:11