I’ve got a code which populates a stream. After population, the stream’s length 1000 (for instance), while the returned string length from Stream.ReadString is 997 and the returned value from StreamReader.ReadToEnd() is an empty stream.
Here’s a code showing what I mean (obviously, this isn’t exactly my working code, but the issue is the same):
MemoryStream stream = MethodCreatingPopulatedStream(stream);
StreamReader reader = new StreamReader(stream);
if (stream.Length != reader.ReadToEnd().Length)
{
PostQuestionInStackOverFlow();
}
else if (!string.Equals(reader.ReadToEnd(), stream.ReadString()))
{
PostQuestionInStackOverFlow();
GetAnnoyedAtDotNet();
}
else
{
Smile();
}
What am I missing here?
P.S, Adding Stream.Flush anywhere made no change
The length of the string (characters) is not necessarily the same length as the stream (bytes). It depends entirely on the encoding and any other overhead associated with storing a string (such as storing its length).
As for your second test,
stream.ReadString()doesn’t even exist and would have to assume a certain encoding if it did.