StringWriter.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // System.IO.StringWriter
  3. //
  4. // Authors
  5. // Marcin Szczepanski ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. // Marek Safar ([email protected])
  8. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. // Copyright 2011 Xamarin Inc.
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System.Globalization;
  33. using System.Text;
  34. using System.Runtime.InteropServices;
  35. using System.Threading.Tasks;
  36. namespace System.IO
  37. {
  38. [Serializable]
  39. [ComVisible (true)]
  40. [MonoLimitation ("Serialization format not compatible with .NET")]
  41. public class StringWriter : TextWriter
  42. {
  43. private StringBuilder internalString;
  44. private bool disposed;
  45. public StringWriter ()
  46. : this (new StringBuilder ())
  47. {
  48. }
  49. public StringWriter (IFormatProvider formatProvider)
  50. : this (new StringBuilder (), formatProvider)
  51. {
  52. }
  53. public StringWriter (StringBuilder sb)
  54. : this (sb, null)
  55. {
  56. }
  57. public StringWriter (StringBuilder sb, IFormatProvider formatProvider)
  58. {
  59. if (sb == null)
  60. throw new ArgumentNullException ("sb");
  61. internalString = sb;
  62. internalFormatProvider = formatProvider;
  63. }
  64. public override Encoding Encoding {
  65. get {
  66. return Encoding.Unicode;
  67. }
  68. }
  69. public override void Close ()
  70. {
  71. Dispose (true);
  72. disposed = true;
  73. }
  74. protected override void Dispose (bool disposing)
  75. {
  76. // MS.NET doesn't clear internal buffer.
  77. // internalString = null;
  78. base.Dispose (disposing);
  79. disposed = true;
  80. }
  81. public virtual StringBuilder GetStringBuilder ()
  82. {
  83. return internalString;
  84. }
  85. public override string ToString ()
  86. {
  87. return internalString.ToString ();
  88. }
  89. public override void Write (char value)
  90. {
  91. if (disposed) {
  92. throw new ObjectDisposedException ("StringReader",
  93. Locale.GetText ("Cannot write to a closed StringWriter"));
  94. }
  95. internalString.Append (value);
  96. }
  97. public override void Write (string value)
  98. {
  99. if (disposed) {
  100. throw new ObjectDisposedException ("StringReader",
  101. Locale.GetText ("Cannot write to a closed StringWriter"));
  102. }
  103. internalString.Append (value);
  104. }
  105. public override void Write (char[] buffer, int index, int count)
  106. {
  107. if (disposed) {
  108. throw new ObjectDisposedException ("StringReader",
  109. Locale.GetText ("Cannot write to a closed StringWriter"));
  110. }
  111. if (buffer == null)
  112. throw new ArgumentNullException ("buffer");
  113. if (index < 0)
  114. throw new ArgumentOutOfRangeException ("index", "< 0");
  115. if (count < 0)
  116. throw new ArgumentOutOfRangeException ("count", "< 0");
  117. // re-ordered to avoid possible integer overflow
  118. if (index > buffer.Length - count)
  119. throw new ArgumentException ("index + count > buffer.Length");
  120. internalString.Append (buffer, index, count);
  121. }
  122. public override Task FlushAsync ()
  123. {
  124. // it appears to do nothing
  125. return TaskConstants.Finished;
  126. }
  127. //
  128. // All async methods return finished task with a result as it's faster
  129. // than setting up async call
  130. //
  131. public override Task WriteAsync (char value)
  132. {
  133. Write (value);
  134. return TaskConstants.Finished;
  135. }
  136. public override Task WriteAsync (char[] buffer, int index, int count)
  137. {
  138. Write (buffer, index, count);
  139. return TaskConstants.Finished;
  140. }
  141. public override Task WriteAsync (string value)
  142. {
  143. Write (value);
  144. return TaskConstants.Finished;
  145. }
  146. public override Task WriteLineAsync (char value)
  147. {
  148. WriteLine (value);
  149. return TaskConstants.Finished;
  150. }
  151. public override Task WriteLineAsync (char[] buffer, int index, int count)
  152. {
  153. WriteLine (buffer, index, count);
  154. return TaskConstants.Finished;
  155. }
  156. public override Task WriteLineAsync (string value)
  157. {
  158. WriteLine (value);
  159. return TaskConstants.Finished;
  160. }
  161. }
  162. }