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
9 // Copyright (C) 2004 Novell (http://www.novell.com)
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
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.
37 using System
.Runtime
.InteropServices
;
44 public class StreamReader
: TextReader
{
46 const int DefaultBufferSize
= 1024;
47 const int DefaultFileBufferSize
= 4096;
48 const int MinimumBufferSize
= 128;
56 // The decoded buffer from the above input buffer
58 char [] decoded_buffer
;
61 // Decoded bytes in decoded_buffer.
66 // Current position in the decoded_buffer
71 // The buffer size that we are using
82 StringBuilder line_builder
;
84 private class NullStreamReader
: StreamReader
{
85 public override int Peek ()
90 public override int Read ()
95 public override int Read ([In
, Out
] char[] buffer
, int index
, int count
)
100 public override string ReadLine ()
105 public override string ReadToEnd ()
110 public override Stream BaseStream
112 get { return Stream.Null; }
115 public override Encoding CurrentEncoding
117 get { return Encoding.Unicode; }
121 public new static readonly StreamReader Null
= (StreamReader
)(new NullStreamReader());
123 internal StreamReader() {}
125 public StreamReader(Stream stream
)
126 : this (stream
, Encoding
.UTF8Unmarked
, true, DefaultBufferSize
) { }
128 public StreamReader(Stream stream
, bool detectEncodingFromByteOrderMarks
)
129 : this (stream
, Encoding
.UTF8Unmarked
, detectEncodingFromByteOrderMarks
, DefaultBufferSize
) { }
131 public StreamReader(Stream stream
, Encoding encoding
)
132 : this (stream
, encoding
, true, DefaultBufferSize
) { }
134 public StreamReader(Stream stream
, Encoding encoding
, bool detectEncodingFromByteOrderMarks
)
135 : this (stream
, encoding
, detectEncodingFromByteOrderMarks
, DefaultBufferSize
) { }
137 public StreamReader(Stream stream
, Encoding encoding
, bool detectEncodingFromByteOrderMarks
, int bufferSize
)
139 Initialize (stream
, encoding
, detectEncodingFromByteOrderMarks
, bufferSize
);
142 public StreamReader(string path
)
143 : this (path
, Encoding
.UTF8Unmarked
, true, DefaultFileBufferSize
) { }
145 public StreamReader(string path
, bool detectEncodingFromByteOrderMarks
)
146 : this (path
, Encoding
.UTF8Unmarked
, detectEncodingFromByteOrderMarks
, DefaultFileBufferSize
) { }
148 public StreamReader(string path
, Encoding encoding
)
149 : this (path
, encoding
, true, DefaultFileBufferSize
) { }
151 public StreamReader(string path
, Encoding encoding
, bool detectEncodingFromByteOrderMarks
)
152 : this (path
, encoding
, detectEncodingFromByteOrderMarks
, DefaultFileBufferSize
) { }
154 public StreamReader(string path
, Encoding encoding
, bool detectEncodingFromByteOrderMarks
, int bufferSize
)
157 throw new ArgumentNullException("path");
158 if (String
.Empty
== path
)
159 throw new ArgumentException("Empty path not allowed");
160 if (path
.IndexOfAny (Path
.InvalidPathChars
) != -1)
161 throw new ArgumentException("path contains invalid characters");
162 if (null == encoding
)
163 throw new ArgumentNullException ("encoding");
165 throw new ArgumentOutOfRangeException ("bufferSize", "The minimum size of the buffer must be positive");
167 Stream stream
= (Stream
) File
.OpenRead (path
);
168 Initialize (stream
, encoding
, detectEncodingFromByteOrderMarks
, bufferSize
);
171 internal void Initialize (Stream stream
, Encoding encoding
, bool detectEncodingFromByteOrderMarks
, int bufferSize
)
174 throw new ArgumentNullException ("stream");
175 if (null == encoding
)
176 throw new ArgumentNullException ("encoding");
178 throw new ArgumentException ("Cannot read stream");
180 throw new ArgumentOutOfRangeException ("bufferSize", "The minimum size of the buffer must be positive");
182 if (bufferSize
< MinimumBufferSize
)
183 bufferSize
= MinimumBufferSize
;
185 base_stream
= stream
;
186 input_buffer
= new byte [bufferSize
];
187 this.buffer_size
= bufferSize
;
188 this.encoding
= encoding
;
189 decoder
= encoding
.GetDecoder ();
191 byte [] preamble
= encoding
.GetPreamble ();
192 do_checks
= detectEncodingFromByteOrderMarks
? 1 : 0;
193 do_checks
+= (preamble
.Length
== 0) ? 0 : 2;
195 // since GetChars() might add flushed character, it
196 // should have additional char buffer for extra 1
197 // (probably 1 is ok, but might be insufficient. I'm not sure)
198 decoded_buffer
= new char [encoding
.GetMaxCharCount (bufferSize
) + 1];
203 public virtual Stream BaseStream
210 public virtual Encoding CurrentEncoding
213 if (encoding
== null)
214 throw new Exception ();
220 public bool EndOfStream
{
221 get { return Peek () < 0; }
225 public override void Close ()
230 protected override void Dispose (bool disposing
)
232 if (disposing
&& base_stream
!= null)
233 base_stream
.Close ();
236 decoded_buffer
= null;
240 base.Dispose (disposing
);
244 // Provides auto-detection of the encoding, as well as skipping over
245 // byte marks at the beginning of a stream.
247 int DoChecks (int count
)
249 if ((do_checks
& 2) == 2){
250 byte [] preamble
= encoding
.GetPreamble ();
251 int c
= preamble
.Length
;
255 for (i
= 0; i
< c
; i
++)
256 if (input_buffer
[i
] != preamble
[i
])
264 if ((do_checks
& 1) == 1){
269 if (input_buffer
[0] == 0xff && input_buffer
[1] == 0xfe){
270 this.encoding
= Encoding
.Unicode
;
275 if (input_buffer
[0] == 0xfe && input_buffer
[1] == 0xff){
276 this.encoding
= Encoding
.BigEndianUnicode
;
283 if (input_buffer
[0] == 0xef && input_buffer
[1] == 0xbb && input_buffer
[2] == 0xbf){
284 this.encoding
= Encoding
.UTF8Unmarked
;
290 if (input_buffer
[0] == 0xff && input_buffer
[1] == 0xfe && input_buffer
[2] != 0) {
291 this.encoding
= Encoding
.Unicode
;
297 if (input_buffer
[0] == 0 && input_buffer
[1] == 0
298 && input_buffer
[2] == 0xfe && input_buffer
[3] == 0xff)
300 this.encoding
= Encoding
.BigEndianUTF32
;
304 if (input_buffer
[0] == 0xff && input_buffer
[1] == 0xfe) {
305 if (input_buffer
[2] == 0 && input_buffer
[3] == 0) {
306 this.encoding
= Encoding
.UTF32
;
310 this.encoding
= Encoding
.Unicode
;
319 public void DiscardBufferedData ()
321 pos
= decoded_count
= 0;
323 // Discard internal state of the decoder too.
324 decoder
= encoding
.GetDecoder ();
327 // the buffer is empty, fill it again
328 private int ReadBuffer ()
333 // keep looping until the decoder gives us some chars
338 cbEncoded
= base_stream
.Read (input_buffer
, 0, buffer_size
);
343 mayBlock
= (cbEncoded
< buffer_size
);
345 Encoding old
= encoding
;
346 parse_start
= DoChecks (cbEncoded
);
347 if (old
!= encoding
){
348 decoder
= encoding
.GetDecoder ();
351 cbEncoded
-= parse_start
;
354 decoded_count
+= decoder
.GetChars (input_buffer
, parse_start
, cbEncoded
, decoded_buffer
, 0);
356 } while (decoded_count
== 0);
358 return decoded_count
;
361 public override int Peek ()
363 if (base_stream
== null)
364 throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
365 if (pos
>= decoded_count
&& (mayBlock
|| ReadBuffer () == 0))
368 return decoded_buffer
[pos
];
371 public override int Read ()
373 if (base_stream
== null)
374 throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
375 if (pos
>= decoded_count
&& ReadBuffer () == 0)
378 return decoded_buffer
[pos
++];
381 public override int Read ([In
, Out
] char[] buffer
, int index
, int count
)
383 if (base_stream
== null)
384 throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
386 throw new ArgumentNullException ("buffer");
388 throw new ArgumentOutOfRangeException ("index", "< 0");
390 throw new ArgumentOutOfRangeException ("count", "< 0");
391 // re-ordered to avoid possible integer overflow
392 if (index
> buffer
.Length
- count
)
393 throw new ArgumentException ("index + count > buffer.Length");
398 if (pos
>= decoded_count
&& ReadBuffer () == 0)
399 return chars_read
> 0 ? chars_read
: 0;
401 int cch
= Math
.Min (decoded_count
- pos
, count
);
402 Array
.Copy (decoded_buffer
, pos
, buffer
, index
, cch
);
417 for (; pos
< decoded_count
; pos
++) {
418 c
= decoded_buffer
[pos
];
421 int res
= (foundCR
) ? (pos
- 2) : (pos
- 1);
423 res
= 0; // if a new buffer starts with a \n and there was a \r at
424 // the end of the previous one, we get here.
427 } else if (foundCR
) {
430 return -2; // Need to flush the current buffered line.
431 // This is a \r at the end of the previous decoded buffer that
432 // is not followed by a \n in the current decoded buffer.
436 foundCR
= (c
== '\r');
442 public override string ReadLine()
444 if (base_stream
== null)
445 throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
447 if (pos
>= decoded_count
&& ReadBuffer () == 0)
451 int end
= FindNextEOL ();
452 if (end
< decoded_count
&& end
>= begin
)
453 return new string (decoded_buffer
, begin
, end
- begin
);
455 return line_builder
.ToString (0, line_builder
.Length
);
457 if (line_builder
== null)
458 line_builder
= new StringBuilder ();
460 line_builder
.Length
= 0;
463 if (foundCR
) // don't include the trailing CR if present
466 line_builder
.Append (decoded_buffer
, begin
, decoded_count
- begin
);
467 if (ReadBuffer () == 0) {
468 if (line_builder
.Capacity
> 32768) {
469 StringBuilder sb
= line_builder
;
471 return sb
.ToString (0, sb
.Length
);
473 return line_builder
.ToString (0, line_builder
.Length
);
477 end
= FindNextEOL ();
478 if (end
< decoded_count
&& end
>= begin
) {
479 line_builder
.Append (decoded_buffer
, begin
, end
- begin
);
480 if (line_builder
.Capacity
> 32768) {
481 StringBuilder sb
= line_builder
;
483 return sb
.ToString (0, sb
.Length
);
485 return line_builder
.ToString (0, line_builder
.Length
);
486 } else if (end
== -2)
487 return line_builder
.ToString (0, line_builder
.Length
);
491 public override string ReadToEnd()
493 if (base_stream
== null)
494 throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
496 StringBuilder text
= new StringBuilder ();
498 int size
= decoded_buffer
.Length
;
499 char [] buffer
= new char [size
];
502 while ((len
= Read (buffer
, 0, size
)) > 0)
503 text
.Append (buffer
, 0, len
);
505 return text
.ToString ();