StringWriter.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #if NET_4_5
  36. using System.Threading.Tasks;
  37. #endif
  38. namespace System.IO
  39. {
  40. [Serializable]
  41. [ComVisible (true)]
  42. [MonoLimitation ("Serialization format not compatible with .NET")]
  43. public class StringWriter : TextWriter
  44. {
  45. private StringBuilder internalString;
  46. private bool disposed;
  47. public StringWriter ()
  48. : this (new StringBuilder ())
  49. {
  50. }
  51. public StringWriter (IFormatProvider formatProvider)
  52. : this (new StringBuilder (), formatProvider)
  53. {
  54. }
  55. public StringWriter (StringBuilder sb)
  56. : this (sb, null)
  57. {
  58. }
  59. public StringWriter (StringBuilder sb, IFormatProvider formatProvider)
  60. {
  61. if (sb == null)
  62. throw new ArgumentNullException ("sb");
  63. internalString = sb;
  64. internalFormatProvider = formatProvider;
  65. }
  66. public override Encoding Encoding {
  67. get {
  68. return Encoding.Unicode;
  69. }
  70. }
  71. public override void Close ()
  72. {
  73. Dispose (true);
  74. disposed = true;
  75. }
  76. protected override void Dispose (bool disposing)
  77. {
  78. // MS.NET doesn't clear internal buffer.
  79. // internalString = null;
  80. base.Dispose (disposing);
  81. disposed = true;
  82. }
  83. public virtual StringBuilder GetStringBuilder ()
  84. {
  85. return internalString;
  86. }
  87. public override string ToString ()
  88. {
  89. return internalString.ToString ();
  90. }
  91. public override void Write (char value)
  92. {
  93. if (disposed) {
  94. throw new ObjectDisposedException ("StringReader",
  95. Locale.GetText ("Cannot write to a closed StringWriter"));
  96. }
  97. internalString.Append (value);
  98. }
  99. public override void Write (string value)
  100. {
  101. if (disposed) {
  102. throw new ObjectDisposedException ("StringReader",
  103. Locale.GetText ("Cannot write to a closed StringWriter"));
  104. }
  105. internalString.Append (value);
  106. }
  107. public override void Write (char[] buffer, int index, int count)
  108. {
  109. if (disposed) {
  110. throw new ObjectDisposedException ("StringReader",
  111. Locale.GetText ("Cannot write to a closed StringWriter"));
  112. }
  113. if (buffer == null)
  114. throw new ArgumentNullException ("buffer");
  115. if (index < 0)
  116. throw new ArgumentOutOfRangeException ("index", "< 0");
  117. if (count < 0)
  118. throw new ArgumentOutOfRangeException ("count", "< 0");
  119. // re-ordered to avoid possible integer overflow
  120. if (index > buffer.Length - count)
  121. throw new ArgumentException ("index + count > buffer.Length");
  122. internalString.Append (buffer, index, count);
  123. }
  124. #if NET_4_5
  125. public override Task FlushAsync ()
  126. {
  127. // it appears to do nothing
  128. return TaskConstants.Finished;
  129. }
  130. //
  131. // All async methods return finished task with a result as it's faster
  132. // than setting up async call
  133. //
  134. public override Task WriteAsync (char value)
  135. {
  136. Write (value);
  137. return TaskConstants.Finished;
  138. }
  139. public override Task WriteAsync (char[] buffer, int index, int count)
  140. {
  141. Write (buffer, index, count);
  142. return TaskConstants.Finished;
  143. }
  144. public override Task WriteAsync (string value)
  145. {
  146. Write (value);
  147. return TaskConstants.Finished;
  148. }
  149. public override Task WriteLineAsync (char value)
  150. {
  151. WriteLine (value);
  152. return TaskConstants.Finished;
  153. }
  154. public override Task WriteLineAsync (char[] buffer, int index, int count)
  155. {
  156. WriteLine (buffer, index, count);
  157. return TaskConstants.Finished;
  158. }
  159. public override Task WriteLineAsync (string value)
  160. {
  161. WriteLine (value);
  162. return TaskConstants.Finished;
  163. }
  164. #endif
  165. }
  166. }