mscorlib(4.0.0.0) API with additions
StringWriter.cs
4 using System.Text;
6 
7 namespace System.IO
8 {
10  [Serializable]
11  [ComVisible(true)]
12  [__DynamicallyInvokable]
13  public class StringWriter : TextWriter
14  {
15  private static volatile UnicodeEncoding m_encoding;
16 
17  private StringBuilder _sb;
18 
19  private bool _isOpen;
20 
23  [__DynamicallyInvokable]
24  public override Encoding Encoding
25  {
26  [__DynamicallyInvokable]
27  get
28  {
29  if (m_encoding == null)
30  {
31  m_encoding = new UnicodeEncoding(bigEndian: false, byteOrderMark: false);
32  }
33  return m_encoding;
34  }
35  }
36 
38  [__DynamicallyInvokable]
39  public StringWriter()
41  {
42  }
43 
46  [__DynamicallyInvokable]
47  public StringWriter(IFormatProvider formatProvider)
48  : this(new StringBuilder(), formatProvider)
49  {
50  }
51 
56  [__DynamicallyInvokable]
58  : this(sb, CultureInfo.CurrentCulture)
59  {
60  }
61 
67  [__DynamicallyInvokable]
68  public StringWriter(StringBuilder sb, IFormatProvider formatProvider)
69  : base(formatProvider)
70  {
71  if (sb == null)
72  {
73  throw new ArgumentNullException("sb", Environment.GetResourceString("ArgumentNull_Buffer"));
74  }
75  _sb = sb;
76  _isOpen = true;
77  }
78 
80  public override void Close()
81  {
82  Dispose(disposing: true);
83  }
84 
88  [__DynamicallyInvokable]
89  protected override void Dispose(bool disposing)
90  {
91  _isOpen = false;
92  base.Dispose(disposing);
93  }
94 
97  [__DynamicallyInvokable]
99  {
100  return _sb;
101  }
102 
106  [__DynamicallyInvokable]
107  public override void Write(char value)
108  {
109  if (!_isOpen)
110  {
111  __Error.WriterClosed();
112  }
113  _sb.Append(value);
114  }
115 
126  [__DynamicallyInvokable]
127  public override void Write(char[] buffer, int index, int count)
128  {
129  if (buffer == null)
130  {
131  throw new ArgumentNullException("buffer", Environment.GetResourceString("ArgumentNull_Buffer"));
132  }
133  if (index < 0)
134  {
135  throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
136  }
137  if (count < 0)
138  {
139  throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
140  }
141  if (buffer.Length - index < count)
142  {
143  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
144  }
145  if (!_isOpen)
146  {
147  __Error.WriterClosed();
148  }
149  _sb.Append(buffer, index, count);
150  }
151 
155  [__DynamicallyInvokable]
156  public override void Write(string value)
157  {
158  if (!_isOpen)
159  {
160  __Error.WriterClosed();
161  }
162  if (value != null)
163  {
164  _sb.Append(value);
165  }
166  }
167 
173  [ComVisible(false)]
174  [__DynamicallyInvokable]
175  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
176  public override Task WriteAsync(char value)
177  {
178  Write(value);
179  return Task.CompletedTask;
180  }
181 
187  [ComVisible(false)]
188  [__DynamicallyInvokable]
189  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
190  public override Task WriteAsync(string value)
191  {
192  Write(value);
193  return Task.CompletedTask;
194  }
195 
208  [ComVisible(false)]
209  [__DynamicallyInvokable]
210  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
211  public override Task WriteAsync(char[] buffer, int index, int count)
212  {
213  Write(buffer, index, count);
214  return Task.CompletedTask;
215  }
216 
222  [ComVisible(false)]
223  [__DynamicallyInvokable]
224  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
225  public override Task WriteLineAsync(char value)
226  {
227  WriteLine(value);
228  return Task.CompletedTask;
229  }
230 
236  [ComVisible(false)]
237  [__DynamicallyInvokable]
238  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
239  public override Task WriteLineAsync(string value)
240  {
241  WriteLine(value);
242  return Task.CompletedTask;
243  }
244 
257  [ComVisible(false)]
258  [__DynamicallyInvokable]
259  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
260  public override Task WriteLineAsync(char[] buffer, int index, int count)
261  {
262  WriteLine(buffer, index, count);
263  return Task.CompletedTask;
264  }
265 
268  [ComVisible(false)]
269  [__DynamicallyInvokable]
270  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
271  public override Task FlushAsync()
272  {
273  return Task.CompletedTask;
274  }
275 
278  [__DynamicallyInvokable]
279  public override string ToString()
280  {
281  return _sb.ToString();
282  }
283  }
284 }
Represents a character encoding.To browse the .NET Framework source code for this type,...
Definition: Encoding.cs:15
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
override Task WriteLineAsync(char[] buffer, int index, int count)
Writes a subarray of characters followed by a line terminator asynchronously to the string.
unsafe override string ToString()
Converts the value of this instance to a T:System.String.
Implements a T:System.IO.TextWriter for writing information to a string. The information is stored in...
Definition: StringWriter.cs:13
override Task WriteAsync(char[] buffer, int index, int count)
Writes a subarray of characters to the string asynchronously.
override Task WriteLineAsync(char value)
Writes a character followed by a line terminator asynchronously to the string.
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
override void Write(char value)
Writes a character to the string.
Provides a mechanism for retrieving an object to control formatting.
void Dispose()
Releases all resources used by the T:System.IO.TextWriter object.
Definition: TextWriter.cs:507
StringWriter(IFormatProvider formatProvider)
Initializes a new instance of the T:System.IO.StringWriter class with the specified format control.
Definition: StringWriter.cs:47
override Task FlushAsync()
Asynchronously clears all buffers for the current writer and causes any buffered data to be written t...
StringWriter()
Initializes a new instance of the T:System.IO.StringWriter class.
Definition: StringWriter.cs:39
SecurityAction
Specifies the security actions that can be performed using declarative security.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
StringBuilder Append(char value, int repeatCount)
Appends a specified number of copies of the string representation of a Unicode character to this inst...
override string ToString()
Returns a string containing the characters written to the current StringWriter so far.
Represents a UTF-16 encoding of Unicode characters.
Represents a writer that can write a sequential series of characters. This class is abstract.
Definition: TextWriter.cs:15
override Task WriteAsync(char value)
Writes a character to the string asynchronously.
override void Dispose(bool disposing)
Releases the unmanaged resources used by the T:System.IO.StringWriter and optionally releases the man...
Definition: StringWriter.cs:89
static Task CompletedTask
Gets a task that has already completed successfully.
Definition: Task.cs:1453
override Task WriteLineAsync(string value)
Writes a string followed by a line terminator asynchronously to the current string.
StringWriter(StringBuilder sb, IFormatProvider formatProvider)
Initializes a new instance of the T:System.IO.StringWriter class that writes to the specified T:Syste...
Definition: StringWriter.cs:68
Represents a mutable string of characters. This class cannot be inherited.To browse the ....
The exception that is thrown when one of the arguments provided to a method is not valid.
StringWriter(StringBuilder sb)
Initializes a new instance of the T:System.IO.StringWriter class that writes to the specified T:Syste...
Definition: StringWriter.cs:57
virtual StringBuilder GetStringBuilder()
Returns the underlying T:System.Text.StringBuilder.
Definition: StringWriter.cs:98
Specifies that the class can be serialized.
override void Write(string value)
Writes a string to the current string.
virtual void WriteLine()
Writes a line terminator to the text string or stream.
Definition: TextWriter.cs:778
override void Write(char[] buffer, int index, int count)
Writes a subarray of characters to the string.
Compare strings using culture-sensitive sort rules and the current culture.
override void Close()
Closes the current T:System.IO.StringWriter and the underlying stream.
Definition: StringWriter.cs:80
override Task WriteAsync(string value)
Writes a string to the current string asynchronously.
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
Represents an asynchronous operation that can return a value.
Definition: Task.cs:18