Pārlūkot izejas kodu

2003-04-19 Ville Palo <[email protected]>

	* BufferedStream.cs: Some fixes, mostly throwing exceptions.
	* MemoryStream.cs: Changed the order of exception checking
	* StringReader.cs: little clean up

svn path=/trunk/mcs/; revision=13798
Ville Palo 23 gadi atpakaļ
vecāks
revīzija
f40f59cdc5

+ 45 - 2
mcs/class/corlib/System.IO/BufferedStream.cs

@@ -3,8 +3,10 @@
 //
 // Author:
 //   Matt Kimball ([email protected])
+//   Ville Palo <[email protected]>
 //
 
+
 namespace System.IO {
 	public sealed class BufferedStream : Stream {
 		Stream m_stream;
@@ -12,11 +14,23 @@ 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 ();
+			
+			// if stream is closed this throws an exception FIXME: better way?
+			{
+			long l = stream.Position;
+			}
+			
 			m_stream = stream;
 			m_buffer = new byte[buffer_size];
 		}
@@ -40,7 +54,8 @@ namespace System.IO {
 		}
 
 		public override long Length {
-			get {
+			get {				
+				Flush ();
 				return m_stream.Length;
 			}
 		}
@@ -59,11 +74,12 @@ namespace System.IO {
 		public override void Close() {
 			Flush();
 			m_stream.Close();
-			m_stream = null;
 			m_buffer = null;
+			disposed = true;
 		}
 
 		public override void Flush() {
+			
 			if (m_buffer_reading) {
 				if (CanSeek)
 					m_stream.Position = Position;
@@ -85,6 +101,9 @@ namespace System.IO {
 		}
 
 		public override int ReadByte() {
+
+			CheckObjectDisposedException ();
+			
 			byte[] b = new byte[1];
 
 			if (Read(b, 0, 1) == 1) {
@@ -95,6 +114,8 @@ namespace System.IO {
 		}
 
 		public override void WriteByte(byte value) {
+
+			CheckObjectDisposedException ();
 			byte[] b = new byte[1];
 
 			b[0] = value;
@@ -102,6 +123,14 @@ namespace System.IO {
 		}
 
 		public override int Read(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;
@@ -146,6 +175,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;
@@ -159,5 +196,11 @@ namespace System.IO {
 				m_buffer_pos += count;
 			}
 		}
+
+		private void CheckObjectDisposedException () 
+		{
+			if (disposed) 
+				throw new ObjectDisposedException ("BufferedStream", "Stream is closed");
+		}			
 	}
 }

+ 6 - 0
mcs/class/corlib/System.IO/ChangeLog

@@ -1,3 +1,9 @@
+2003-04-19  Ville Palo <[email protected]>
+
+	* BufferedStream.cs: Some fixes, mostly throwing exceptions.
+	* MemoryStream.cs: Changed the order of exception checking
+	* StringReader.cs: little clean up
+	
 2003-04-14  Ville Palo <[email protected]>
 
 	* BinaryWriter.cs: Fixed decimal writing and lots of 

+ 2 - 1
mcs/class/corlib/System.IO/MemoryStream.cs

@@ -284,10 +284,11 @@ namespace System.IO
 
 		public override void SetLength (long value)
 		{
-			CheckIfClosedThrowDisposed ();
 			if (!expandable && value > capacity)
 				throw new NotSupportedException ("Expanding this MemoryStream is not supported");
 
+			CheckIfClosedThrowDisposed ();
+
 			if (!canWrite)
 				throw new IOException ("Cannot write to this MemoryStream");
 

+ 9 - 9
mcs/class/corlib/System.IO/StringReader.cs

@@ -42,8 +42,7 @@ namespace System.IO {
 
 		public override int Peek() {
 
-			if (disposed) 
-				throw new ObjectDisposedException ("StringReader", "Cannot read from a closed StringReader");
+			CheckObjectDisposedException ();
 
 			if( nextChar >= sourceLength ) {
 				return -1;
@@ -54,8 +53,7 @@ namespace System.IO {
 
 		public override int Read() {
 
-			if (disposed) 
-				throw new ObjectDisposedException ("StringReader", "Cannot read from a closed StringReader");
+			CheckObjectDisposedException ();
 
 			if( nextChar >= sourceLength ) {
 				return -1;
@@ -72,8 +70,7 @@ namespace System.IO {
 
 		public override int Read( char[] buffer, int index, int count ) {
 
-			if (disposed) 
-				throw new ObjectDisposedException ("StringReader", "Cannot read from a closed StringReader");
+			CheckObjectDisposedException ();
 
 			if( buffer == null ) {
 				throw new ArgumentNullException();
@@ -145,13 +142,16 @@ namespace System.IO {
 
                 public override string ReadToEnd() {
 
-			if (disposed) 
-				throw new ObjectDisposedException ("StringReader", "Cannot read from a closed StringReader");
-
+			CheckObjectDisposedException ();
                         string toEnd = source.Substring( nextChar, sourceLength - nextChar );
                         nextChar = sourceLength;
                         return toEnd;
                 }
 
+		private void CheckObjectDisposedException ()
+		{
+			if (disposed) 
+				throw new ObjectDisposedException ("StringReader", "Cannot read from a closed StringReader");
+		}
 	}
 }