TextReader.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. //
  2. // System.IO.TextReader
  3. //
  4. // Authors:
  5. // Marcin Szczepanski ([email protected])
  6. // Miguel de Icaza ([email protected])
  7. // Marek Safar ([email protected])
  8. //
  9. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  10. // Copyright 2011 Xamarin Inc.
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Runtime.InteropServices;
  33. #if NET_4_5
  34. using System.Threading.Tasks;
  35. #endif
  36. namespace System.IO {
  37. [Serializable]
  38. [ComVisible (true)]
  39. #if NET_2_1
  40. public abstract class TextReader : IDisposable {
  41. #else
  42. public abstract class TextReader : MarshalByRefObject, IDisposable {
  43. #endif
  44. sealed class NullTextReader : TextReader
  45. {
  46. public override string ReadLine ()
  47. {
  48. return null;
  49. }
  50. public override string ReadToEnd ()
  51. {
  52. return String.Empty;
  53. }
  54. }
  55. public static readonly TextReader Null = new NullTextReader ();
  56. protected TextReader()
  57. {
  58. }
  59. public virtual void Close()
  60. {
  61. Dispose(true);
  62. }
  63. public void Dispose ()
  64. {
  65. Dispose(true);
  66. }
  67. protected virtual void Dispose (bool disposing)
  68. {
  69. if (disposing){
  70. // If we are explicitly disposed, we can avoid finalization.
  71. GC.SuppressFinalize (this);
  72. }
  73. return;
  74. }
  75. public virtual int Peek()
  76. {
  77. return -1;
  78. }
  79. public virtual int Read()
  80. {
  81. return -1;
  82. }
  83. public virtual int Read ([In, Out] char[] buffer, int index, int count)
  84. {
  85. int c, i;
  86. for (i = 0; i < count; i++) {
  87. if ((c = Read ()) == -1)
  88. return i;
  89. buffer [index + i] = (char)c;
  90. }
  91. return i;
  92. }
  93. public virtual int ReadBlock ([In, Out] char [] buffer, int index, int count)
  94. {
  95. int total_read_count = 0;
  96. int current_read_count;
  97. do {
  98. current_read_count = Read (buffer, index, count);
  99. index += current_read_count;
  100. total_read_count += current_read_count;
  101. count -= current_read_count;
  102. } while (current_read_count != 0 && count > 0);
  103. return total_read_count;
  104. }
  105. public virtual string ReadLine ()
  106. {
  107. var result = new System.Text.StringBuilder ();
  108. int c;
  109. while ((c = Read ()) != -1){
  110. // check simple character line ending
  111. if (c == '\n')
  112. break;
  113. if (c == '\r') {
  114. if (Peek () == '\n')
  115. Read ();
  116. break;
  117. }
  118. result.Append ((char) c);
  119. }
  120. if (c == -1 && result.Length == 0)
  121. return null;
  122. return result.ToString ();
  123. }
  124. public virtual string ReadToEnd ()
  125. {
  126. var result = new System.Text.StringBuilder ();
  127. int c;
  128. while ((c = Read ()) != -1)
  129. result.Append ((char) c);
  130. return result.ToString ();
  131. }
  132. public static TextReader Synchronized (TextReader reader)
  133. {
  134. if (reader == null)
  135. throw new ArgumentNullException ("reader is null");
  136. if (reader is SynchronizedReader)
  137. return reader;
  138. return new SynchronizedReader (reader);
  139. }
  140. #if NET_4_5
  141. //
  142. // Use tuple to pack the arguments because it's faster than
  143. // setting up anonymous method container with an instance delegate
  144. //
  145. public virtual Task<int> ReadAsync (char[] buffer, int index, int count)
  146. {
  147. return Task.Factory.StartNew (l => {
  148. var t = (Tuple<TextReader, char[], int, int>) l;
  149. return t.Item1.Read (t.Item2, t.Item3, t.Item4);
  150. }, Tuple.Create (this, buffer, index, count));
  151. }
  152. public virtual Task<int> ReadBlockAsync (char[] buffer, int index, int count)
  153. {
  154. return Task.Factory.StartNew (l => {
  155. var t = (Tuple<TextReader, char[], int, int>) l;
  156. return t.Item1.ReadBlock (t.Item2, t.Item3, t.Item4);
  157. }, Tuple.Create (this, buffer, index, count));
  158. }
  159. public virtual Task<string> ReadLineAsync ()
  160. {
  161. return Task.Factory.StartNew (l => ((TextReader) l).ReadLine (), this);
  162. }
  163. public virtual Task<string> ReadToEndAsync ()
  164. {
  165. return Task.Factory.StartNew (l => ((TextReader) l).ReadToEnd (), this);
  166. }
  167. #endif
  168. }
  169. //
  170. // Synchronized Reader implementation, used internally.
  171. //
  172. [Serializable]
  173. sealed class SynchronizedReader : TextReader
  174. {
  175. readonly TextReader reader;
  176. public SynchronizedReader (TextReader reader)
  177. {
  178. this.reader = reader;
  179. }
  180. public override void Close ()
  181. {
  182. lock (this){
  183. reader.Close ();
  184. }
  185. }
  186. public override int Peek ()
  187. {
  188. lock (this){
  189. return reader.Peek ();
  190. }
  191. }
  192. public override int ReadBlock (char [] buffer, int index, int count)
  193. {
  194. lock (this){
  195. return reader.ReadBlock (buffer, index, count);
  196. }
  197. }
  198. public override string ReadLine ()
  199. {
  200. lock (this){
  201. return reader.ReadLine ();
  202. }
  203. }
  204. public override string ReadToEnd ()
  205. {
  206. lock (this){
  207. return reader.ReadToEnd ();
  208. }
  209. }
  210. public override int Read ()
  211. {
  212. lock (this){
  213. return reader.Read ();
  214. }
  215. }
  216. public override int Read (char [] buffer, int index, int count)
  217. {
  218. lock (this){
  219. return reader.Read (buffer, index, count);
  220. }
  221. }
  222. #if NET_4_5
  223. public override Task<int> ReadAsync (char[] buffer, int index, int count)
  224. {
  225. lock (this) {
  226. return reader.ReadAsync (buffer, index, count);
  227. }
  228. }
  229. public override Task<int> ReadBlockAsync (char[] buffer, int index, int count)
  230. {
  231. lock (this) {
  232. return reader.ReadBlockAsync (buffer, index, count);
  233. }
  234. }
  235. public override Task<string> ReadLineAsync ()
  236. {
  237. lock (this) {
  238. return reader.ReadLineAsync ();
  239. }
  240. }
  241. public override Task<string> ReadToEndAsync ()
  242. {
  243. lock (this) {
  244. return reader.ReadToEndAsync ();
  245. }
  246. }
  247. #endif
  248. }
  249. }