2008-09-14 Zoltan Varga <vargaz@gmail.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
41 #if NET_2_1
42         public abstract class TextReader : IDisposable {\r
43 #else\r
44         public abstract class TextReader : MarshalByRefObject, IDisposable {\r
45 #endif\r
46                 protected TextReader() { }\r
47                 \r
48                 public static readonly TextReader Null;\r
49                 \r
50                 public virtual void Close()\r
51                 { \r
52                         Dispose(true);\r
53                 }\r
54 \r
55 #if NET_2_0\r
56                 public void Dispose ()\r
57 #else\r
58                 void System.IDisposable.Dispose()\r
59 #endif                  \r
60                 {\r
61                         Dispose(true);\r
62                 }\r
63 \r
64                 protected virtual void Dispose (bool disposing)\r
65                 {\r
66                         if (disposing){\r
67                                 // If we are explicitly disposed, we can avoid finalization.\r
68                                 GC.SuppressFinalize (this);\r
69                         }\r
70                         return;\r
71                 }\r
72                 \r
73                 public virtual int Peek()\r
74                 {\r
75                         return -1;\r
76                 }\r
77                 \r
78                 public virtual int Read()\r
79                 {\r
80                         return -1;\r
81                 }\r
82                 \r
83 \r
84                 public virtual int Read ([In, Out] char[] buffer, int index, int count)\r
85                 {\r
86                         int c, i;\r
87                         \r
88                         for (i = 0; i < count; i++) {\r
89                                 if ((c = Read ()) == -1)\r
90                                         return i;\r
91                                 buffer [index + i] = (char)c;\r
92                         }\r
93                         \r
94                         return i;\r
95                 }\r
96                 \r
97                 public virtual int ReadBlock ([In, Out] char [] buffer, int index, int count)\r
98                 {\r
99                         int total_read_count = 0;\r
100                         int current_read_count = 0;\r
101 \r
102                         do {\r
103                                 current_read_count = Read (buffer, index, count);\r
104                                 index += current_read_count;\r
105                                 total_read_count += current_read_count;\r
106                                 count -= current_read_count;\r
107                         } while (current_read_count != 0 && count > 0);\r
108 \r
109                         return total_read_count;\r
110                 }\r
111 \r
112                 public virtual string ReadLine()\r
113                 { \r
114                         return String.Empty;\r
115                 }\r
116 \r
117                 public virtual string ReadToEnd()\r
118                 { \r
119                         return String.Empty;\r
120                 }\r
121 \r
122                 public static TextReader Synchronized (TextReader reader)\r
123                 {\r
124                         if (reader == null)\r
125                                 throw new ArgumentNullException ("reader is null");\r
126                         if (reader is SynchronizedReader)\r
127                                 return reader;\r
128 \r
129                         return new SynchronizedReader (reader);\r
130                 }       \r
131         }\r
132 \r
133         //\r
134         // Synchronized Reader implementation, used internally.\r
135         //\r
136         [Serializable]\r
137         internal class SynchronizedReader : TextReader {\r
138                 TextReader reader;\r
139                 \r
140                 public SynchronizedReader (TextReader reader)\r
141                 {\r
142                         this.reader = reader;\r
143                 }\r
144 \r
145                 public override void Close ()\r
146                 {\r
147                         lock (this){\r
148                                 reader.Close ();\r
149                         }\r
150                 }\r
151 \r
152                 public override int Peek ()\r
153                 {\r
154                         lock (this){\r
155                                 return reader.Peek ();\r
156                         }\r
157                 }\r
158 \r
159                 public override int ReadBlock (char [] buffer, int index, int count)\r
160                 {\r
161                         lock (this){\r
162                                 return reader.ReadBlock (buffer, index, count);\r
163                         }\r
164                 }\r
165 \r
166                 public override string ReadLine ()\r
167                 {\r
168                         lock (this){\r
169                                 return reader.ReadLine ();\r
170                         }\r
171                 }\r
172 \r
173                 public override string ReadToEnd ()\r
174                 {\r
175                         lock (this){\r
176                                 return reader.ReadToEnd ();\r
177                         }\r
178                 }\r
179 #region Read Methods\r
180                 public override int Read ()\r
181                 {\r
182                         lock (this){\r
183                                 return reader.Read ();\r
184                         }\r
185                 }\r
186 \r
187                 public override int Read (char [] buffer, int index, int count)\r
188                 {\r
189                         lock (this){\r
190                                 return reader.Read (buffer, index, count);\r
191                         }\r
192                 }\r
193 #endregion\r
194                 \r
195         }\r
196 }\r