2004-07-05 Dick Porter <dick@ximian.com>
[mono.git] / mcs / class / corlib / System.IO / TextReader.cs
index d6ac7b70ad5412c2b52b7d8248f0b46ecd727726..0ca3fd5574e4e3bd282f96f69bdbfcd314659833 100644 (file)
@@ -1,16 +1,40 @@
 //\r
 // System.IO.TextReader\r
 //\r
-// Author: Marcin Szczepanski (marcins@zipworld.com.au)\r
-//\r
-// TODO: Implement the Thread Safe stuff\r
+// Authors:\r
+//   Marcin Szczepanski (marcins@zipworld.com.au)\r
+//   Miguel de Icaza (miguel@gnome.org)\r
 //\r
+
+//
+// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
 \r
 using System;\r
+using System.Runtime.InteropServices;\r
 \r
 namespace System.IO {\r
 \r
-       [MonoTODO]\r
+       [Serializable]\r
        public abstract class TextReader : MarshalByRefObject, IDisposable {\r
                \r
                protected TextReader() { }\r
@@ -42,11 +66,8 @@ namespace System.IO {
                        return -1;\r
                }\r
                \r
-               // LAMESPEC:  The Beta2 docs say this should be Read( out char[] ...\r
-               // whereas the MS implementation is just Read( char[] ... )\r
-               // Not sure which one is right, we'll see in Beta3 :)\r
-\r
-               public virtual int Read (char[] buffer, int index, int count)\r
+
+               public virtual int Read ([In, Out] char[] buffer, int index, int count)\r
                {\r
                        int c, i;\r
                        \r
@@ -59,9 +80,19 @@ namespace System.IO {
                        return i;\r
                }\r
                \r
-               public virtual int ReadBlock( char[] buffer, int index, int count )\r
-               { \r
-                       return 0;\r
+               public virtual int ReadBlock ([In, Out] char [] buffer, int index, int count)\r
+               {\r
+                       int total_read_count = 0;\r
+                        int current_read_count = 0;\r
+\r
+                       do {\r
+                               current_read_count = Read (buffer, index, count);\r
+                               index += current_read_count;\r
+                               total_read_count += current_read_count;\r
+                               count -= current_read_count;\r
+                       } while (current_read_count != 0 && count > 0);\r
+\r
+                       return total_read_count;\r
                }\r
 \r
                public virtual string ReadLine()\r
@@ -74,11 +105,78 @@ namespace System.IO {
                        return String.Empty;\r
                }\r
 \r
-               [MonoTODO]\r
-               public static TextReader Synchronised( TextReader reader )\r
+               public static TextReader Synchronized (TextReader reader)\r
                {\r
-                        // TODO: Implement\r
-                       return Null;\r
+                       if (reader == null)\r
+                               throw new ArgumentNullException ("reader is null");\r
+                       if (reader is SynchronizedReader)\r
+                               return reader;\r
+\r
+                       return new SynchronizedReader (reader);\r
                }       \r
        }\r
+\r
+       //\r
+       // Synchronized Reader implementation, used internally.\r
+       //\r
+       [Serializable]\r
+       internal class SynchronizedReader : TextReader {\r
+               TextReader reader;\r
+               \r
+               public SynchronizedReader (TextReader reader)\r
+               {\r
+                       this.reader = reader;\r
+               }\r
+\r
+               public override void Close ()\r
+               {\r
+                       lock (this){\r
+                               reader.Close ();\r
+                       }\r
+               }\r
+\r
+               public override int Peek ()\r
+               {\r
+                       lock (this){\r
+                               return reader.Peek ();\r
+                       }\r
+               }\r
+\r
+               public override int ReadBlock (char [] buffer, int index, int count)\r
+               {\r
+                       lock (this){\r
+                               return reader.ReadBlock (buffer, index, count);\r
+                       }\r
+               }\r
+\r
+               public override string ReadLine ()\r
+               {\r
+                       lock (this){\r
+                               return reader.ReadLine ();\r
+                       }\r
+               }\r
+\r
+               public override string ReadToEnd ()\r
+               {\r
+                       lock (this){\r
+                               return reader.ReadToEnd ();\r
+                       }\r
+               }\r
+#region Read Methods\r
+               public override int Read ()\r
+               {\r
+                       lock (this){\r
+                               return reader.Read ();\r
+                       }\r
+               }\r
+\r
+               public override int Read (char [] buffer, int index, int count)\r
+               {\r
+                       lock (this){\r
+                               return reader.Read (buffer, index, count);\r
+                       }\r
+               }\r
+#endregion\r
+               \r
+       }\r
 }\r