StringWriter.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // System.IO.StringWriter
  3. //
  4. // Authors
  5. // Marcin Szczepanski ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Copyright (C) 2004 Novell (http://www.novell.com)
  9. //
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  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. namespace System.IO {
  36. [Serializable]
  37. [ComVisible (true)]
  38. [MonoLimitation ("Serialization format not compatible with .NET")]
  39. public class StringWriter : TextWriter {
  40. private StringBuilder internalString;
  41. private bool disposed = false;
  42. public StringWriter () : this (new StringBuilder ())
  43. {
  44. }
  45. public StringWriter (IFormatProvider formatProvider) :
  46. this (new StringBuilder (), formatProvider)
  47. {
  48. }
  49. public StringWriter (StringBuilder sb) :
  50. this (sb, null)
  51. {
  52. }
  53. public StringWriter (StringBuilder sb, IFormatProvider formatProvider)
  54. {
  55. if (sb == null)
  56. throw new ArgumentNullException ("sb");
  57. internalString = sb;
  58. internalFormatProvider = formatProvider;
  59. }
  60. public override System.Text.Encoding Encoding {
  61. get {
  62. return System.Text.Encoding.Unicode;
  63. }
  64. }
  65. public override void Close ()
  66. {
  67. Dispose (true);
  68. disposed = true;
  69. }
  70. protected override void Dispose (bool disposing)
  71. {
  72. // MS.NET doesn't clear internal buffer.
  73. // internalString = null;
  74. base.Dispose (disposing);
  75. disposed = true;
  76. }
  77. public virtual StringBuilder GetStringBuilder ()
  78. {
  79. return internalString;
  80. }
  81. public override string ToString ()
  82. {
  83. return internalString.ToString ();
  84. }
  85. public override void Write (char value)
  86. {
  87. if (disposed) {
  88. throw new ObjectDisposedException ("StringReader",
  89. Locale.GetText ("Cannot write to a closed StringWriter"));
  90. }
  91. internalString.Append (value);
  92. }
  93. public override void Write (string value)
  94. {
  95. if (disposed) {
  96. throw new ObjectDisposedException ("StringReader",
  97. Locale.GetText ("Cannot write to a closed StringWriter"));
  98. }
  99. internalString.Append (value);
  100. }
  101. public override void Write (char[] buffer, int index, int count)
  102. {
  103. if (disposed) {
  104. throw new ObjectDisposedException ("StringReader",
  105. Locale.GetText ("Cannot write to a closed StringWriter"));
  106. }
  107. if (buffer == null)
  108. throw new ArgumentNullException ("buffer");
  109. if (index < 0)
  110. throw new ArgumentOutOfRangeException ("index", "< 0");
  111. if (count < 0)
  112. throw new ArgumentOutOfRangeException ("count", "< 0");
  113. // re-ordered to avoid possible integer overflow
  114. if (index > buffer.Length - count)
  115. throw new ArgumentException ("index + count > buffer.Length");
  116. internalString.Append (buffer, index, count);
  117. }
  118. }
  119. }