3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 /*============================================================
10 ** <OWNER>Microsoft</OWNER>
12 ** Purpose: For writing text to a string
15 ===========================================================*/
20 using System
.Globalization
;
21 using System
.Diagnostics
.Contracts
;
22 using System
.Runtime
.InteropServices
;
23 using System
.Security
.Permissions
;
25 using System
.Threading
.Tasks
;
29 // This class implements a text writer that writes to a string buffer and allows
30 // the resulting sequence of characters to be presented as a string.
34 public class StringWriter
: TextWriter
36 private static volatile UnicodeEncoding m_encoding
=null;
38 private StringBuilder _sb
;
41 // Constructs a new StringWriter. A new StringBuilder is automatically
42 // created and associated with the new StringWriter.
44 : this(new StringBuilder(), CultureInfo
.CurrentCulture
)
48 public StringWriter(IFormatProvider formatProvider
)
49 : this(new StringBuilder(), formatProvider
) {
52 // Constructs a new StringWriter that writes to the given StringBuilder.
54 public StringWriter(StringBuilder sb
) : this(sb
, CultureInfo
.CurrentCulture
) {
57 public StringWriter(StringBuilder sb
, IFormatProvider formatProvider
) : base(formatProvider
) {
59 throw new ArgumentNullException("sb", Environment
.GetResourceString("ArgumentNull_Buffer"));
60 Contract
.EndContractBlock();
65 public override void Close()
70 protected override void Dispose(bool disposing
)
72 // Do not destroy _sb, so that we can extract this after we are
73 // done writing (similar to MemoryStream's GetBuffer & ToArray methods)
75 base.Dispose(disposing
);
79 public override Encoding Encoding
{
81 if (m_encoding
==null) {
82 m_encoding
= new UnicodeEncoding(false, false);
88 // Returns the underlying StringBuilder. This is either the StringBuilder
89 // that was passed to the constructor, or the StringBuilder that was
90 // automatically created.
92 public virtual StringBuilder
GetStringBuilder() {
96 // Writes a character to the underlying string buffer.
98 public override void Write(char value) {
100 __Error
.WriterClosed();
104 // Writes a range of a character array to the underlying string buffer.
105 // This method will write count characters of data into this
106 // StringWriter from the buffer character array starting at position
109 public override void Write(char[] buffer
, int index
, int count
) {
111 throw new ArgumentNullException("buffer", Environment
.GetResourceString("ArgumentNull_Buffer"));
113 throw new ArgumentOutOfRangeException("index", Environment
.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
115 throw new ArgumentOutOfRangeException("count", Environment
.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
116 if (buffer
.Length
- index
< count
)
117 throw new ArgumentException(Environment
.GetResourceString("Argument_InvalidOffLen"));
118 Contract
.EndContractBlock();
121 __Error
.WriterClosed();
123 _sb
.Append(buffer
, index
, count
);
126 // Writes a string to the underlying string buffer. If the given string is
127 // null, nothing is written.
129 public override void Write(String
value) {
131 __Error
.WriterClosed();
132 if (value != null) _sb
.Append(value);
137 #region Task based Async APIs
138 [HostProtection(ExternalThreading
= true)]
140 public override Task
WriteAsync(char value)
143 return Task
.CompletedTask
;
146 [HostProtection(ExternalThreading
= true)]
148 public override Task
WriteAsync(String
value)
151 return Task
.CompletedTask
;
154 [HostProtection(ExternalThreading
= true)]
156 public override Task
WriteAsync(char[] buffer
, int index
, int count
)
158 Write(buffer
, index
, count
);
159 return Task
.CompletedTask
;
162 [HostProtection(ExternalThreading
= true)]
164 public override Task
WriteLineAsync(char value)
167 return Task
.CompletedTask
;
170 [HostProtection(ExternalThreading
= true)]
172 public override Task
WriteLineAsync(String
value)
175 return Task
.CompletedTask
;
178 [HostProtection(ExternalThreading
= true)]
180 public override Task
WriteLineAsync(char[] buffer
, int index
, int count
)
182 WriteLine(buffer
, index
, count
);
183 return Task
.CompletedTask
;
186 [HostProtection(ExternalThreading
= true)]
188 public override Task
FlushAsync()
190 return Task
.CompletedTask
;
193 #endif //FEATURE_ASYNC_IO
195 // Returns a string containing the characters written to this TextWriter
198 public override String
ToString() {
199 return _sb
.ToString();