-//\r
-// System.IO.StreamReader.cs\r
-//\r
-// Author:\r
-// Dietmar Maurer (dietmar@ximian.com)\r
-//\r
-// (C) Ximian, Inc. http://www.ximian.com\r
-//\r
-\r
-using System;\r
-using System.Text;\r
-\r
-namespace System.IO {\r
- [Serializable]\r
- public class StreamReader : TextReader {\r
-\r
- private const int DefaultBufferSize = 1024;\r
- private const int DefaultFileBufferSize = 4096;\r
- private const int MinimumBufferSize = 128;\r
-\r
- // buffering members\r
- private byte [] rgbEncoded;\r
-// private int cbEncoded;\r
- private char [] rgchDecoded;\r
- private int cchDecoded;\r
-\r
- private int pos;\r
-\r
-\r
- private Encoding internalEncoding;\r
- private Decoder decoder;\r
-\r
- private Stream internalStream;\r
-\r
- private class NullStreamReader : StreamReader {\r
- public override int Peek ()\r
- {\r
- return -1;\r
- }\r
-\r
- public override int Read ()\r
- {\r
- return -1;\r
- }\r
-\r
- public override int Read (char[] buffer, int index, int count)\r
- {\r
- return 0;\r
- }\r
-\r
- public override string ReadLine ()\r
- {\r
- return null;\r
- }\r
-\r
- public override string ReadToEnd ()\r
- {\r
- return String.Empty;\r
- }\r
-\r
- public override Stream BaseStream\r
- {\r
- get { return Stream.Null; }\r
- }\r
-\r
- public override Encoding CurrentEncoding\r
- {\r
- get { return Encoding.Unicode; }\r
- }\r
- }\r
-\r
- public new static readonly StreamReader Null = (StreamReader)(new NullStreamReader());\r
-\r
- internal StreamReader() {}\r
-\r
- public StreamReader(Stream stream)\r
- : this (stream, null, false, DefaultBufferSize) { }\r
-\r
- public StreamReader(Stream stream, bool detectEncodingFromByteOrderMarks)\r
- : this (stream, null, detectEncodingFromByteOrderMarks, DefaultBufferSize) { }\r
-\r
- public StreamReader(Stream stream, Encoding encoding)\r
- : this (stream, encoding, false, DefaultBufferSize) { }\r
-\r
- public StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks)\r
- : this (stream, encoding, detectEncodingFromByteOrderMarks, DefaultBufferSize) { }\r
- \r
- public StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize)\r
- {\r
- Initialize (stream, encoding, detectEncodingFromByteOrderMarks, bufferSize);\r
- }\r
-\r
- public StreamReader(string path)\r
- : this (path, null, false, DefaultFileBufferSize) { }\r
-\r
- public StreamReader(string path, bool detectEncodingFromByteOrderMarks)\r
- : this (path, null, detectEncodingFromByteOrderMarks, DefaultFileBufferSize) { }\r
-\r
- public StreamReader(string path, Encoding encoding)\r
- : this (path, encoding, false, DefaultFileBufferSize) { }\r
-\r
- public StreamReader(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks)\r
- : this (path, encoding, detectEncodingFromByteOrderMarks, DefaultFileBufferSize) { }\r
- \r
- [MonoTODO]\r
- public StreamReader(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize)\r
- {\r
- if (null == path)\r
- throw new ArgumentNullException();\r
- if (String.Empty == path)\r
- throw new ArgumentException();\r
- if (path.IndexOfAny (Path.InvalidPathChars) != -1)\r
- throw new ArgumentException("path contains invalid characters");\r
-\r
- string DirName = Path.GetDirectoryName(path);\r
- if (DirName != String.Empty && !Directory.Exists(DirName))\r
- throw new DirectoryNotFoundException();\r
- if (!File.Exists(path))\r
- throw new FileNotFoundException(path);\r
-\r
- Stream stream = (Stream) File.OpenRead (path);\r
- Initialize (stream, encoding, detectEncodingFromByteOrderMarks, bufferSize);\r
- }\r
-\r
- [MonoTODO]\r
- protected void Initialize (Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize)\r
- {\r
- if (null == stream)\r
- throw new ArgumentNullException();\r
- if (!stream.CanRead)\r
- throw new ArgumentException("Cannot read stream");\r
-\r
- internalStream = stream;\r
-\r
- // use detect encoding flag\r
- if (encoding == null) {\r
- internalEncoding = Encoding.UTF8;\r
- decoder = Encoding.UTF8.GetDecoder ();\r
- } else {\r
- internalEncoding = encoding;\r
- decoder = encoding.GetDecoder ();\r
- }\r
-\r
- if (bufferSize < MinimumBufferSize)\r
- bufferSize = MinimumBufferSize;\r
-\r
- rgbEncoded = new byte [bufferSize];\r
- rgchDecoded = new char [internalEncoding.GetMaxCharCount (bufferSize)];\r
- pos = 0;\r
- cchDecoded = 0;\r
- }\r
-\r
- public virtual Stream BaseStream\r
- {\r
- get {\r
- return internalStream;\r
- }\r
- }\r
-\r
- public virtual Encoding CurrentEncoding\r
- {\r
- get {\r
- return internalEncoding;\r
- }\r
- }\r
-\r
- public override void Close ()\r
- {\r
- Dispose (true);\r
- }\r
-\r
- protected override void Dispose (bool disposing)\r
- {\r
- if (disposing)\r
- internalStream.Close ();\r
- \r
- rgbEncoded = null;\r
- rgchDecoded = null;\r
- }\r
-\r
- public void DiscardBufferedData ()\r
- {\r
- pos = 0;\r
- cchDecoded = 0;\r
-\r
-/* I'm sure there's no need to do all this\r
- if ((cchDecoded == null) || (pos == cchDecoded.Length))\r
- return;\r
-\r
- if (!internalStream.CanSeek)\r
- return;\r
-\r
- int seek_back = pos - cchDecoded.Length;\r
- internalStream.Seek (seek_back, SeekOrigin.Current);\r
-*/\r
- }\r
-\r
-\r
- // the buffer is empty, fill it again\r
- [MonoTODO ("handle byte order marks here")]\r
- private int ReadBuffer ()\r
- {\r
- pos = 0;\r
- int cbEncoded = 0;\r
- cchDecoded = 0;\r
- do // keep looping until the decoder gives us some chars\r
- {\r
- cbEncoded = internalStream.Read (rgbEncoded, 0, rgbEncoded.Length);\r
- // TODO: remove this line when iconv is fixed\r
- int bufcnt = decoder.GetCharCount (rgbEncoded, 0, cbEncoded);\r
-\r
- if (cbEncoded == 0)\r
- return 0;\r
- // TODO: remove byte order marks here\r
- cchDecoded += decoder.GetChars (rgbEncoded, 0, cbEncoded, rgchDecoded, 0);\r
- } while (cchDecoded == 0);\r
-\r
- return cchDecoded;\r
- }\r
-\r
- public override int Peek ()\r
- {\r
- if (!internalStream.CanSeek)\r
- return -1;\r
-\r
- if (pos >= cchDecoded && ReadBuffer () == 0)\r
- return -1;\r
-\r
- return rgchDecoded [pos];\r
- }\r
-\r
- public override int Read ()\r
- {\r
- if (pos >= cchDecoded && ReadBuffer () == 0)\r
- return -1;\r
-\r
- return rgchDecoded [pos++];\r
- }\r
-\r
- public override int Read (char[] dest_buffer, int index, int count)\r
- {\r
- if (dest_buffer == null)\r
- throw new ArgumentException ();\r
-\r
- if ((index < 0) || (count < 0))\r
- throw new ArgumentOutOfRangeException ();\r
-\r
- if (index + count > dest_buffer.Length)\r
- throw new ArgumentException ();\r
-\r
- int cchRead = 0;\r
- while (count > 0)\r
- {\r
- if (pos >= cchDecoded && ReadBuffer () == 0)\r
- return cchRead > 0? cchRead: -1;\r
-\r
- int cch = Math.Min (cchDecoded - pos, count);\r
- Array.Copy (rgchDecoded, pos, dest_buffer, index, cch);\r
- pos += cch;\r
- index += cch;\r
- count -= cch;\r
- cchRead += cch;\r
- }\r
- return cchRead;\r
- }\r
-\r
- public override string ReadLine()\r
- {\r
- StringBuilder text = new StringBuilder ();\r
-\r
- while (true) {\r
- int c = Read ();\r
-\r
- if (c == -1) { // end of stream\r
- if (text.Length == 0)\r
- return null;\r
-\r
- break;\r
- }\r
-\r
- if (c == '\n') { // newline\r
- if ((text.Length > 0) && (text [text.Length - 1] == '\r'))\r
- text.Length--;\r
- break;\r
- }\r
-\r
- text.Append ((char) c);\r
- }\r
-\r
- return text.ToString ();\r
- }\r
-\r
- public override string ReadToEnd()\r
- {\r
- StringBuilder text = new StringBuilder ();\r
-\r
- int c;\r
- while ((c = Read ()) != -1) {\r
- text.Append ((char) c);\r
- }\r
-\r
- if (text.Length == 0)\r
- return String.Empty;\r
- return text.ToString ();\r
- }\r
- }\r
-}\r
+//
+// System.IO.StreamReader.cs
+//
+// Author:
+// Dietmar Maurer (dietmar@ximian.com)
+// Miguel de Icaza (miguel@ximian.com)
+//
+// (C) Ximian, Inc. http://www.ximian.com
+// Copyright (C) 2004 Novell (http://www.novell.com)
+//
+
+//
+// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.Text;
+using System.Runtime.InteropServices;
+
+namespace System.IO {
+ [Serializable]
+ public class StreamReader : TextReader {
+
+ const int DefaultBufferSize = 1024;
+ const int DefaultFileBufferSize = 4096;
+ const int MinimumBufferSize = 128;
+
+ //
+ // The input buffer
+ //
+ byte [] input_buffer;
+
+ //
+ // The decoded buffer from the above input buffer
+ //
+ char [] decoded_buffer;
+
+ //
+ // Decoded bytes in decoded_buffer.
+ //
+ int decoded_count;
+
+ //
+ // Current position in the decoded_buffer
+ //
+ int pos;
+
+ //
+ // The buffer size that we are using
+ //
+ int buffer_size;
+
+ int do_checks;
+
+ Encoding encoding;
+ Decoder decoder;
+
+ Stream base_stream;
+ bool mayBlock;
+ StringBuilder line_builder;
+
+ private class NullStreamReader : StreamReader {
+ public override int Peek ()
+ {
+ return -1;
+ }
+
+ public override int Read ()
+ {
+ return -1;
+ }
+
+ public override int Read ([In, Out] char[] buffer, int index, int count)
+ {
+ return 0;
+ }
+
+ public override string ReadLine ()
+ {
+ return null;
+ }
+
+ public override string ReadToEnd ()
+ {
+ return String.Empty;
+ }
+
+ public override Stream BaseStream
+ {
+ get { return Stream.Null; }
+ }
+
+ public override Encoding CurrentEncoding
+ {
+ get { return Encoding.Unicode; }
+ }
+ }
+
+ public new static readonly StreamReader Null = (StreamReader)(new NullStreamReader());
+
+ internal StreamReader() {}
+
+ public StreamReader(Stream stream)
+ : this (stream, Encoding.UTF8Unmarked, true, DefaultBufferSize) { }
+
+ public StreamReader(Stream stream, bool detect_encoding_from_bytemarks)
+ : this (stream, Encoding.UTF8Unmarked, detect_encoding_from_bytemarks, DefaultBufferSize) { }
+
+ public StreamReader(Stream stream, Encoding encoding)
+ : this (stream, encoding, true, DefaultBufferSize) { }
+
+ public StreamReader(Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks)
+ : this (stream, encoding, detect_encoding_from_bytemarks, DefaultBufferSize) { }
+
+ public StreamReader(Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
+ {
+ Initialize (stream, encoding, detect_encoding_from_bytemarks, buffer_size);
+ }
+
+ public StreamReader(string path)
+ : this (path, Encoding.UTF8Unmarked, true, DefaultFileBufferSize) { }
+
+ public StreamReader(string path, bool detect_encoding_from_bytemarks)
+ : this (path, Encoding.UTF8Unmarked, detect_encoding_from_bytemarks, DefaultFileBufferSize) { }
+
+ public StreamReader(string path, Encoding encoding)
+ : this (path, encoding, true, DefaultFileBufferSize) { }
+
+ public StreamReader(string path, Encoding encoding, bool detect_encoding_from_bytemarks)
+ : this (path, encoding, detect_encoding_from_bytemarks, DefaultFileBufferSize) { }
+
+ public StreamReader(string path, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
+ {
+ if (null == path)
+ throw new ArgumentNullException("path");
+ if (String.Empty == path)
+ throw new ArgumentException("Empty path not allowed");
+ if (path.IndexOfAny (Path.InvalidPathChars) != -1)
+ throw new ArgumentException("path contains invalid characters");
+ if (null == encoding)
+ throw new ArgumentNullException ("encoding");
+ if (buffer_size <= 0)
+ throw new ArgumentOutOfRangeException ("buffer_size", "The minimum size of the buffer must be positive");
+
+ string DirName = Path.GetDirectoryName(path);
+ if (DirName != String.Empty && !Directory.Exists(DirName))
+ throw new DirectoryNotFoundException ("Directory '" + DirName + "' not found.");
+ if (!File.Exists(path))
+ throw new FileNotFoundException("File not found.", path);
+
+ Stream stream = (Stream) File.OpenRead (path);
+ Initialize (stream, encoding, detect_encoding_from_bytemarks, buffer_size);
+ }
+
+ internal void Initialize (Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
+ {
+ if (null == stream)
+ throw new ArgumentNullException ("stream");
+ if (null == encoding)
+ throw new ArgumentNullException ("encoding");
+ if (!stream.CanRead)
+ throw new ArgumentException ("Cannot read stream");
+ if (buffer_size <= 0)
+ throw new ArgumentOutOfRangeException ("buffer_size", "The minimum size of the buffer must be positive");
+
+ if (buffer_size < MinimumBufferSize)
+ buffer_size = MinimumBufferSize;
+
+ base_stream = stream;
+ input_buffer = new byte [buffer_size];
+ this.buffer_size = buffer_size;
+ this.encoding = encoding;
+ decoder = encoding.GetDecoder ();
+
+ byte [] preamble = encoding.GetPreamble ();
+ do_checks = detect_encoding_from_bytemarks ? 1 : 0;
+ do_checks += (preamble.Length == 0) ? 0 : 2;
+
+ decoded_buffer = new char [encoding.GetMaxCharCount (buffer_size)];
+ decoded_count = 0;
+ pos = 0;
+ }
+
+ public virtual Stream BaseStream
+ {
+ get {
+ return base_stream;
+ }
+ }
+
+ public virtual Encoding CurrentEncoding
+ {
+ get {
+ if (encoding == null)
+ throw new Exception ();
+ return encoding;
+ }
+ }
+
+#if NET_2_0
+ public bool EndOfStream {
+ get { return Peek () < 0; }
+ }
+#endif
+
+ public override void Close ()
+ {
+ Dispose (true);
+ }
+
+ protected override void Dispose (bool disposing)
+ {
+ if (disposing && base_stream != null)
+ base_stream.Close ();
+
+ input_buffer = null;
+ decoded_buffer = null;
+ encoding = null;
+ decoder = null;
+ base_stream = null;
+ base.Dispose (disposing);
+ }
+
+ //
+ // Provides auto-detection of the encoding, as well as skipping over
+ // byte marks at the beginning of a stream.
+ //
+ int DoChecks (int count)
+ {
+ if ((do_checks & 2) == 2){
+ byte [] preamble = encoding.GetPreamble ();
+ int c = preamble.Length;
+ if (count >= c){
+ int i;
+
+ for (i = 0; i < c; i++)
+ if (input_buffer [i] != preamble [i])
+ break;
+
+ if (i == c)
+ return i;
+ }
+ }
+
+ if ((do_checks & 1) == 1){
+ if (count < 2)
+ return 0;
+
+ if (input_buffer [0] == 0xfe && input_buffer [1] == 0xff){
+ this.encoding = Encoding.BigEndianUnicode;
+ return 2;
+ }
+
+ if (input_buffer [0] == 0xff && input_buffer [1] == 0xfe){
+ this.encoding = Encoding.Unicode;
+ return 2;
+ }
+
+ if (count < 3)
+ return 0;
+
+ if (input_buffer [0] == 0xef && input_buffer [1] == 0xbb && input_buffer [2] == 0xbf){
+ this.encoding = Encoding.UTF8Unmarked;
+ return 3;
+ }
+ }
+
+ return 0;
+ }
+
+ public void DiscardBufferedData ()
+ {
+ pos = decoded_count = 0;
+ mayBlock = false;
+ // Discard internal state of the decoder too.
+ decoder = encoding.GetDecoder ();
+ }
+
+ // the buffer is empty, fill it again
+ private int ReadBuffer ()
+ {
+ pos = 0;
+ int cbEncoded = 0;
+
+ // keep looping until the decoder gives us some chars
+ decoded_count = 0;
+ int parse_start = 0;
+ do
+ {
+ cbEncoded = base_stream.Read (input_buffer, 0, buffer_size);
+
+ if (cbEncoded == 0)
+ return 0;
+
+ mayBlock = (cbEncoded < buffer_size);
+ if (do_checks > 0){
+ Encoding old = encoding;
+ parse_start = DoChecks (cbEncoded);
+ if (old != encoding){
+ decoder = encoding.GetDecoder ();
+ }
+ do_checks = 0;
+ cbEncoded -= parse_start;
+ }
+
+ decoded_count += decoder.GetChars (input_buffer, parse_start, cbEncoded, decoded_buffer, 0);
+ parse_start = 0;
+ } while (decoded_count == 0);
+
+ return decoded_count;
+ }
+
+ public override int Peek ()
+ {
+ if (base_stream == null)
+ throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
+ if (pos >= decoded_count && (mayBlock || ReadBuffer () == 0))
+ return -1;
+
+ return decoded_buffer [pos];
+ }
+
+ public override int Read ()
+ {
+ if (base_stream == null)
+ throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
+ if (pos >= decoded_count && ReadBuffer () == 0)
+ return -1;
+
+ return decoded_buffer [pos++];
+ }
+
+ public override int Read ([In, Out] char[] dest_buffer, int index, int count)
+ {
+ if (base_stream == null)
+ throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
+ if (dest_buffer == null)
+ throw new ArgumentNullException ("dest_buffer");
+ if (index < 0)
+ throw new ArgumentOutOfRangeException ("index", "< 0");
+ if (count < 0)
+ throw new ArgumentOutOfRangeException ("count", "< 0");
+ // re-ordered to avoid possible integer overflow
+ if (index > dest_buffer.Length - count)
+ throw new ArgumentException ("index + count > dest_buffer.Length");
+
+ int chars_read = 0;
+ while (count > 0)
+ {
+ if (pos >= decoded_count && ReadBuffer () == 0)
+ return chars_read > 0 ? chars_read : 0;
+
+ int cch = Math.Min (decoded_count - pos, count);
+ Array.Copy (decoded_buffer, pos, dest_buffer, index, cch);
+ pos += cch;
+ index += cch;
+ count -= cch;
+ chars_read += cch;
+ }
+ return chars_read;
+ }
+
+ bool foundCR;
+ int FindNextEOL ()
+ {
+ char c = '\0';
+ for (; pos < decoded_count; pos++) {
+ c = decoded_buffer [pos];
+ if (c == '\n') {
+ pos++;
+ int res = (foundCR) ? (pos - 2) : (pos - 1);
+ if (res < 0)
+ res = 0; // if a new buffer starts with a \n and there was a \r at
+ // the end of the previous one, we get here.
+ foundCR = false;
+ return res;
+ } else if (foundCR) {
+ foundCR = false;
+ return pos - 1;
+ }
+
+ foundCR = (c == '\r');
+ }
+
+ return -1;
+ }
+
+ public override string ReadLine()
+ {
+ if (base_stream == null)
+ throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
+
+ if (pos >= decoded_count && ReadBuffer () == 0)
+ return null;
+
+ int begin = pos;
+ int end = FindNextEOL ();
+ if (end < decoded_count && end >= begin)
+ return new string (decoded_buffer, begin, end - begin);
+
+ if (line_builder == null)
+ line_builder = new StringBuilder ();
+ else
+ line_builder.Length = 0;
+
+ while (true) {
+ if (foundCR) // don't include the trailing CR if present
+ decoded_count--;
+
+ line_builder.Append (decoded_buffer, begin, decoded_count - begin);
+ if (ReadBuffer () == 0) {
+ if (line_builder.Capacity > 32768) {
+ StringBuilder sb = line_builder;
+ line_builder = null;
+ return sb.ToString (0, sb.Length);
+ }
+ return line_builder.ToString (0, line_builder.Length);
+ }
+
+ begin = pos;
+ end = FindNextEOL ();
+ if (end < decoded_count && end >= begin) {
+ line_builder.Append (decoded_buffer, begin, end - begin);
+ if (line_builder.Capacity > 32768) {
+ StringBuilder sb = line_builder;
+ line_builder = null;
+ return sb.ToString (0, sb.Length);
+ }
+ return line_builder.ToString (0, line_builder.Length);
+ }
+ }
+ }
+
+ public override string ReadToEnd()
+ {
+ if (base_stream == null)
+ throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
+
+ StringBuilder text = new StringBuilder ();
+
+ int size = decoded_buffer.Length;
+ char [] buffer = new char [size];
+ int len;
+
+ while ((len = Read (buffer, 0, size)) > 0)
+ text.Append (buffer, 0, len);
+
+ return text.ToString ();
+ }
+ }
+}