StreamReader.cs 9.3 KB

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