Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / September 2007

Tip: Looking for answers? Try searching our database.

Bug in StreamReader class?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Edwin - 26 Sep 2007 15:53 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.