StringWriter.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. namespace System.IO {
  35. [Serializable]
  36. [MonoTODO ("Fix serialization compatibility with MS.NET")]
  37. public class StringWriter : TextWriter {
  38. private StringBuilder internalString;
  39. private bool disposed = false;
  40. public StringWriter () : this (new StringBuilder ())
  41. {
  42. }
  43. public StringWriter (IFormatProvider formatProvider) :
  44. this (new StringBuilder (), formatProvider)
  45. {
  46. }
  47. public StringWriter (StringBuilder sb) :
  48. this (sb, null)
  49. {
  50. }
  51. public StringWriter (StringBuilder sb, IFormatProvider formatProvider)
  52. {
  53. if (sb == null)
  54. throw new ArgumentNullException ("sb");
  55. internalString = sb;
  56. internalFormatProvider = formatProvider;
  57. }
  58. public override System.Text.Encoding Encoding {
  59. get {
  60. return System.Text.Encoding.Unicode;
  61. }
  62. }
  63. public override void Close ()
  64. {
  65. Dispose (true);
  66. disposed = true;
  67. }
  68. protected override void Dispose (bool disposing)
  69. {
  70. // MS.NET doesn't clear internal buffer.
  71. // internalString = null;
  72. base.Dispose (disposing);
  73. disposed = true;
  74. }
  75. public virtual StringBuilder GetStringBuilder ()
  76. {
  77. return internalString;
  78. }
  79. public override string ToString ()
  80. {
  81. return internalString.ToString ();
  82. }
  83. public override void Write (char value)
  84. {
  85. if (disposed) {
  86. throw new ObjectDisposedException ("StringReader",
  87. Locale.GetText ("Cannot write to a closed StringWriter"));
  88. }
  89. internalString.Append (value);
  90. }
  91. public override void Write (string 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 (char[] buffer, int index, int count)
  100. {
  101. if (disposed) {
  102. throw new ObjectDisposedException ("StringReader",
  103. Locale.GetText ("Cannot write to a closed StringWriter"));
  104. }
  105. if (buffer == null)
  106. throw new ArgumentNullException ("buffer");
  107. if (index < 0)
  108. throw new ArgumentOutOfRangeException ("index", "< 0");
  109. if (count < 0)
  110. throw new ArgumentOutOfRangeException ("count", "< 0");
  111. // re-ordered to avoid possible integer overflow
  112. if (index > buffer.Length - count)
  113. throw new ArgumentException ("index + count > buffer.Length");
  114. internalString.Append (buffer, index, count);
  115. }
  116. }
  117. }