public static bool WriteBeyondEndTest(Stream s)
{
Console.WriteLine("Write Beyond End test on "+s.GetType().Name);
FileStream fs = s as FileStream;
if (fs != null)
Console.WriteLine("FileStream type is: "+(fs.IsAsync ? "asynchronous" : "synchronous"));
long origLength = s.Length;
byte[] bytes = new byte[10];
for(int i=0; i<bytes.Length; i++)
bytes[i] = (byte) i;
int spanPastEnd = 5;
s.Seek(spanPastEnd, SeekOrigin.End);
if (s.Position != s.Length + spanPastEnd)
throw new Exception("Position is incorrect! Seek(5, SeekOrigin.End) should leave us at s.Length + spanPastEnd ("+(s.Length + spanPastEnd)+"), but got: "+s.Position);
Console.WriteLine("Original Length: "+origLength);
s.Write(bytes, 0, bytes.Length);
long pos = s.Position;
if (pos != origLength + spanPastEnd + bytes.Length)
throw new Exception(String.Format("After asynchronously writing beyond end of the stream, position is now incorrect! origLength: {0} pos: {1}", origLength, pos));
if (s.Length != origLength + spanPastEnd + bytes.Length)
throw new Exception(String.Format("After asynchronously writing beyond end of the stream, Length is now incorrect! origLength: {0} pos: {1}", origLength, pos));
WritePastEndHelper(s, bytes, origLength, spanPastEnd, false);
origLength = s.Length;
s.Position = s.Length + spanPastEnd;
s.WriteByte(0x42);
long expected = origLength + spanPastEnd + 1;
if (s.Position != expected)
{
iCountErrors++ ;
throw new Exception("After WriteByte, Position was wrong! got: "+s.Position+" expected: "+expected);
}
if (s.Length != expected)
{
iCountErrors++ ;
throw new Exception("After WriteByte, Length was wrong! got: "+s.Length+" expected: "+expected);
}
origLength = s.Length;
s.Position = s.Length + spanPastEnd;
IAsyncResult ar = s.BeginWrite(bytes, 0, bytes.Length, null, null);
s.EndWrite(ar);
pos = s.Position;
if (pos != origLength + spanPastEnd + bytes.Length)
{
iCountErrors++ ;
throw new Exception(String.Format("After writing beyond end of the stream, position is now incorrect! origLength: {0} pos: {1}", origLength, pos));
}
if (s.Length != origLength + spanPastEnd + bytes.Length)
{
iCountErrors++;
throw new Exception(String.Format("After writing beyond end of the stream, Length is now incorrect! origLength: {0} pos: {1}", origLength, pos));
}
WritePastEndHelper(s, bytes, origLength, spanPastEnd, true);
return true;
}
private static bool WritePastEndHelper(Stream s, byte[] bytes, long origLength, int spanPastEnd, bool afterAsync)