[Mono-devel-list] BufferedStream patch

Gonzalo Paniagua Javier gonzalo at ximian.com
Sun May 11 13:15:56 EDT 2003


El dom, 11 de 05 de 2003 a las 15:55, Nick Drochak escribió:
> Hi.
> 
> There's a unit test failure for BufferedStream. It doesn't throw when
> constructing one from a stream that is already closed like it should.
> 
> Here is a proposed patch.  Is this OK to commit?

+
+           /* get the stream to throw an exception if it is closed */
+           stream.Seek (0, SeekOrigin.Current)

Mmm, I don't think this is ok. For example, NetworkStream are not
seekable.

I've attached a simple Stream derived class which i used to play with
this. Compile and run.

You'll see "CanRead called" and no exception thrown. Change CanRead to
return false and run it again. You'll see "CanWrite called" and no
exception thrown. So, if CanRead is false, CanWrite is called.

Change CanWrite to return false too. Wooot. Exception thrown.

So we have:

        CanRead	CanWrite Exception
           T       X       No
           F       T       No
           F       F       Yes
        
Now the exercise is simple: write an «if» statement that behaves like
this ;-).

-Gonzalo

-------------- next part --------------
using System;
using System.IO;

namespace Test
{
	public class MyStream : Stream
	{
		public MyStream ()
		{
			Console.WriteLine ("ctor called");
		}

		public override bool CanRead {
			get {
				Console.WriteLine ("CanRead called");
				return true;
			}
		}

                public override bool CanWrite {
                        get {
				Console.WriteLine ("CanWrite called");
				return true;
                        }
                }
		
		public override bool CanSeek {
                        get {
				Console.WriteLine ("CanSeek called");
				return true;
                        }
                }

		public override long Length {
			get {
				Console.WriteLine ("get_Length called");
				return 100;
			}
		}

		public override long Position {
			get {
				Console.WriteLine ("get_Position called");
				return 0;
			}
			set {
				Console.WriteLine ("set_Position called");
			}
		}

		public override int Read (byte[] dest, int dest_offset, int count)
		{
			Console.WriteLine ("Read called");
			return 0;
		}

		public override void Write (byte[] src, int src_offset, int count)
		{
			Console.WriteLine ("Write called");
		}

		public override long Seek (long offset, SeekOrigin origin)
		{
			Console.WriteLine ("Seek called");
			return 0;
		}

		public override void SetLength (long length)
		{
			Console.WriteLine ("SetLength called");
		}

		public override void Flush ()
		{
			Console.WriteLine ("Flush called");
		}

		static void Main ()
		{
			BufferedStream bs = new BufferedStream (new MyStream ());
		}
	}
}



More information about the Mono-devel-list mailing list