FileStream bug?

J

Jon Payne

I'm finding that in certain cases I'm getting inconsistent results
from FileStream.Read after a calling Seek. The problem only occurs
after certain patterns of Read and Seek. Calling Flush before Read
removes the problem but I cannot find anything in the documentation to
indicate this is required and I'm concerned that this could cause
performance problems. I am using the Compact Framework version
2.0.7045.0.

Can anyone confirm if this is a bug or if I am doing something wrong?

The following code and sample output demonstrates the problem:

using System;
using System.IO;

static class Program
{
static void Log(string t)
{
System.Diagnostics.Debug.WriteLine(t);
}

static void Main()
{
// create a test file
using (FileStream stream = new FileStream(@"\test.dat",
FileMode.Create))
{
for (int i = 0; i < 1000; ++i)
{
stream.WriteByte((byte)(i % 256));
}
}

// demonstrate issue
using
(
FileStream stream = new FileStream(
@"\test.dat", FileMode.Open,
FileAccess.Read, FileShare.Read, 128
)
)
{
byte[] buffer = new byte[500];

stream.Read(buffer, 0, 252);
stream.Read(buffer, 0, 102);
stream.Read(buffer, 0, 5);
stream.Read(buffer, 0, 63);
stream.Read(buffer, 0, 39);
stream.Read(buffer, 0, 34);
stream.Read(buffer, 0, 73);
stream.Read(buffer, 0, 73);
stream.Read(buffer, 0, 73);
stream.Read(buffer, 0, 73);

long pos = stream.Position;

Log(string.Format("At {0}", stream.Position));
stream.Read(buffer, 0, 73);
Log(string.Format("Read {0}, {1}, {2}...", buffer[0], buffer[1],
buffer[2]));

Log(string.Format("Seek to {0}", pos));
stream.Seek(pos, SeekOrigin.Begin);

Log(string.Format("At {0}", stream.Position));
stream.Read(buffer, 0, 73);
Log(string.Format("Read {0}, {1}, {2}...", buffer[0], buffer[1],
buffer[2]));
}
}
}

Sample output:

At 787
Read 19, 20, 21...
Seek to 787
At 787
Read 1, 2, 3...

Regards,

Jon Payne
 
B

Brent Pipal

Better late than never...

I discovered this bug last month and it is fixed in the Jan 2010 rollup.

Essentially this is a problem with the internal .NetCF seek pointer getting out of sync with the OS seek pointer.

You can find the fix described at

http://www.microsoft.com/downloads/...FamilyID=0a003b66-5c00-43c2-8658-5453be22c402

and downloadable at
http://www.microsoft.com/downloads/...FamilyID=0a003b66-5c00-43c2-8658-5453be22c402

Thanks
-Brent





Jon Payne wrote:

FileStream bug?
07-Dec-07

I'm finding that in certain cases I'm getting inconsistent results
from FileStream.Read after a calling Seek. The problem only occurs
after certain patterns of Read and Seek. Calling Flush before Read
removes the problem but I cannot find anything in the documentation to
indicate this is required and I'm concerned that this could cause
performance problems. I am using the Compact Framework version
2.0.7045.0.

Can anyone confirm if this is a bug or if I am doing something wrong?

The following code and sample output demonstrates the problem:

using System;
using System.IO;

static class Program
{
static void Log(string t)
{
System.Diagnostics.Debug.WriteLine(t);
}

static void Main()
{
// create a test file
using (FileStream stream = new FileStream(@"\test.dat",
FileMode.Create))
{
for (int i = 0; i < 1000; ++i)
{
stream.WriteByte((byte)(i % 256));
}
}

// demonstrate issue
using
(
FileStream stream = new FileStream(
@"\test.dat", FileMode.Open,
FileAccess.Read, FileShare.Read, 128
)
)
{
byte[] buffer = new byte[500];

stream.Read(buffer, 0, 252);
stream.Read(buffer, 0, 102);
stream.Read(buffer, 0, 5);
stream.Read(buffer, 0, 63);
stream.Read(buffer, 0, 39);
stream.Read(buffer, 0, 34);
stream.Read(buffer, 0, 73);
stream.Read(buffer, 0, 73);
stream.Read(buffer, 0, 73);
stream.Read(buffer, 0, 73);

long pos = stream.Position;

Log(string.Format("At {0}", stream.Position));
stream.Read(buffer, 0, 73);
Log(string.Format("Read {0}, {1}, {2}...", buffer[0], buffer[1],
buffer[2]));

Log(string.Format("Seek to {0}", pos));
stream.Seek(pos, SeekOrigin.Begin);

Log(string.Format("At {0}", stream.Position));
stream.Read(buffer, 0, 73);
Log(string.Format("Read {0}, {1}, {2}...", buffer[0], buffer[1],
buffer[2]));
}
}
}

Sample output:

At 787
Read 19, 20, 21...
Seek to 787
At 787
Read 1, 2, 3...

Regards,

Jon Payne

Previous Posts In This Thread:

FileStream bug?
I'm finding that in certain cases I'm getting inconsistent results
from FileStream.Read after a calling Seek. The problem only occurs
after certain patterns of Read and Seek. Calling Flush before Read
removes the problem but I cannot find anything in the documentation to
indicate this is required and I'm concerned that this could cause
performance problems. I am using the Compact Framework version
2.0.7045.0.

Can anyone confirm if this is a bug or if I am doing something wrong?

The following code and sample output demonstrates the problem:

using System;
using System.IO;

static class Program
{
static void Log(string t)
{
System.Diagnostics.Debug.WriteLine(t);
}

static void Main()
{
// create a test file
using (FileStream stream = new FileStream(@"\test.dat",
FileMode.Create))
{
for (int i = 0; i < 1000; ++i)
{
stream.WriteByte((byte)(i % 256));
}
}

// demonstrate issue
using
(
FileStream stream = new FileStream(
@"\test.dat", FileMode.Open,
FileAccess.Read, FileShare.Read, 128
)
)
{
byte[] buffer = new byte[500];

stream.Read(buffer, 0, 252);
stream.Read(buffer, 0, 102);
stream.Read(buffer, 0, 5);
stream.Read(buffer, 0, 63);
stream.Read(buffer, 0, 39);
stream.Read(buffer, 0, 34);
stream.Read(buffer, 0, 73);
stream.Read(buffer, 0, 73);
stream.Read(buffer, 0, 73);
stream.Read(buffer, 0, 73);

long pos = stream.Position;

Log(string.Format("At {0}", stream.Position));
stream.Read(buffer, 0, 73);
Log(string.Format("Read {0}, {1}, {2}...", buffer[0], buffer[1],
buffer[2]));

Log(string.Format("Seek to {0}", pos));
stream.Seek(pos, SeekOrigin.Begin);

Log(string.Format("At {0}", stream.Position));
stream.Read(buffer, 0, 73);
Log(string.Format("Read {0}, {1}, {2}...", buffer[0], buffer[1],
buffer[2]));
}
}
}

Sample output:

At 787
Read 19, 20, 21...
Seek to 787
At 787
Read 1, 2, 3...

Regards,

Jon Payne


Submitted via EggHeadCafe - Software Developer Portal of Choice
Adding WCF Service References
http://www.eggheadcafe.com/tutorial...9-dfa51a9fab8e/adding-wcf-service-refere.aspx
 

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