New test.
[mono.git] / mcs / class / corlib / System.IO / StreamReader.cs
1 //
2 // System.IO.StreamReader.cs
3 //
4 // Author:
5 //   Dietmar Maurer (dietmar@ximian.com)
6 //   Miguel de Icaza (miguel@ximian.com) 
7 //
8 // (C) Ximian, Inc.  http://www.ximian.com
9 // Copyright (C) 2004 Novell (http://www.novell.com)
10 //
11
12 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System;
36 using System.Text;
37 using System.Runtime.InteropServices;
38
39 namespace System.IO {
40         [Serializable]
41         public class StreamReader : TextReader {
42
43                 const int DefaultBufferSize = 1024;
44                 const int DefaultFileBufferSize = 4096;
45                 const int MinimumBufferSize = 128;
46
47                 //
48                 // The input buffer
49                 //
50                 byte [] input_buffer;
51
52                 //
53                 // The decoded buffer from the above input buffer
54                 //
55                 char [] decoded_buffer;
56
57                 //
58                 // Decoded bytes in decoded_buffer.
59                 //
60                 int decoded_count;
61
62                 //
63                 // Current position in the decoded_buffer
64                 //
65                 int pos;
66
67                 //
68                 // The buffer size that we are using
69                 //
70                 int buffer_size;
71
72                 int do_checks;
73                 
74                 Encoding encoding;
75                 Decoder decoder;
76
77                 Stream base_stream;
78                 bool mayBlock;
79                 StringBuilder line_builder;
80
81                 private class NullStreamReader : StreamReader {
82                         public override int Peek ()
83                         {
84                                 return -1;
85                         }
86
87                         public override int Read ()
88                         {
89                                 return -1;
90                         }
91
92                         public override int Read ([In, Out] char[] buffer, int index, int count)
93                         {
94                                 return 0;
95                         }
96
97                         public override string ReadLine ()
98                         {
99                                 return null;
100                         }
101
102                         public override string ReadToEnd ()
103                         {
104                                 return String.Empty;
105                         }
106
107                         public override Stream BaseStream
108                         {
109                                 get { return Stream.Null; }
110                         }
111
112                         public override Encoding CurrentEncoding
113                         {
114                                 get { return Encoding.Unicode; }
115                         }
116                 }
117
118                 public new static readonly StreamReader Null =  (StreamReader)(new NullStreamReader());
119                 
120                 internal StreamReader() {}
121
122                 public StreamReader(Stream stream)
123                         : this (stream, Encoding.UTF8Unmarked, true, DefaultBufferSize) { }
124
125                 public StreamReader(Stream stream, bool detect_encoding_from_bytemarks)
126                         : this (stream, Encoding.UTF8Unmarked, detect_encoding_from_bytemarks, DefaultBufferSize) { }
127
128                 public StreamReader(Stream stream, Encoding encoding)
129                         : this (stream, encoding, true, DefaultBufferSize) { }
130
131                 public StreamReader(Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks)
132                         : this (stream, encoding, detect_encoding_from_bytemarks, DefaultBufferSize) { }
133                 
134                 public StreamReader(Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
135                 {
136                         Initialize (stream, encoding, detect_encoding_from_bytemarks, buffer_size);
137                 }
138
139                 public StreamReader(string path)
140                         : this (path, Encoding.UTF8Unmarked, true, DefaultFileBufferSize) { }
141
142                 public StreamReader(string path, bool detect_encoding_from_bytemarks)
143                         : this (path, Encoding.UTF8Unmarked, detect_encoding_from_bytemarks, DefaultFileBufferSize) { }
144
145                 public StreamReader(string path, Encoding encoding)
146                         : this (path, encoding, true, DefaultFileBufferSize) { }
147
148                 public StreamReader(string path, Encoding encoding, bool detect_encoding_from_bytemarks)
149                         : this (path, encoding, detect_encoding_from_bytemarks, DefaultFileBufferSize) { }
150                 
151                 public StreamReader(string path, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
152                 {
153                         if (null == path)
154                                 throw new ArgumentNullException("path");
155                         if (String.Empty == path)
156                                 throw new ArgumentException("Empty path not allowed");
157                         if (path.IndexOfAny (Path.InvalidPathChars) != -1)
158                                 throw new ArgumentException("path contains invalid characters");
159                         if (null == encoding)
160                                 throw new ArgumentNullException ("encoding");
161                         if (buffer_size <= 0)
162                                 throw new ArgumentOutOfRangeException ("buffer_size", "The minimum size of the buffer must be positive");
163
164                         string DirName = Path.GetDirectoryName(path);
165                         if (DirName != String.Empty && !Directory.Exists(DirName))
166                                 throw new DirectoryNotFoundException ("Directory '" + DirName + "' not found.");
167                         if (!File.Exists(path))
168                                 throw new FileNotFoundException("File not found.", path);
169
170                         Stream stream = (Stream) File.OpenRead (path);
171                         Initialize (stream, encoding, detect_encoding_from_bytemarks, buffer_size);
172                 }
173
174                 internal void Initialize (Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
175                 {
176                         if (null == stream)
177                                 throw new ArgumentNullException ("stream");
178                         if (null == encoding)
179                                 throw new ArgumentNullException ("encoding");
180                         if (!stream.CanRead)
181                                 throw new ArgumentException ("Cannot read stream");
182                         if (buffer_size <= 0)
183                                 throw new ArgumentOutOfRangeException ("buffer_size", "The minimum size of the buffer must be positive");
184
185                         if (buffer_size < MinimumBufferSize)
186                                 buffer_size = MinimumBufferSize;
187
188                         base_stream = stream;
189                         input_buffer = new byte [buffer_size];
190                         this.buffer_size = buffer_size;
191                         this.encoding = encoding;
192                         decoder = encoding.GetDecoder ();
193
194                         byte [] preamble = encoding.GetPreamble ();
195                         do_checks = detect_encoding_from_bytemarks ? 1 : 0;
196                         do_checks += (preamble.Length == 0) ? 0 : 2;
197                         
198                         decoded_buffer = new char [encoding.GetMaxCharCount (buffer_size)];
199                         decoded_count = 0;
200                         pos = 0;
201                 }
202
203                 public virtual Stream BaseStream
204                 {
205                         get {
206                                 return base_stream;
207                         }
208                 }
209
210                 public virtual Encoding CurrentEncoding
211                 {
212                         get {
213                                 if (encoding == null)
214                                         throw new Exception ();
215                                 return encoding;
216                         }
217                 }
218
219 #if NET_2_0
220                 public bool EndOfStream {
221                         get { return Peek () < 0; }
222                 }
223 #endif
224
225                 public override void Close ()
226                 {
227                         Dispose (true);
228                 }
229
230                 protected override void Dispose (bool disposing)
231                 {
232                         if (disposing && base_stream != null)
233                                 base_stream.Close ();
234                         
235                         input_buffer = null;
236                         decoded_buffer = null;
237                         encoding = null;
238                         decoder = null;
239                         base_stream = null;
240                         base.Dispose (disposing);
241                 }
242
243                 //
244                 // Provides auto-detection of the encoding, as well as skipping over
245                 // byte marks at the beginning of a stream.
246                 //
247                 int DoChecks (int count)
248                 {
249                         if ((do_checks & 2) == 2){
250                                 byte [] preamble = encoding.GetPreamble ();
251                                 int c = preamble.Length;
252                                 if (count >= c){
253                                         int i;
254                                         
255                                         for (i = 0; i < c; i++)
256                                                 if (input_buffer [i] != preamble [i])
257                                                         break;
258
259                                         if (i == c)
260                                                 return i;
261                                 }
262                         }
263
264                         if ((do_checks & 1) == 1){
265                                 if (count < 2)
266                                         return 0;
267
268                                 if (input_buffer [0] == 0xfe && input_buffer [1] == 0xff){
269                                         this.encoding = Encoding.BigEndianUnicode;
270                                         return 2;
271                                 }
272
273                                 if (input_buffer [0] == 0xff && input_buffer [1] == 0xfe){
274                                         this.encoding = Encoding.Unicode;
275                                         return 2;
276                                 }
277
278                                 if (count < 3)
279                                         return 0;
280
281                                 if (input_buffer [0] == 0xef && input_buffer [1] == 0xbb && input_buffer [2] == 0xbf){
282                                         this.encoding = Encoding.UTF8Unmarked;
283                                         return 3;
284                                 }
285                         }
286
287                         return 0;
288                 }
289
290                 public void DiscardBufferedData ()
291                 {
292                         pos = decoded_count = 0;
293                         mayBlock = false;
294                         // Discard internal state of the decoder too.
295                         decoder = encoding.GetDecoder ();
296                 }
297                 
298                 // the buffer is empty, fill it again
299                 private int ReadBuffer ()
300                 {
301                         pos = 0;
302                         int cbEncoded = 0;
303
304                         // keep looping until the decoder gives us some chars
305                         decoded_count = 0;
306                         int parse_start = 0;
307                         do      
308                         {
309                                 cbEncoded = base_stream.Read (input_buffer, 0, buffer_size);
310                                 
311                                 if (cbEncoded <= 0)
312                                         return 0;
313
314                                 mayBlock = (cbEncoded < buffer_size);
315                                 if (do_checks > 0){
316                                         Encoding old = encoding;
317                                         parse_start = DoChecks (cbEncoded);
318                                         if (old != encoding){
319                                                 decoder = encoding.GetDecoder ();
320                                         }
321                                         do_checks = 0;
322                                         cbEncoded -= parse_start;
323                                 }
324                                 
325                                 decoded_count += decoder.GetChars (input_buffer, parse_start, cbEncoded, decoded_buffer, 0);
326                                 parse_start = 0;
327                         } while (decoded_count == 0);
328
329                         return decoded_count;
330                 }
331
332                 public override int Peek ()
333                 {
334                         if (base_stream == null)
335                                 throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
336                         if (pos >= decoded_count && (mayBlock || ReadBuffer () == 0))
337                                 return -1;
338
339                         return decoded_buffer [pos];
340                 }
341
342                 public override int Read ()
343                 {
344                         if (base_stream == null)
345                                 throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
346                         if (pos >= decoded_count && ReadBuffer () == 0)
347                                 return -1;
348
349                         return decoded_buffer [pos++];
350                 }
351
352                 public override int Read ([In, Out] char[] dest_buffer, int index, int count)
353                 {
354                         if (base_stream == null)
355                                 throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
356                         if (dest_buffer == null)
357                                 throw new ArgumentNullException ("dest_buffer");
358                         if (index < 0)
359                                 throw new ArgumentOutOfRangeException ("index", "< 0");
360                         if (count < 0)
361                                 throw new ArgumentOutOfRangeException ("count", "< 0");
362                         // re-ordered to avoid possible integer overflow
363                         if (index > dest_buffer.Length - count)
364                                 throw new ArgumentException ("index + count > dest_buffer.Length");
365
366                         int chars_read = 0;
367                         while (count > 0)
368                         {
369                                 if (pos >= decoded_count && ReadBuffer () == 0)
370                                         return chars_read > 0 ? chars_read : 0;
371
372                                 int cch = Math.Min (decoded_count - pos, count);
373                                 Array.Copy (decoded_buffer, pos, dest_buffer, index, cch);
374                                 pos += cch;
375                                 index += cch;
376                                 count -= cch;
377                                 chars_read += cch;
378                         }
379                         return chars_read;
380                 }
381
382                 bool foundCR;
383                 int FindNextEOL ()
384                 {
385                         char c = '\0';
386                         for (; pos < decoded_count; pos++) {
387                                 c = decoded_buffer [pos];
388                                 if (c == '\n') {
389                                         pos++;
390                                         int res = (foundCR) ? (pos - 2) : (pos - 1);
391                                         if (res < 0)
392                                                 res = 0; // if a new buffer starts with a \n and there was a \r at
393                                                         // the end of the previous one, we get here.
394                                         foundCR = false;
395                                         return res;
396                                 } else if (foundCR) {
397                                         foundCR = false;
398                                         return pos - 1;
399                                 }
400
401                                 foundCR = (c == '\r');
402                         }
403
404                         return -1;
405                 }
406
407                 public override string ReadLine()
408                 {
409                         if (base_stream == null)
410                                 throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
411
412                         if (pos >= decoded_count && ReadBuffer () == 0)
413                                 return null;
414
415                         int begin = pos;
416                         int end = FindNextEOL ();
417                         if (end < decoded_count && end >= begin)
418                                 return new string (decoded_buffer, begin, end - begin);
419
420                         if (line_builder == null)
421                                 line_builder = new StringBuilder ();
422                         else
423                                 line_builder.Length = 0;
424
425                         while (true) {
426                                 if (foundCR) // don't include the trailing CR if present
427                                         decoded_count--;
428
429                                 line_builder.Append (decoded_buffer, begin, decoded_count - begin);
430                                 if (ReadBuffer () == 0) {
431                                         if (line_builder.Capacity > 32768) {
432                                                 StringBuilder sb = line_builder;
433                                                 line_builder = null;
434                                                 return sb.ToString (0, sb.Length);
435                                         }
436                                         return line_builder.ToString (0, line_builder.Length);
437                                 }
438
439                                 begin = pos;
440                                 end = FindNextEOL ();
441                                 if (end < decoded_count && end >= begin) {
442                                         line_builder.Append (decoded_buffer, begin, end - begin);
443                                         if (line_builder.Capacity > 32768) {
444                                                 StringBuilder sb = line_builder;
445                                                 line_builder = null;
446                                                 return sb.ToString (0, sb.Length);
447                                         }
448                                         return line_builder.ToString (0, line_builder.Length);
449                                 }
450                         }
451                 }
452
453                 public override string ReadToEnd()
454                 {
455                         if (base_stream == null)
456                                 throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
457
458                         StringBuilder text = new StringBuilder ();
459
460                         int size = decoded_buffer.Length;
461                         char [] buffer = new char [size];
462                         int len;
463                         
464                         while ((len = Read (buffer, 0, size)) > 0)
465                                 text.Append (buffer, 0, len);
466
467                         return text.ToString ();
468                 }
469         }
470 }