Bug in BinaryWriter

Z

Zachary Turner

I searched Usenet and found some posts with people having similar
problems, I'm wondering if any MVPs or anyone else knows if this is a
confirmed bug and if MS plans to do anything about it. First let me
quite the MSDN documentation:

public virtual void Write ( string value )

A length-prefixed string represents the string length by prefixing to
the string a single byte or word that contains the length of that
string. This method writes a length-prefixed string to this stream
using the BinaryWriter instance's current Encoding. -----This method
first writes the length of the string as a four-byte unsigned
integer-----, and then writes that many characters to the stream.

the section enclosed in ----- ----- indicates where the "bug" arises.
There's either a bug in the documentation or the function, one or the
other.

The following sample program should demonstrate:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace WriterTest
{
class Program
{
static void Main(string[] args)
{
using (MemoryStream MStream = new MemoryStream())
{
BinaryWriter Writer = new BinaryWriter(MStream);

string TestString = "This is a test string";
System.Console.WriteLine("The writer is at position {0}.",
Writer.BaseStream.Position);

Writer.Write(TestString);

System.Console.WriteLine("Position after
BinaryWriter.Write(String) - {0}, String length - {1}, # of bytes
for size - {2}.", Writer.BaseStream.Position, TestString.Length,
Writer.BaseStream.Position - (long)TestString.Length);
System.Console.ReadLine();
}
}
}
}

Here's the output.

The writer is at position 0.
Position after BinaryWriter.Write(String) - 22, String length - 21,
# of bytes for size - 1.
 
P

Patrice

I would say the doc. From my tests it looks like the prefix is using the
hihg bit to indicate if the length keeps using the following bytes or
something similar...
 
J

Jon Skeet [C# MVP]

Zachary Turner said:
I searched Usenet and found some posts with people having similar
problems, I'm wondering if any MVPs or anyone else knows if this is a
confirmed bug and if MS plans to do anything about it. First let me
quite the MSDN documentation:

public virtual void Write ( string value )

A length-prefixed string represents the string length by prefixing to
the string a single byte or word that contains the length of that
string. This method writes a length-prefixed string to this stream
using the BinaryWriter instance's current Encoding. -----This method
first writes the length of the string as a four-byte unsigned
integer-----, and then writes that many characters to the stream.

the section enclosed in ----- ----- indicates where the "bug" arises.
There's either a bug in the documentation or the function, one or the
other.

It's a bug in the docs. It's writes a "7 bit encoded" integer as per
the Write7BitEncodedInt method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top