[Mono-list] Re: BinaryReader class

Dick Porter dick@ximian.com
16 Aug 2002 07:58:51 +0100


--=-5Wex+0tOmjcSpmr0E4RV
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Thu, 2002-08-15 at 18:00, Dick Porter wrote:
> On Thu, 2002-08-15 at 15:36, Matt Kimball wrote:
> > I think I got the idea that it was buffered from the presence of a
> > "FillBuffer" method documented in the API docs, but it isn't clear whether the
> > class always buffers some, and the buffer size is just controlled by
> > "FillBuffer" or if it only buffers when you call "FillBuffer" before reading. 
> > Perhaps it would work better if you do the latter.  What does MS's .NET
> > implementation do?
> 
> I'll investigate.

Just in case anyone else cares, it seems that calling FillBuffer(n) has
the effect of seeking forward n bytes (test source attached).  The data
file is a BinaryWriter-written list of int32 (4321, 1234, 5678, 9876,
23). This was compiled with csc and executed with the .net runtime:   

$ ./buftest.exe
Testing BinaryReader buffering, FillBuffer calls enabled
Read int 4321
Stream pos 4
Filling buffer with one char
Stream pos 5
Read int 771751940
Stream pos 9
Filling buffer with one char
Stream pos 10
Read int 647233536
Stream pos 14
Test done
Testing BinaryReader buffering, FillBuffer calls disabled
Read int 4321
Stream pos 4
Read int 1234
Stream pos 8
Read int 5678
Stream pos 12
Test done



I took a look at the pnet source to see what their interpretation of the
docs is, and they use FillBuffer() to read the next n bytes from the
stream into a buffer, starting at index 0.  The various ReadFoo()
methods then call FillBuffer() to read the exact number of bytes
required for each data type and do their data conversions on that
buffer.

- Dick


--=-5Wex+0tOmjcSpmr0E4RV
Content-Disposition: attachment; filename=buftest.cs
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; name=buftest.cs; charset=ISO-8859-1


using System;
using System.IO;
using System.Text;

public class Test : BinaryReader {
	public Test(Stream input, Encoding encoding) : base(input, encoding) {}

	public static int DoRead (bool fill) {
		FileStream instr=3Dnew FileStream("bufferwriter.data", FileMode.Open);
		Test reader=3Dnew Test(instr, Encoding.UTF8);

		int i;

		if(fill=3D=3Dtrue) {
			Console.WriteLine("Testing BinaryReader buffering, FillBuffer calls enab=
led");
		} else {
			Console.WriteLine("Testing BinaryReader buffering, FillBuffer calls disa=
bled");
		}

		i=3Dreader.ReadInt32();
		Console.WriteLine("Read int \"{0}\"", i);
		Console.WriteLine("Stream pos {0}", reader.BaseStream.Position);

		if(fill=3D=3Dtrue) {
			Console.WriteLine("Filling buffer with one char");
			reader.FillBuffer(1);
			Console.WriteLine("Stream pos {0}", reader.BaseStream.Position);
		}

		i=3Dreader.ReadInt32();
		Console.WriteLine("Read int {0}", i);
		Console.WriteLine("Stream pos {0}", reader.BaseStream.Position);

		if(fill=3D=3Dtrue) {
			Console.WriteLine("Filling buffer with one char");
			reader.FillBuffer(1);
			Console.WriteLine("Stream pos {0}", reader.BaseStream.Position);
		}

		i=3Dreader.ReadInt32();
		Console.WriteLine("Read int \"{0}\"", i);
		Console.WriteLine("Stream pos {0}", reader.BaseStream.Position);

		reader.Close();

		Console.WriteLine("Test done");

		return 0;
	}

	public static void Main () {
		DoRead(true);
		DoRead(false);
	}
}


--=-5Wex+0tOmjcSpmr0E4RV--