G
Guest
Below are two quoted pieces of the IO.StreamReader.ReadToEnd documentation
(remarks section), and my questions follow. These sections seem to
discourage using ReadToEnd and they leave me cold as to what to do instead.
FYI, my use of ReadToEnd is reading web pages from 10K to 1M bytes in size.
1. "ReadToEnd assumes that the stream knows when it has reached an end. For
interactive protocols, in which the server sends data only when you ask for
it and does not close the connection, ReadToEnd might block indefinitely and
should be avoided."
Come again? ReadToEnd should be avoided in what circumstances? If I
replace ReadToEnd with ReadBlock or Read, aren't I up against the same
problem?
2. "If the current method throws an OutOfMemoryException, the reader's
position in the underlying Stream is advanced by the number of characters the
method was able to read, but the characters already read into the internal
ReadLine buffer are discarded. Since the position of the reader in the stream
cannot be changed, the characters already read are unrecoverable, and can be
accessed only by reinitializing the StreamReader. If the initial position
within the stream is unknown or the stream does not support seeking, the
underlying Stream also needs to be reinitialized. To avoid such a situation
and produce robust code you should use the Read method and store the read
characters in a preallocated buffer"
I can get exceptions with Read, so the same worries apply. But this section
started with OutOfMemoryException, so maybe it is just talking about huge
streams that ought to be read in pieces. If so, why beat around the bushes
and why not just say so? If something else, then what?
(remarks section), and my questions follow. These sections seem to
discourage using ReadToEnd and they leave me cold as to what to do instead.
FYI, my use of ReadToEnd is reading web pages from 10K to 1M bytes in size.
1. "ReadToEnd assumes that the stream knows when it has reached an end. For
interactive protocols, in which the server sends data only when you ask for
it and does not close the connection, ReadToEnd might block indefinitely and
should be avoided."
Come again? ReadToEnd should be avoided in what circumstances? If I
replace ReadToEnd with ReadBlock or Read, aren't I up against the same
problem?
2. "If the current method throws an OutOfMemoryException, the reader's
position in the underlying Stream is advanced by the number of characters the
method was able to read, but the characters already read into the internal
ReadLine buffer are discarded. Since the position of the reader in the stream
cannot be changed, the characters already read are unrecoverable, and can be
accessed only by reinitializing the StreamReader. If the initial position
within the stream is unknown or the stream does not support seeking, the
underlying Stream also needs to be reinitialized. To avoid such a situation
and produce robust code you should use the Read method and store the read
characters in a preallocated buffer"
I can get exceptions with Read, so the same worries apply. But this section
started with OutOfMemoryException, so maybe it is just talking about huge
streams that ought to be read in pieces. If so, why beat around the bushes
and why not just say so? If something else, then what?