2008-05-14 Andreas Nahr <ClassDevelopment@A-SoftTech.com>
[mono.git] / mcs / class / corlib / System.IO / TextReader.cs
1 //\r
2 // System.IO.TextReader\r
3 //\r
4 // Authors:\r
5 //   Marcin Szczepanski (marcins@zipworld.com.au)\r
6 //   Miguel de Icaza (miguel@gnome.org)\r
7 //\r
8 \r
9 //\r
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)\r
11 //\r
12 // Permission is hereby granted, free of charge, to any person obtaining\r
13 // a copy of this software and associated documentation files (the\r
14 // "Software"), to deal in the Software without restriction, including\r
15 // without limitation the rights to use, copy, modify, merge, publish,\r
16 // distribute, sublicense, and/or sell copies of the Software, and to\r
17 // permit persons to whom the Software is furnished to do so, subject to\r
18 // the following conditions:\r
19 // \r
20 // The above copyright notice and this permission notice shall be\r
21 // included in all copies or substantial portions of the Software.\r
22 // \r
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
30 //\r
31 \r
32 using System;\r
33 using System.Runtime.InteropServices;\r
34 \r
35 namespace System.IO {\r
36 \r
37         [Serializable]\r
38 #if NET_2_0\r
39         [ComVisible (true)]\r
40 #endif\r
41         public abstract class TextReader : MarshalByRefObject, IDisposable {\r
42                 \r
43                 protected TextReader() { }\r
44                 \r
45                 public static readonly TextReader Null;\r
46                 \r
47                 public virtual void Close()\r
48                 { \r
49                         Dispose(true);\r
50                 }\r
51 \r
52 #if NET_2_0\r
53                 public void Dispose ()\r
54 #else\r
55                 void System.IDisposable.Dispose()\r
56 #endif                  \r
57                 {\r
58                         Dispose(true);\r
59                 }\r
60 \r
61                 protected virtual void Dispose (bool disposing)\r
62                 {\r
63                         if (disposing){\r
64                                 // If we are explicitly disposed, we can avoid finalization.\r
65                                 GC.SuppressFinalize (this);\r
66                         }\r
67                         return;\r
68                 }\r
69                 \r
70                 public virtual int Peek()\r
71                 {\r
72                         return -1;\r
73                 }\r
74                 \r
75                 public virtual int Read()\r
76                 {\r
77                         return -1;\r
78                 }\r
79                 \r
80 \r
81                 public virtual int Read ([In, Out] char[] buffer, int index, int count)\r
82                 {\r
83                         int c, i;\r
84                         \r
85                         for (i = 0; i < count; i++) {\r
86                                 if ((c = Read ()) == -1)\r
87                                         return i;\r
88                                 buffer [index + i] = (char)c;\r
89                         }\r
90                         \r
91                         return i;\r
92                 }\r
93                 \r
94                 public virtual int ReadBlock ([In, Out] char [] buffer, int index, int count)\r
95                 {\r
96                         int total_read_count = 0;\r
97                         int current_read_count = 0;\r
98 \r
99                         do {\r
100                                 current_read_count = Read (buffer, index, count);\r
101                                 index += current_read_count;\r
102                                 total_read_count += current_read_count;\r
103                                 count -= current_read_count;\r
104                         } while (current_read_count != 0 && count > 0);\r
105 \r
106                         return total_read_count;\r
107                 }\r
108 \r
109                 public virtual string ReadLine()\r
110                 { \r
111                         return String.Empty;\r
112                 }\r
113 \r
114                 public virtual string ReadToEnd()\r
115                 { \r
116                         return String.Empty;\r
117                 }\r
118 \r
119                 public static TextReader Synchronized (TextReader reader)\r
120                 {\r
121                         if (reader == null)\r
122                                 throw new ArgumentNullException ("reader is null");\r
123                         if (reader is SynchronizedReader)\r
124                                 return reader;\r
125 \r
126                         return new SynchronizedReader (reader);\r
127                 }       \r
128         }\r
129 \r
130         //\r
131         // Synchronized Reader implementation, used internally.\r
132         //\r
133         [Serializable]\r
134         internal class SynchronizedReader : TextReader {\r
135                 TextReader reader;\r
136                 \r
137                 public SynchronizedReader (TextReader reader)\r
138                 {\r
139                         this.reader = reader;\r
140                 }\r
141 \r
142                 public override void Close ()\r
143                 {\r
144                         lock (this){\r
145                                 reader.Close ();\r
146                         }\r
147                 }\r
148 \r
149                 public override int Peek ()\r
150                 {\r
151                         lock (this){\r
152                                 return reader.Peek ();\r
153                         }\r
154                 }\r
155 \r
156                 public override int ReadBlock (char [] buffer, int index, int count)\r
157                 {\r
158                         lock (this){\r
159                                 return reader.ReadBlock (buffer, index, count);\r
160                         }\r
161                 }\r
162 \r
163                 public override string ReadLine ()\r
164                 {\r
165                         lock (this){\r
166                                 return reader.ReadLine ();\r
167                         }\r
168                 }\r
169 \r
170                 public override string ReadToEnd ()\r
171                 {\r
172                         lock (this){\r
173                                 return reader.ReadToEnd ();\r
174                         }\r
175                 }\r
176 #region Read Methods\r
177                 public override int Read ()\r
178                 {\r
179                         lock (this){\r
180                                 return reader.Read ();\r
181                         }\r
182                 }\r
183 \r
184                 public override int Read (char [] buffer, int index, int count)\r
185                 {\r
186                         lock (this){\r
187                                 return reader.Read (buffer, index, count);\r
188                         }\r
189                 }\r
190 #endregion\r
191                 \r
192         }\r
193 }\r