StreamReader.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. //
  2. // System.IO.StreamReader.cs
  3. //
  4. // Author:
  5. // Dietmar Maurer ([email protected])
  6. // Miguel de Icaza ([email protected])
  7. //
  8. // (C) Ximian, Inc. http://www.ximian.com
  9. //
  10. using System;
  11. using System.Text;
  12. namespace System.IO {
  13. [Serializable]
  14. public class StreamReader : TextReader {
  15. private const int DefaultBufferSize = 1024;
  16. private const int DefaultFileBufferSize = 4096;
  17. private const int MinimumBufferSize = 128;
  18. //
  19. // The input buffer
  20. //
  21. private byte [] input_buffer;
  22. //
  23. // The decoded buffer from the above input buffer
  24. //
  25. private char [] decoded_buffer;
  26. //
  27. // Decoded bytes in decoded_buffer.
  28. //
  29. private int decoded_count;
  30. //
  31. // Current position in the decoded_buffer
  32. //
  33. private int pos;
  34. //
  35. // The buffer size that we are using
  36. //
  37. private int buffer_size;
  38. //
  39. // Index into `input_buffer' where we start decoding
  40. //
  41. private int parse_start;
  42. int do_checks;
  43. private Encoding encoding;
  44. private Decoder decoder;
  45. private Stream base_stream;
  46. private bool mayBlock;
  47. private class NullStreamReader : StreamReader {
  48. public override int Peek ()
  49. {
  50. return -1;
  51. }
  52. public override int Read ()
  53. {
  54. return -1;
  55. }
  56. public override int Read (char[] buffer, int index, int count)
  57. {
  58. return 0;
  59. }
  60. public override string ReadLine ()
  61. {
  62. return null;
  63. }
  64. public override string ReadToEnd ()
  65. {
  66. return String.Empty;
  67. }
  68. public override Stream BaseStream
  69. {
  70. get { return Stream.Null; }
  71. }
  72. public override Encoding CurrentEncoding
  73. {
  74. get { return Encoding.Unicode; }
  75. }
  76. }
  77. public new static readonly StreamReader Null = (StreamReader)(new NullStreamReader());
  78. internal StreamReader() {}
  79. public StreamReader(Stream stream)
  80. : this (stream, Encoding.UTF8Unmarked, true, DefaultBufferSize) { }
  81. public StreamReader(Stream stream, bool detect_encoding_from_bytemarks)
  82. : this (stream, Encoding.UTF8Unmarked, detect_encoding_from_bytemarks, DefaultBufferSize) { }
  83. public StreamReader(Stream stream, Encoding encoding)
  84. : this (stream, encoding, true, DefaultBufferSize) { }
  85. public StreamReader(Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks)
  86. : this (stream, encoding, detect_encoding_from_bytemarks, DefaultBufferSize) { }
  87. public StreamReader(Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
  88. {
  89. Initialize (stream, encoding, detect_encoding_from_bytemarks, buffer_size);
  90. }
  91. public StreamReader(string path)
  92. : this (path, Encoding.UTF8Unmarked, true, DefaultFileBufferSize) { }
  93. public StreamReader(string path, bool detect_encoding_from_bytemarks)
  94. : this (path, Encoding.UTF8Unmarked, detect_encoding_from_bytemarks, DefaultFileBufferSize) { }
  95. public StreamReader(string path, Encoding encoding)
  96. : this (path, encoding, true, DefaultFileBufferSize) { }
  97. public StreamReader(string path, Encoding encoding, bool detect_encoding_from_bytemarks)
  98. : this (path, encoding, detect_encoding_from_bytemarks, DefaultFileBufferSize) { }
  99. public StreamReader(string path, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
  100. {
  101. if (null == path)
  102. throw new ArgumentNullException("path");
  103. if (String.Empty == path)
  104. throw new ArgumentException("Empty path not allowed");
  105. if (path.IndexOfAny (Path.InvalidPathChars) != -1)
  106. throw new ArgumentException("path contains invalid characters");
  107. string DirName = Path.GetDirectoryName(path);
  108. if (DirName != String.Empty && !Directory.Exists(DirName))
  109. throw new DirectoryNotFoundException ("Directory '" + DirName + "' not found.");
  110. if (!File.Exists(path))
  111. throw new FileNotFoundException("File not found.", path);
  112. Stream stream = (Stream) File.OpenRead (path);
  113. Initialize (stream, encoding, detect_encoding_from_bytemarks, buffer_size);
  114. }
  115. internal void Initialize (Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
  116. {
  117. if (null == stream)
  118. throw new ArgumentNullException("stream");
  119. if (!stream.CanRead)
  120. throw new ArgumentException("Cannot read stream");
  121. if (buffer_size < MinimumBufferSize)
  122. buffer_size = MinimumBufferSize;
  123. base_stream = stream;
  124. input_buffer = new byte [buffer_size];
  125. this.buffer_size = buffer_size;
  126. this.encoding = encoding;
  127. decoder = encoding.GetDecoder ();
  128. byte [] preamble = encoding.GetPreamble ();
  129. do_checks = detect_encoding_from_bytemarks ? 1 : 0;
  130. do_checks += (preamble.Length == 0) ? 0 : 2;
  131. decoded_buffer = new char [encoding.GetMaxCharCount (buffer_size)];
  132. decoded_count = 0;
  133. pos = 0;
  134. }
  135. public virtual Stream BaseStream
  136. {
  137. get {
  138. return base_stream;
  139. }
  140. }
  141. public virtual Encoding CurrentEncoding
  142. {
  143. get {
  144. if (encoding == null)
  145. throw new Exception ();
  146. return encoding;
  147. }
  148. }
  149. public override void Close ()
  150. {
  151. Dispose (true);
  152. }
  153. protected override void Dispose (bool disposing)
  154. {
  155. if (disposing && base_stream != null)
  156. base_stream.Close ();
  157. input_buffer = null;
  158. decoded_buffer = null;
  159. encoding = null;
  160. decoder = null;
  161. base_stream = null;
  162. base.Dispose (disposing);
  163. }
  164. //
  165. // Provides auto-detection of the encoding, as well as skipping over
  166. // byte marks at the beginning of a stream.
  167. //
  168. int DoChecks (int count)
  169. {
  170. if ((do_checks & 2) == 2){
  171. byte [] preamble = encoding.GetPreamble ();
  172. int c = preamble.Length;
  173. if (count >= c){
  174. int i;
  175. for (i = 0; i < c; i++)
  176. if (input_buffer [i] != preamble [i])
  177. break;
  178. if (i == c)
  179. return i;
  180. }
  181. }
  182. if ((do_checks & 1) == 1){
  183. if (count < 2)
  184. return 0;
  185. if (input_buffer [0] == 0xfe && input_buffer [1] == 0xff){
  186. this.encoding = Encoding.BigEndianUnicode;
  187. return 2;
  188. }
  189. if (input_buffer [0] == 0xff && input_buffer [1] == 0xfe){
  190. this.encoding = Encoding.Unicode;
  191. return 2;
  192. }
  193. if (count < 3)
  194. return 0;
  195. if (input_buffer [0] == 0xef && input_buffer [1] == 0xbb && input_buffer [2] == 0xbf){
  196. this.encoding = Encoding.UTF8Unmarked;
  197. return 3;
  198. }
  199. }
  200. return 0;
  201. }
  202. // the buffer is empty, fill it again
  203. private int ReadBuffer ()
  204. {
  205. pos = 0;
  206. int cbEncoded = 0;
  207. // keep looping until the decoder gives us some chars
  208. decoded_count = 0;
  209. int parse_start = 0;
  210. do
  211. {
  212. cbEncoded = base_stream.Read (input_buffer, 0, buffer_size);
  213. if (cbEncoded == 0)
  214. return 0;
  215. mayBlock = (cbEncoded < buffer_size);
  216. if (do_checks > 0){
  217. Encoding old = encoding;
  218. parse_start = DoChecks (cbEncoded);
  219. if (old != encoding){
  220. decoder = encoding.GetDecoder ();
  221. }
  222. do_checks = 0;
  223. cbEncoded -= parse_start;
  224. }
  225. decoded_count += decoder.GetChars (input_buffer, parse_start, cbEncoded, decoded_buffer, 0);
  226. parse_start = 0;
  227. } while (decoded_count == 0);
  228. return decoded_count;
  229. }
  230. public override int Peek ()
  231. {
  232. if (pos >= decoded_count && (mayBlock || ReadBuffer () == 0))
  233. return -1;
  234. return decoded_buffer [pos];
  235. }
  236. public override int Read ()
  237. {
  238. if (pos >= decoded_count && ReadBuffer () == 0)
  239. return -1;
  240. return decoded_buffer [pos++];
  241. }
  242. public override int Read (char[] dest_buffer, int index, int count)
  243. {
  244. if (dest_buffer == null)
  245. throw new ArgumentException ();
  246. if ((index < 0) || (count < 0))
  247. throw new ArgumentOutOfRangeException ();
  248. if (index + count > dest_buffer.Length)
  249. throw new ArgumentException ();
  250. int chars_read = 0;
  251. while (count > 0)
  252. {
  253. if (pos >= decoded_count && ReadBuffer () == 0)
  254. return chars_read > 0 ? chars_read : 0;
  255. int cch = Math.Min (decoded_count - pos, count);
  256. Array.Copy (decoded_buffer, pos, dest_buffer, index, cch);
  257. pos += cch;
  258. index += cch;
  259. count -= cch;
  260. chars_read += cch;
  261. }
  262. return chars_read;
  263. }
  264. public override string ReadLine()
  265. {
  266. bool foundCR = false;
  267. StringBuilder text = new StringBuilder ();
  268. while (true) {
  269. int c = Read ();
  270. if (c == -1) { // end of stream
  271. if (text.Length == 0)
  272. return null;
  273. if (foundCR)
  274. text.Length--;
  275. break;
  276. }
  277. if (c == '\n') { // newline
  278. if ((text.Length > 0) && (text [text.Length - 1] == '\r'))
  279. text.Length--;
  280. foundCR = false;
  281. break;
  282. } else if (foundCR) {
  283. pos--;
  284. text.Length--;
  285. break;
  286. }
  287. if (c == '\r')
  288. foundCR = true;
  289. text.Append ((char) c);
  290. }
  291. return text.ToString ();
  292. }
  293. public override string ReadToEnd()
  294. {
  295. StringBuilder text = new StringBuilder ();
  296. int size = decoded_buffer.Length;
  297. char [] buffer = new char [size];
  298. int len;
  299. while ((len = Read (buffer, 0, size)) != 0)
  300. text.Append (buffer, 0, len);
  301. return text.ToString ();
  302. }
  303. }
  304. }