2
0

TextReader.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. public abstract class TextReader : MarshalByRefObject, IDisposable {
  35. protected TextReader() { }
  36. public static readonly TextReader Null;
  37. public virtual void Close()
  38. {
  39. Dispose(true);
  40. }
  41. void System.IDisposable.Dispose()
  42. {
  43. Dispose(true);
  44. }
  45. protected virtual void Dispose( bool disposing )
  46. {
  47. return;
  48. }
  49. public virtual int Peek()
  50. {
  51. return -1;
  52. }
  53. public virtual int Read()
  54. {
  55. return -1;
  56. }
  57. public virtual int Read ([In, Out] char[] buffer, int index, int count)
  58. {
  59. int c, i;
  60. for (i = 0; i < count; i++) {
  61. if ((c = Read ()) == -1)
  62. return i;
  63. buffer [index + i] = (char)c;
  64. }
  65. return i;
  66. }
  67. public virtual int ReadBlock ([In, Out] char [] buffer, int index, int count)
  68. {
  69. int total_read_count = 0;
  70. int current_read_count = 0;
  71. do {
  72. current_read_count = Read (buffer, index, count);
  73. index += current_read_count;
  74. total_read_count += current_read_count;
  75. count -= current_read_count;
  76. } while (current_read_count != 0 && count > 0);
  77. return total_read_count;
  78. }
  79. public virtual string ReadLine()
  80. {
  81. return String.Empty;
  82. }
  83. public virtual string ReadToEnd()
  84. {
  85. return String.Empty;
  86. }
  87. public static TextReader Synchronized (TextReader reader)
  88. {
  89. if (reader == null)
  90. throw new ArgumentNullException ("reader is null");
  91. if (reader is SynchronizedReader)
  92. return reader;
  93. return new SynchronizedReader (reader);
  94. }
  95. }
  96. //
  97. // Synchronized Reader implementation, used internally.
  98. //
  99. [Serializable]
  100. internal class SynchronizedReader : TextReader {
  101. TextReader reader;
  102. public SynchronizedReader (TextReader reader)
  103. {
  104. this.reader = reader;
  105. }
  106. public override void Close ()
  107. {
  108. lock (this){
  109. reader.Close ();
  110. }
  111. }
  112. public override int Peek ()
  113. {
  114. lock (this){
  115. return reader.Peek ();
  116. }
  117. }
  118. public override int ReadBlock (char [] buffer, int index, int count)
  119. {
  120. lock (this){
  121. return reader.ReadBlock (buffer, index, count);
  122. }
  123. }
  124. public override string ReadLine ()
  125. {
  126. lock (this){
  127. return reader.ReadLine ();
  128. }
  129. }
  130. public override string ReadToEnd ()
  131. {
  132. lock (this){
  133. return reader.ReadToEnd ();
  134. }
  135. }
  136. #region Read Methods
  137. public override int Read ()
  138. {
  139. lock (this){
  140. return reader.Read ();
  141. }
  142. }
  143. public override int Read (char [] buffer, int index, int count)
  144. {
  145. lock (this){
  146. return reader.Read (buffer, index, count);
  147. }
  148. }
  149. #endregion
  150. }
  151. }