2 // System.IO.StreamReader.cs
5 // Dietmar Maurer (dietmar@ximian.com)
6 // Miguel de Icaza (miguel@ximian.com)
8 // (C) Ximian, Inc. http://www.ximian.com
17 public class StreamReader : TextReader {
19 private const int DefaultBufferSize = 1024;
20 private const int DefaultFileBufferSize = 4096;
21 private const int MinimumBufferSize = 128;
26 private byte [] input_buffer;
29 // The decoded buffer from the above input buffer
31 private char [] decoded_buffer;
34 // Decoded bytes in decoded_buffer.
36 private int decoded_count;
39 // Current position in the decoded_buffer
44 // The buffer size that we are using
46 private int buffer_size;
49 // Index into `input_buffer' where we start decoding
51 private int parse_start;
55 private Encoding encoding;
56 private Decoder decoder;
58 private Stream base_stream;
60 private class NullStreamReader : StreamReader {
61 public override int Peek ()
66 public override int Read ()
71 public override int Read (char[] buffer, int index, int count)
76 public override string ReadLine ()
81 public override string ReadToEnd ()
86 public override Stream BaseStream
88 get { return Stream.Null; }
91 public override Encoding CurrentEncoding
93 get { return Encoding.Unicode; }
97 public new static readonly StreamReader Null = (StreamReader)(new NullStreamReader());
99 internal StreamReader() {}
101 public StreamReader(Stream stream)
102 : this (stream, Encoding.UTF8, true, DefaultBufferSize) { }
104 public StreamReader(Stream stream, bool detect_encoding_from_bytemarks)
105 : this (stream, Encoding.UTF8, detect_encoding_from_bytemarks, DefaultBufferSize) { }
107 public StreamReader(Stream stream, Encoding encoding)
108 : this (stream, encoding, true, DefaultBufferSize) { }
110 public StreamReader(Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks)
111 : this (stream, encoding, detect_encoding_from_bytemarks, DefaultBufferSize) { }
113 public StreamReader(Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
115 Initialize (stream, encoding, detect_encoding_from_bytemarks, buffer_size);
118 public StreamReader(string path)
119 : this (path, Encoding.UTF8, true, DefaultFileBufferSize) { }
121 public StreamReader(string path, bool detect_encoding_from_bytemarks)
122 : this (path, Encoding.UTF8, detect_encoding_from_bytemarks, DefaultFileBufferSize) { }
124 public StreamReader(string path, Encoding encoding)
125 : this (path, encoding, true, DefaultFileBufferSize) { }
127 public StreamReader(string path, Encoding encoding, bool detect_encoding_from_bytemarks)
128 : this (path, encoding, detect_encoding_from_bytemarks, DefaultFileBufferSize) { }
130 public StreamReader(string path, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
133 throw new ArgumentNullException("path");
134 if (String.Empty == path)
135 throw new ArgumentException("Empty path not allowed");
136 if (path.IndexOfAny (Path.InvalidPathChars) != -1)
137 throw new ArgumentException("path contains invalid characters");
139 string DirName = Path.GetDirectoryName(path);
140 if (DirName != String.Empty && !Directory.Exists(DirName))
141 throw new DirectoryNotFoundException ("Directory '" + DirName + "' not found.");
142 if (!File.Exists(path))
143 throw new FileNotFoundException(path);
145 Stream stream = (Stream) File.OpenRead (path);
146 Initialize (stream, encoding, detect_encoding_from_bytemarks, buffer_size);
149 protected void Initialize (Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
152 throw new ArgumentNullException("stream");
154 throw new ArgumentException("Cannot read stream");
156 if (buffer_size < MinimumBufferSize)
157 buffer_size = MinimumBufferSize;
159 base_stream = stream;
160 input_buffer = new byte [buffer_size];
161 this.buffer_size = buffer_size;
162 this.encoding = encoding;
163 decoder = encoding.GetDecoder ();
165 byte [] preamble = encoding.GetPreamble ();
166 do_checks = detect_encoding_from_bytemarks ? 1 : 0;
167 do_checks += (preamble.Length == 0) ? 0 : 2;
169 decoded_buffer = new char [encoding.GetMaxCharCount (buffer_size)];
174 public virtual Stream BaseStream
181 public virtual Encoding CurrentEncoding
184 if (encoding == null)
185 throw new Exception ();
190 public override void Close ()
195 protected override void Dispose (bool disposing)
197 if (disposing && base_stream != null)
198 base_stream.Close ();
201 decoded_buffer = null;
205 base.Dispose (disposing);
209 // Provides auto-detection of the encoding, as well as skipping over
210 // byte marks at the beginning of a stream.
212 int DoChecks (int count)
214 if ((do_checks & 2) == 2){
215 byte [] preamble = encoding.GetPreamble ();
216 int c = preamble.Length;
220 for (i = 0; i < c; i++)
221 if (input_buffer [i] != preamble [i])
229 if ((do_checks & 1) == 1){
233 if (input_buffer [0] == 0xfe && input_buffer [1] == 0xff){
234 this.encoding = Encoding.BigEndianUnicode;
238 if (input_buffer [0] == 0xff && input_buffer [1] == 0xfe){
239 this.encoding = Encoding.Unicode;
246 if (input_buffer [0] == 0xef && input_buffer [1] == 0xbb && input_buffer [2] == 0xbf){
247 this.encoding = Encoding.UTF8;
255 // the buffer is empty, fill it again
256 private int ReadBuffer ()
261 // keep looping until the decoder gives us some chars
266 cbEncoded = base_stream.Read (input_buffer, 0, buffer_size);
272 Encoding old = encoding;
273 parse_start = DoChecks (cbEncoded);
274 if (old != encoding){
275 decoder = encoding.GetDecoder ();
278 cbEncoded -= parse_start;
281 decoded_count += decoder.GetChars (input_buffer, parse_start, cbEncoded, decoded_buffer, 0);
283 } while (decoded_count == 0);
285 return decoded_count;
288 public override int Peek ()
290 if (!base_stream.CanSeek)
293 if (pos >= decoded_count && ReadBuffer () == 0)
296 return decoded_buffer [pos];
299 public override int Read ()
301 if (pos >= decoded_count && ReadBuffer () == 0)
304 return decoded_buffer [pos++];
307 public override int Read (char[] dest_buffer, int index, int count)
309 if (dest_buffer == null)
310 throw new ArgumentException ();
312 if ((index < 0) || (count < 0))
313 throw new ArgumentOutOfRangeException ();
315 if (index + count > dest_buffer.Length)
316 throw new ArgumentException ();
321 if (pos >= decoded_count && ReadBuffer () == 0)
322 return chars_read > 0 ? chars_read : 0;
324 int cch = Math.Min (decoded_count - pos, count);
325 Array.Copy (decoded_buffer, pos, dest_buffer, index, cch);
334 public override string ReadLine()
336 StringBuilder text = new StringBuilder ();
341 if (c == -1) { // end of stream
342 if (text.Length == 0)
348 if (c == '\n') { // newline
349 if ((text.Length > 0) && (text [text.Length - 1] == '\r'))
354 text.Append ((char) c);
357 return text.ToString ();
360 public override string ReadToEnd()
362 StringBuilder text = new StringBuilder ();
364 int size = decoded_buffer.Length;
365 char [] buffer = new char [size];
368 while ((len = Read (buffer, 0, size)) != 0)
369 text.Append (buffer, 0, len);
371 return text.ToString ();