TextReader.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // System.IO.TextReader
  3. //
  4. // Authors:
  5. // Marcin Szczepanski ([email protected])
  6. // Miguel de Icaza ([email protected])
  7. //
  8. //
  9. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Runtime.InteropServices;
  32. namespace System.IO {
  33. [Serializable]
  34. #if NET_2_0
  35. [ComVisible (true)]
  36. #endif
  37. public abstract class TextReader : MarshalByRefObject, IDisposable {
  38. protected TextReader() { }
  39. public static readonly TextReader Null;
  40. public virtual void Close()
  41. {
  42. Dispose(true);
  43. }
  44. #if NET_2_0
  45. public void Dispose ()
  46. #else
  47. void System.IDisposable.Dispose()
  48. #endif
  49. {
  50. Dispose(true);
  51. }
  52. protected virtual void Dispose( bool disposing )
  53. {
  54. return;
  55. }
  56. public virtual int Peek()
  57. {
  58. return -1;
  59. }
  60. public virtual int Read()
  61. {
  62. return -1;
  63. }
  64. public virtual int Read ([In, Out] char[] buffer, int index, int count)
  65. {
  66. int c, i;
  67. for (i = 0; i < count; i++) {
  68. if ((c = Read ()) == -1)
  69. return i;
  70. buffer [index + i] = (char)c;
  71. }
  72. return i;
  73. }
  74. public virtual int ReadBlock ([In, Out] char [] buffer, int index, int count)
  75. {
  76. int total_read_count = 0;
  77. int current_read_count = 0;
  78. do {
  79. current_read_count = Read (buffer, index, count);
  80. index += current_read_count;
  81. total_read_count += current_read_count;
  82. count -= current_read_count;
  83. } while (current_read_count != 0 && count > 0);
  84. return total_read_count;
  85. }
  86. public virtual string ReadLine()
  87. {
  88. return String.Empty;
  89. }
  90. public virtual string ReadToEnd()
  91. {
  92. return String.Empty;
  93. }
  94. public static TextReader Synchronized (TextReader reader)
  95. {
  96. if (reader == null)
  97. throw new ArgumentNullException ("reader is null");
  98. if (reader is SynchronizedReader)
  99. return reader;
  100. return new SynchronizedReader (reader);
  101. }
  102. }
  103. //
  104. // Synchronized Reader implementation, used internally.
  105. //
  106. [Serializable]
  107. internal class SynchronizedReader : TextReader {
  108. TextReader reader;
  109. public SynchronizedReader (TextReader reader)
  110. {
  111. this.reader = reader;
  112. }
  113. public override void Close ()
  114. {
  115. lock (this){
  116. reader.Close ();
  117. }
  118. }
  119. public override int Peek ()
  120. {
  121. lock (this){
  122. return reader.Peek ();
  123. }
  124. }
  125. public override int ReadBlock (char [] buffer, int index, int count)
  126. {
  127. lock (this){
  128. return reader.ReadBlock (buffer, index, count);
  129. }
  130. }
  131. public override string ReadLine ()
  132. {
  133. lock (this){
  134. return reader.ReadLine ();
  135. }
  136. }
  137. public override string ReadToEnd ()
  138. {
  139. lock (this){
  140. return reader.ReadToEnd ();
  141. }
  142. }
  143. #region Read Methods
  144. public override int Read ()
  145. {
  146. lock (this){
  147. return reader.Read ();
  148. }
  149. }
  150. public override int Read (char [] buffer, int index, int count)
  151. {
  152. lock (this){
  153. return reader.Read (buffer, index, count);
  154. }
  155. }
  156. #endregion
  157. }
  158. }