77a3d15a1b427c2e3cb9bbeb6014d9b5902a025d
[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
80                 private class NullStreamReader : StreamReader {
81                         public override int Peek ()
82                         {
83                                 return -1;
84                         }
85
86                         public override int Read ()
87                         {
88                                 return -1;
89                         }
90
91                         public override int Read ([In, Out] char[] buffer, int index, int count)
92                         {
93                                 return 0;
94                         }
95
96                         public override string ReadLine ()
97                         {
98                                 return null;
99                         }
100
101                         public override string ReadToEnd ()
102                         {
103                                 return String.Empty;
104                         }
105
106                         public override Stream BaseStream
107                         {
108                                 get { return Stream.Null; }
109                         }
110
111                         public override Encoding CurrentEncoding
112                         {
113                                 get { return Encoding.Unicode; }
114                         }
115                 }
116
117                 public new static readonly StreamReader Null =  (StreamReader)(new NullStreamReader());
118                 
119                 internal StreamReader() {}
120
121                 public StreamReader(Stream stream)
122                         : this (stream, Encoding.UTF8Unmarked, true, DefaultBufferSize) { }
123
124                 public StreamReader(Stream stream, bool detect_encoding_from_bytemarks)
125                         : this (stream, Encoding.UTF8Unmarked, detect_encoding_from_bytemarks, DefaultBufferSize) { }
126
127                 public StreamReader(Stream stream, Encoding encoding)
128                         : this (stream, encoding, true, DefaultBufferSize) { }
129
130                 public StreamReader(Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks)
131                         : this (stream, encoding, detect_encoding_from_bytemarks, DefaultBufferSize) { }
132                 
133                 public StreamReader(Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
134                 {
135                         Initialize (stream, encoding, detect_encoding_from_bytemarks, buffer_size);
136                 }
137
138                 public StreamReader(string path)
139                         : this (path, Encoding.UTF8Unmarked, true, DefaultFileBufferSize) { }
140
141                 public StreamReader(string path, bool detect_encoding_from_bytemarks)
142                         : this (path, Encoding.UTF8Unmarked, detect_encoding_from_bytemarks, DefaultFileBufferSize) { }
143
144                 public StreamReader(string path, Encoding encoding)
145                         : this (path, encoding, true, DefaultFileBufferSize) { }
146
147                 public StreamReader(string path, Encoding encoding, bool detect_encoding_from_bytemarks)
148                         : this (path, encoding, detect_encoding_from_bytemarks, DefaultFileBufferSize) { }
149                 
150                 public StreamReader(string path, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
151                 {
152                         if (null == path)
153                                 throw new ArgumentNullException("path");
154                         if (String.Empty == path)
155                                 throw new ArgumentException("Empty path not allowed");
156                         if (path.IndexOfAny (Path.InvalidPathChars) != -1)
157                                 throw new ArgumentException("path contains invalid characters");
158                         if (null == encoding)
159                                 throw new ArgumentNullException ("encoding");
160                         if (buffer_size <= 0)
161                                 throw new ArgumentOutOfRangeException ("buffer_size", "The minimum size of the buffer must be positive");
162
163                         string DirName = Path.GetDirectoryName(path);
164                         if (DirName != String.Empty && !Directory.Exists(DirName))
165                                 throw new DirectoryNotFoundException ("Directory '" + DirName + "' not found.");
166                         if (!File.Exists(path))
167                                 throw new FileNotFoundException("File not found.", path);
168
169                         Stream stream = (Stream) File.OpenRead (path);
170                         Initialize (stream, encoding, detect_encoding_from_bytemarks, buffer_size);
171                 }
172
173                 internal void Initialize (Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
174                 {
175                         if (null == stream)
176                                 throw new ArgumentNullException ("stream");
177                         if (null == encoding)
178                                 throw new ArgumentNullException ("encoding");
179                         if (!stream.CanRead)
180                                 throw new ArgumentException ("Cannot read stream");
181                         if (buffer_size <= 0)
182                                 throw new ArgumentOutOfRangeException ("buffer_size", "The minimum size of the buffer must be positive");
183
184                         if (buffer_size < MinimumBufferSize)
185                                 buffer_size = MinimumBufferSize;
186
187                         base_stream = stream;
188                         input_buffer = new byte [buffer_size];
189                         this.buffer_size = buffer_size;
190                         this.encoding = encoding;
191                         decoder = encoding.GetDecoder ();
192
193                         byte [] preamble = encoding.GetPreamble ();
194                         do_checks = detect_encoding_from_bytemarks ? 1 : 0;
195                         do_checks += (preamble.Length == 0) ? 0 : 2;
196                         
197                         decoded_buffer = new char [encoding.GetMaxCharCount (buffer_size)];
198                         decoded_count = 0;
199                         pos = 0;
200                 }
201
202                 public virtual Stream BaseStream
203                 {
204                         get {
205                                 return base_stream;
206                         }
207                 }
208
209                 public virtual Encoding CurrentEncoding
210                 {
211                         get {
212                                 if (encoding == null)
213                                         throw new Exception ();
214                                 return encoding;
215                         }
216                 }
217
218                 public override void Close ()
219                 {
220                         Dispose (true);
221                 }
222
223                 protected override void Dispose (bool disposing)
224                 {
225                         if (disposing && base_stream != null)
226                                 base_stream.Close ();
227                         
228                         input_buffer = null;
229                         decoded_buffer = null;
230                         encoding = null;
231                         decoder = null;
232                         base_stream = null;
233                         base.Dispose (disposing);
234                 }
235
236                 //
237                 // Provides auto-detection of the encoding, as well as skipping over
238                 // byte marks at the beginning of a stream.
239                 //
240                 int DoChecks (int count)
241                 {
242                         if ((do_checks & 2) == 2){
243                                 byte [] preamble = encoding.GetPreamble ();
244                                 int c = preamble.Length;
245                                 if (count >= c){
246                                         int i;
247                                         
248                                         for (i = 0; i < c; i++)
249                                                 if (input_buffer [i] != preamble [i])
250                                                         break;
251
252                                         if (i == c)
253                                                 return i;
254                                 }
255                         }
256
257                         if ((do_checks & 1) == 1){
258                                 if (count < 2)
259                                         return 0;
260
261                                 if (input_buffer [0] == 0xfe && input_buffer [1] == 0xff){
262                                         this.encoding = Encoding.BigEndianUnicode;
263                                         return 2;
264                                 }
265
266                                 if (input_buffer [0] == 0xff && input_buffer [1] == 0xfe){
267                                         this.encoding = Encoding.Unicode;
268                                         return 2;
269                                 }
270
271                                 if (count < 3)
272                                         return 0;
273
274                                 if (input_buffer [0] == 0xef && input_buffer [1] == 0xbb && input_buffer [2] == 0xbf){
275                                         this.encoding = Encoding.UTF8Unmarked;
276                                         return 3;
277                                 }
278                         }
279
280                         return 0;
281                 }
282
283                 public void DiscardBufferedData ()
284                 {
285                         pos = decoded_count = 0;
286                         mayBlock = false;
287                 }
288                 
289                 // the buffer is empty, fill it again
290                 private int ReadBuffer ()
291                 {
292                         pos = 0;
293                         int cbEncoded = 0;
294
295                         // keep looping until the decoder gives us some chars
296                         decoded_count = 0;
297                         int parse_start = 0;
298                         do      
299                         {
300                                 cbEncoded = base_stream.Read (input_buffer, 0, buffer_size);
301                                 
302                                 if (cbEncoded == 0)
303                                         return 0;
304
305                                 mayBlock = (cbEncoded < buffer_size);
306                                 if (do_checks > 0){
307                                         Encoding old = encoding;
308                                         parse_start = DoChecks (cbEncoded);
309                                         if (old != encoding){
310                                                 decoder = encoding.GetDecoder ();
311                                         }
312                                         do_checks = 0;
313                                         cbEncoded -= parse_start;
314                                 }
315                                 
316                                 decoded_count += decoder.GetChars (input_buffer, parse_start, cbEncoded, decoded_buffer, 0);
317                                 parse_start = 0;
318                         } while (decoded_count == 0);
319
320                         return decoded_count;
321                 }
322
323                 public override int Peek ()
324                 {
325                         if (base_stream == null)
326                                 throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
327                         if (pos >= decoded_count && (mayBlock || ReadBuffer () == 0))
328                                 return -1;
329
330                         return decoded_buffer [pos];
331                 }
332
333                 public override int Read ()
334                 {
335                         if (base_stream == null)
336                                 throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
337                         if (pos >= decoded_count && ReadBuffer () == 0)
338                                 return -1;
339
340                         return decoded_buffer [pos++];
341                 }
342
343                 public override int Read ([In, Out] char[] dest_buffer, int index, int count)
344                 {
345                         if (base_stream == null)
346                                 throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
347                         if (dest_buffer == null)
348                                 throw new ArgumentNullException ("dest_buffer");
349                         if (index < 0)
350                                 throw new ArgumentOutOfRangeException ("index", "< 0");
351                         if (count < 0)
352                                 throw new ArgumentOutOfRangeException ("count", "< 0");
353                         // re-ordered to avoid possible integer overflow
354                         if (index > dest_buffer.Length - count)
355                                 throw new ArgumentException ("index + count > dest_buffer.Length");
356
357                         int chars_read = 0;
358                         while (count > 0)
359                         {
360                                 if (pos >= decoded_count && ReadBuffer () == 0)
361                                         return chars_read > 0 ? chars_read : 0;
362
363                                 int cch = Math.Min (decoded_count - pos, count);
364                                 Array.Copy (decoded_buffer, pos, dest_buffer, index, cch);
365                                 pos += cch;
366                                 index += cch;
367                                 count -= cch;
368                                 chars_read += cch;
369                         }
370                         return chars_read;
371                 }
372
373                 public override string ReadLine()
374                 {
375                         if (base_stream == null)
376                                 throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
377                         
378                         bool foundCR = false;
379                         StringBuilder text = new StringBuilder ();
380
381                         while (true) {
382                                 int c = Read ();
383
384                                 if (c == -1) {                          // end of stream
385                                         if (text.Length == 0)
386                                                 return null;
387
388                                         if (foundCR)
389                                                 text.Length--;
390
391                                         break;
392                                 }
393
394                                 if (c == '\n') {                        // newline
395                                         if ((text.Length > 0) && (text [text.Length - 1] == '\r'))
396                                                 text.Length--;
397
398                                         foundCR = false;
399                                         break;
400                                 } else if (foundCR) {
401                                         pos--;
402                                         text.Length--;
403                                         break;
404                                 }
405
406                                 if (c == '\r')
407                                         foundCR = true;
408                                         
409
410                                 text.Append ((char) c);
411                         }
412
413                         return text.ToString ();
414                 }
415
416                 public override string ReadToEnd()
417                 {
418                         if (base_stream == null)
419                                 throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
420
421                         StringBuilder text = new StringBuilder ();
422
423                         int size = decoded_buffer.Length;
424                         char [] buffer = new char [size];
425                         int len;
426                         
427                         while ((len = Read (buffer, 0, size)) != 0)
428                                 text.Append (buffer, 0, len);
429
430                         return text.ToString ();
431                 }
432         }
433 }