StreamReader.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 class NullStreamReader : StreamReader {
  47. public override int Peek ()
  48. {
  49. return -1;
  50. }
  51. public override int Read ()
  52. {
  53. return -1;
  54. }
  55. public override int Read (char[] buffer, int index, int count)
  56. {
  57. return 0;
  58. }
  59. public override string ReadLine ()
  60. {
  61. return null;
  62. }
  63. public override string ReadToEnd ()
  64. {
  65. return String.Empty;
  66. }
  67. public override Stream BaseStream
  68. {
  69. get { return Stream.Null; }
  70. }
  71. public override Encoding CurrentEncoding
  72. {
  73. get { return Encoding.Unicode; }
  74. }
  75. }
  76. public new static readonly StreamReader Null = (StreamReader)(new NullStreamReader());
  77. internal StreamReader() {}
  78. public StreamReader(Stream stream)
  79. : this (stream, Encoding.UTF8Unmarked, true, DefaultBufferSize) { }
  80. public StreamReader(Stream stream, bool detect_encoding_from_bytemarks)
  81. : this (stream, Encoding.UTF8Unmarked, detect_encoding_from_bytemarks, DefaultBufferSize) { }
  82. public StreamReader(Stream stream, Encoding encoding)
  83. : this (stream, encoding, true, DefaultBufferSize) { }
  84. public StreamReader(Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks)
  85. : this (stream, encoding, detect_encoding_from_bytemarks, DefaultBufferSize) { }
  86. public StreamReader(Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
  87. {
  88. Initialize (stream, encoding, detect_encoding_from_bytemarks, buffer_size);
  89. }
  90. public StreamReader(string path)
  91. : this (path, Encoding.UTF8Unmarked, true, DefaultFileBufferSize) { }
  92. public StreamReader(string path, bool detect_encoding_from_bytemarks)
  93. : this (path, Encoding.UTF8Unmarked, detect_encoding_from_bytemarks, DefaultFileBufferSize) { }
  94. public StreamReader(string path, Encoding encoding)
  95. : this (path, encoding, true, DefaultFileBufferSize) { }
  96. public StreamReader(string path, Encoding encoding, bool detect_encoding_from_bytemarks)
  97. : this (path, encoding, detect_encoding_from_bytemarks, DefaultFileBufferSize) { }
  98. public StreamReader(string path, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
  99. {
  100. if (null == path)
  101. throw new ArgumentNullException("path");
  102. if (String.Empty == path)
  103. throw new ArgumentException("Empty path not allowed");
  104. if (path.IndexOfAny (Path.InvalidPathChars) != -1)
  105. throw new ArgumentException("path contains invalid characters");
  106. string DirName = Path.GetDirectoryName(path);
  107. if (DirName != String.Empty && !Directory.Exists(DirName))
  108. throw new DirectoryNotFoundException ("Directory '" + DirName + "' not found.");
  109. if (!File.Exists(path))
  110. throw new FileNotFoundException(path);
  111. Stream stream = (Stream) File.OpenRead (path);
  112. Initialize (stream, encoding, detect_encoding_from_bytemarks, buffer_size);
  113. }
  114. protected void Initialize (Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
  115. {
  116. if (null == stream)
  117. throw new ArgumentNullException("stream");
  118. if (!stream.CanRead)
  119. throw new ArgumentException("Cannot read stream");
  120. if (buffer_size < MinimumBufferSize)
  121. buffer_size = MinimumBufferSize;
  122. base_stream = stream;
  123. input_buffer = new byte [buffer_size];
  124. this.buffer_size = buffer_size;
  125. this.encoding = encoding;
  126. decoder = encoding.GetDecoder ();
  127. byte [] preamble = encoding.GetPreamble ();
  128. do_checks = detect_encoding_from_bytemarks ? 1 : 0;
  129. do_checks += (preamble.Length == 0) ? 0 : 2;
  130. decoded_buffer = new char [encoding.GetMaxCharCount (buffer_size)];
  131. decoded_count = 0;
  132. pos = 0;
  133. }
  134. public virtual Stream BaseStream
  135. {
  136. get {
  137. return base_stream;
  138. }
  139. }
  140. public virtual Encoding CurrentEncoding
  141. {
  142. get {
  143. if (encoding == null)
  144. throw new Exception ();
  145. return encoding;
  146. }
  147. }
  148. public override void Close ()
  149. {
  150. Dispose (true);
  151. }
  152. protected override void Dispose (bool disposing)
  153. {
  154. if (disposing && base_stream != null)
  155. base_stream.Close ();
  156. input_buffer = null;
  157. decoded_buffer = null;
  158. encoding = null;
  159. decoder = null;
  160. base_stream = null;
  161. base.Dispose (disposing);
  162. }
  163. //
  164. // Provides auto-detection of the encoding, as well as skipping over
  165. // byte marks at the beginning of a stream.
  166. //
  167. int DoChecks (int count)
  168. {
  169. if ((do_checks & 2) == 2){
  170. byte [] preamble = encoding.GetPreamble ();
  171. int c = preamble.Length;
  172. if (count >= c){
  173. int i;
  174. for (i = 0; i < c; i++)
  175. if (input_buffer [i] != preamble [i])
  176. break;
  177. if (i == c)
  178. return i;
  179. }
  180. }
  181. if ((do_checks & 1) == 1){
  182. if (count < 2)
  183. return 0;
  184. if (input_buffer [0] == 0xfe && input_buffer [1] == 0xff){
  185. this.encoding = Encoding.BigEndianUnicode;
  186. return 2;
  187. }
  188. if (input_buffer [0] == 0xff && input_buffer [1] == 0xfe){
  189. this.encoding = Encoding.Unicode;
  190. return 2;
  191. }
  192. if (count < 3)
  193. return 0;
  194. if (input_buffer [0] == 0xef && input_buffer [1] == 0xbb && input_buffer [2] == 0xbf){
  195. this.encoding = Encoding.UTF8Unmarked;
  196. return 3;
  197. }
  198. }
  199. return 0;
  200. }
  201. // the buffer is empty, fill it again
  202. private int ReadBuffer ()
  203. {
  204. pos = 0;
  205. int cbEncoded = 0;
  206. // keep looping until the decoder gives us some chars
  207. decoded_count = 0;
  208. int parse_start = 0;
  209. do
  210. {
  211. cbEncoded = base_stream.Read (input_buffer, 0, buffer_size);
  212. if (cbEncoded == 0)
  213. return 0;
  214. if (do_checks > 0){
  215. Encoding old = encoding;
  216. parse_start = DoChecks (cbEncoded);
  217. if (old != encoding){
  218. decoder = encoding.GetDecoder ();
  219. }
  220. do_checks = 0;
  221. cbEncoded -= parse_start;
  222. }
  223. decoded_count += decoder.GetChars (input_buffer, parse_start, cbEncoded, decoded_buffer, 0);
  224. parse_start = 0;
  225. } while (decoded_count == 0);
  226. return decoded_count;
  227. }
  228. public override int Peek ()
  229. {
  230. if (!base_stream.CanSeek)
  231. return -1;
  232. if (pos >= decoded_count && 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. }