* BufferedStream.cs, FileStream.cs, MemoryStream.cs, Stream.cs:
[mono.git] / mcs / class / corlib / System.IO / BufferedStream.cs
index 12039be8b759737fea07e520da5d558338dd3ce9..ca46bce69c852b30e46772c59826ecc99e6da2e1 100644 (file)
@@ -3,8 +3,11 @@
 //
 // Author:
 //   Matt Kimball (matt@kimball.net)
+//   Ville Palo <vi64pa@kolumbus.fi>
 //
 
+using System.Runtime.InteropServices;
+
 namespace System.IO {
        public sealed class BufferedStream : Stream {
                Stream m_stream;
@@ -12,11 +15,18 @@ namespace System.IO {
                int m_buffer_pos;
                int m_buffer_read_ahead;
                bool m_buffer_reading;
+               private bool disposed = false;
 
                public BufferedStream(Stream stream) : this(stream, 4096) {
                }
 
                public BufferedStream(Stream stream, int buffer_size) {
+
+                       if (stream == null)
+                               throw new ArgumentNullException ("stream was null");
+                       if (buffer_size < 0)
+                               throw new ArgumentOutOfRangeException ();
+                                               
                        m_stream = stream;
                        m_buffer = new byte[buffer_size];
                }
@@ -40,13 +50,15 @@ namespace System.IO {
                }
 
                public override long Length {
-                       get {
+                       get {                           
+                               Flush ();
                                return m_stream.Length;
                        }
                }
                
                public override long Position {
                        get {
+                               CheckObjectDisposedException ();
                                return m_stream.Position - m_buffer_read_ahead + m_buffer_pos;
                        }
 
@@ -57,16 +69,23 @@ namespace System.IO {
                }
 
                public override void Close() {
-                       Flush();
+                       
+                       if (m_buffer != null)
+                               Flush();
+
                        m_stream.Close();
-                       m_stream = null;
                        m_buffer = null;
+                       disposed = true;
                }
 
                public override void Flush() {
+                       
+                       CheckObjectDisposedException ();
+
                        if (m_buffer_reading) {
-                               m_stream.Position = Position;
-                       } else {
+                               if (CanSeek)
+                                       m_stream.Position = Position;
+                       } else if (m_buffer_pos > 0) {
                                m_stream.Write(m_buffer, 0, m_buffer_pos);
                        }
 
@@ -84,6 +103,9 @@ namespace System.IO {
                }
 
                public override int ReadByte() {
+
+                       CheckObjectDisposedException ();
+                       
                        byte[] b = new byte[1];
 
                        if (Read(b, 0, 1) == 1) {
@@ -94,13 +116,23 @@ namespace System.IO {
                }
 
                public override void WriteByte(byte value) {
+
+                       CheckObjectDisposedException ();
                        byte[] b = new byte[1];
 
                        b[0] = value;
                        Write(b, 0, 1);
                }
 
-               public override int Read(byte[] array, int offset, int count) {
+               public override int Read([In,Out] byte[] array, int offset, int count) {
+
+                       CheckObjectDisposedException ();
+
+                       if (array.Length < offset + count)
+                               throw new ArgumentException ();
+                       if (offset < 0)
+                               throw new ArgumentOutOfRangeException ("Offset was negative value.");
+
                        if (!m_buffer_reading) {
                                Flush();
                                m_buffer_reading = true;
@@ -145,6 +177,14 @@ namespace System.IO {
                }
 
                public override void Write(byte[] array, int offset, int count) {
+
+                       CheckObjectDisposedException ();
+
+                       if (!m_stream.CanRead)
+                               throw new NotSupportedException ();
+                       if (offset < 0)
+                               throw new ArgumentOutOfRangeException ();
+
                        if (m_buffer_reading) {
                                Flush();
                                m_buffer_reading = false;
@@ -158,5 +198,11 @@ namespace System.IO {
                                m_buffer_pos += count;
                        }
                }
+
+               private void CheckObjectDisposedException () 
+               {
+                       if (disposed) 
+                               throw new ObjectDisposedException ("BufferedStream", "Stream is closed");
+               }                       
        }
 }