Hi there,
Does anyone know a Microsoft email-adres for bug-reporting. I think I
found a bug in the StreamReader class:
Encoding dos = Encoding.GetEncoding(437);
StreamReader sr = new StreamReader(@"d:\test.in", dos);
StreamWriter sw = new StreamWriter(@"d:\test.out",false,dos);
string s = sr.ReadToEnd();
sw.WriteLine(s);
sw.Flush();
sw.Close();
sr.Close();
This should, in effect, duplicate the test.in file in the test.out
file. But, coincidentaly,
the test.in-file was a testfile of mine, containing 256 bytes 0xFF,
0xFE, 0xFD to 0x00.
And because 0xFF 0xFE is a unicode byte-order-mask (BOM), it thinks it
is an unicode file. This is obviously not the case, as I explicitly
provide a different encoding.
I think in this case, Microsoft should honor the encoding.
Just by using Lutz Roeder's excellent Reflector I found this bug; it
is actually in
System.IO.StreamReader.ReadBuffer():
[...]
if (!this.IsPreamble())
{
if (this._detectEncoding && (this.byteLen >= 2))
{
this.DetectEncoding();
}
this.charLen += this.decoder.GetChars(this.byteBuffer, 0,
this.byteLen, this.charBuffer, this.charLen);
}
[...]
The DetectEncoding() even fires when a encoding is supplied, which in
my opinion, shouldn't.
If anyone thinks I'm absolutely wrong, let me know:-)
Kind regards,
Edwin.
Doug Semler - 28 Sep 2007 19:14 GMT
> Hi there,
>
[quoted text clipped - 3 lines]
> Encoding dos = Encoding.GetEncoding(437);
> StreamReader sr = new StreamReader(@"d:\test.in", dos);
The constructor that you used for the reader should be:
StreamReader sr = new StreamReader(@"d:\test.in", dos, false);
See the remarks section of StreamReader(string, Encoding) constructor.
MSDN Documentation states that the constructor WITHOUT the detectEncoding
boolean will first try to detect the encoding THEN fall back to the user
defined encoding. The third boolean parameter overridedes this behavior.

Signature
Doug Semler, MCPD
a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
The answer is 42; DNRC o-
Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?
Edwin - 30 Sep 2007 14:12 GMT
Ah, how stupid of me... I really overlooked that.
Clearly a case of RTFM :-)
I really don't like that implicit intelligence Microsoft likes to
throw in, attempting to be smarter than the programmer by default :-S
Kind regards,
Edwin.