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