Make sure to clear compiled items. Fixes regressions appeared in WCF land.
[mono.git] / mcs / class / corlib / System.IO / StringReader.cs
1 //
2 // System.IO.StringReader
3 //
4 // Author: Marcin Szczepanski (marcins@zipworld.com.au)
5 //
6 // Copyright (C) 2004 Novell (http://www.novell.com)
7 // 
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
32 using System;
33 using System.Globalization;
34 using System.Runtime.InteropServices;
35
36 namespace System.IO {
37         [Serializable]
38         [ComVisible (true)]
39         public class StringReader : TextReader {
40
41                 string source;
42                 int nextChar;
43                 int sourceLength;
44
45                 public StringReader( string s ) {
46
47                         if (s == null) 
48                                 throw new ArgumentNullException ("s");
49
50                         this.source = s;
51                         nextChar = 0;
52                         sourceLength = s.Length;
53                 }
54
55                 public override void Close ()
56                 {
57                         Dispose (true);
58                 }
59
60                 protected override void Dispose (bool disposing)
61                 {
62                         source = null;
63                         base.Dispose (disposing);
64                 }
65
66                 public override int Peek() {
67
68                         CheckObjectDisposedException ();
69
70                         if( nextChar >= sourceLength ) {
71                                 return -1;
72                         } else {
73                                 return (int)source[ nextChar ];
74                         }
75                 }
76
77                 public override int Read() {
78
79                         CheckObjectDisposedException ();
80
81                         if( nextChar >= sourceLength ) {
82                                 return -1;
83                         } else {
84                                 return (int)source[ nextChar++ ];
85                         }
86                 }
87
88
89                 // The method will read up to count characters from the StringReader
90                 // into the buffer character array starting at position index. Returns
91                 // the actual number of characters read, or zero if the end of the string
92                 // has been reached and no characters are read.
93
94                 public override int Read ([In, Out] char[] buffer, int index, int count)
95                 {
96                         CheckObjectDisposedException ();
97
98                         if (buffer == null)
99                                 throw new ArgumentNullException ("buffer");
100                         if (buffer.Length - index < count)
101                                 throw new ArgumentException ();
102                         if (index < 0 || count < 0)
103                                 throw new ArgumentOutOfRangeException ();
104
105                         int charsToRead;
106
107                         // reordered to avoir possible integer overflow
108                         if (nextChar > sourceLength - count)
109                                 charsToRead = sourceLength - nextChar;
110                         else
111                                 charsToRead = count;
112                         
113                         source.CopyTo (nextChar, buffer, index, charsToRead);
114
115                         nextChar += charsToRead;
116
117                         return charsToRead;
118                 }
119
120                 public override string ReadLine ()
121                 {
122                         // Reads until next \r or \n or \r\n, otherwise return null
123
124                         // LAMESPEC:
125                         // The Beta 2 SDK help says that the ReadLine method
126                         // returns "The next line from the input stream [...] A
127                         // line is defined as a sequence of characters followed by
128                         // a carriage return (\r), a line feed (\n), or a carriage
129                         // return immediately followed by a line feed (\r\n).
130                         // [...] The returned value is a null reference if the end
131                         // of the input stream has been reached."
132                         //
133                         // HOWEVER, the MS implementation returns the rest of
134                         // the string if no \r and/or \n is found in the string
135
136                         CheckObjectDisposedException ();
137
138                         if (nextChar >= source.Length)
139                                 return null;
140
141                         int nextCR = source.IndexOf ('\r', nextChar);
142                         int nextLF = source.IndexOf ('\n', nextChar);
143                         int readTo;
144                         bool consecutive = false;
145
146                         if (nextCR == -1) {
147                                 if (nextLF == -1)
148                                         return ReadToEnd ();
149
150                                 readTo = nextLF;
151                         } else if (nextLF == -1) {
152                                 readTo = nextCR;
153                         } else {
154                                 readTo = (nextCR > nextLF) ? nextLF : nextCR;
155                                 consecutive = (nextCR + 1 == nextLF);
156                         }
157
158                         string nextLine = source.Substring (nextChar, readTo - nextChar);
159                         nextChar = readTo + ((consecutive) ? 2 : 1);
160                         return nextLine;
161                 }
162
163                 public override string ReadToEnd() {
164
165                         CheckObjectDisposedException ();
166                         string toEnd = source.Substring( nextChar, sourceLength - nextChar );
167                         nextChar = sourceLength;
168                         return toEnd;
169                 }
170
171                 private void CheckObjectDisposedException ()
172                 {
173                         if (source == null)
174                                 throw new ObjectDisposedException ("StringReader", 
175                                         Locale.GetText ("Cannot read from a closed StringReader"));
176                 }
177         }
178 }