[Mono-list] FileStream weirdness

Dan Lewis dihlewis@yahoo.co.uk
Mon, 11 Mar 2002 23:15:31 +0000 (GMT)


 --- Mark Lewis wrote:
> I spent a couple of hours mucking with your test code, trying different
> things.  The code looks really simple, and I rewrote it to make sure it
> wasn't a fault in the test program.  I think you've found a bug in the MS
> VM.

Thanks, Mark! I really appreciate the second opinion :-)

After further investigation I have to agree -- it looks like a bug in the
Microsoft FileStream implementation (could be rather nasty for some random
access applications).

I've sent them the following test case which kind of distills the problem. It
looks like it's something to do with the buffer management (which funnily
enough was what I was trying to test about my implementation in the first place
:)

Dan.




using System;
using System.IO;

public class FileStreamTest {
	private static readonly int BufferSize = 256;
	private static readonly int Offset = 5;

	public static void Main (string [] args) {
		FileStream file;
		string filename = Path.GetTempFileName ();
		byte [] data = new byte [BufferSize * 10];

		// 1. create file
		
		file = new FileStream (filename, FileMode.Create);
		new Random (12345).NextBytes (data);
		file.Write (data, 0, data.Length);
		file.Close ();

		// 2. direct seek

		Console.WriteLine ("Direct seek:");
		file = new FileStream (filename, FileMode.Open, FileAccess.Read,
FileShare.Read, BufferSize);

		file.Seek (Offset + 1, SeekOrigin.Begin);
		for (int i = 0; i < 10; ++ i)
			Console.Write ("{0:x2} ", file.ReadByte ());
		Console.WriteLine ();

		file.Close ();

		// 3. straddle buffer, then seek

		Console.WriteLine ("Buffer straddle, then seek:");
		file = new FileStream (filename, FileMode.Open, FileAccess.Read,
FileShare.Read, BufferSize);

		file.Read (data, 0, Offset);
		file.Read (data, 0, BufferSize);
		
		file.Seek (Offset + 1, SeekOrigin.Begin);
		for (int i = 0; i < 10; ++ i)
			Console.Write ("{0:x2} ", file.ReadByte ());
		Console.WriteLine ();

		file.Close ();
	}
}


__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com