Updates referencesource to .NET 4.7
[mono-project.git] / mcs / class / referencesource / mscorlib / system / io / stringwriter.cs
blobf464f5c1a3363e8548742b1ab93c1db0df8867d3
1 // ==++==
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 //
5 // ==--==
6 /*============================================================
7 **
8 ** Class: StringWriter
9 **
10 ** <OWNER>Microsoft</OWNER>
12 ** Purpose: For writing text to a string
15 ===========================================================*/
17 using System;
18 using System.Runtime;
19 using System.Text;
20 using System.Globalization;
21 using System.Diagnostics.Contracts;
22 using System.Runtime.InteropServices;
23 using System.Security.Permissions;
24 #if FEATURE_ASYNC_IO
25 using System.Threading.Tasks;
26 #endif
28 namespace System.IO {
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.
32 [Serializable]
33 [ComVisible(true)]
34 public class StringWriter : TextWriter
36 private static volatile UnicodeEncoding m_encoding=null;
38 private StringBuilder _sb;
39 private bool _isOpen;
41 // Constructs a new StringWriter. A new StringBuilder is automatically
42 // created and associated with the new StringWriter.
43 public 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.
53 //
54 public StringWriter(StringBuilder sb) : this(sb, CultureInfo.CurrentCulture) {
57 public StringWriter(StringBuilder sb, IFormatProvider formatProvider) : base(formatProvider) {
58 if (sb==null)
59 throw new ArgumentNullException("sb", Environment.GetResourceString("ArgumentNull_Buffer"));
60 Contract.EndContractBlock();
61 _sb = sb;
62 _isOpen = true;
65 public override void Close()
67 Dispose(true);
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)
74 _isOpen = false;
75 base.Dispose(disposing);
79 public override Encoding Encoding {
80 get {
81 if (m_encoding==null) {
82 m_encoding = new UnicodeEncoding(false, false);
84 return m_encoding;
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() {
93 return _sb;
96 // Writes a character to the underlying string buffer.
98 public override void Write(char value) {
99 if (!_isOpen)
100 __Error.WriterClosed();
101 _sb.Append(value);
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
107 // index.
109 public override void Write(char[] buffer, int index, int count) {
110 if (buffer==null)
111 throw new ArgumentNullException("buffer", Environment.GetResourceString("ArgumentNull_Buffer"));
112 if (index < 0)
113 throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
114 if (count < 0)
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();
120 if (!_isOpen)
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) {
130 if (!_isOpen)
131 __Error.WriterClosed();
132 if (value != null) _sb.Append(value);
136 #if FEATURE_ASYNC_IO
137 #region Task based Async APIs
138 [HostProtection(ExternalThreading = true)]
139 [ComVisible(false)]
140 public override Task WriteAsync(char value)
142 Write(value);
143 return Task.CompletedTask;
146 [HostProtection(ExternalThreading = true)]
147 [ComVisible(false)]
148 public override Task WriteAsync(String value)
150 Write(value);
151 return Task.CompletedTask;
154 [HostProtection(ExternalThreading = true)]
155 [ComVisible(false)]
156 public override Task WriteAsync(char[] buffer, int index, int count)
158 Write(buffer, index, count);
159 return Task.CompletedTask;
162 [HostProtection(ExternalThreading = true)]
163 [ComVisible(false)]
164 public override Task WriteLineAsync(char value)
166 WriteLine(value);
167 return Task.CompletedTask;
170 [HostProtection(ExternalThreading = true)]
171 [ComVisible(false)]
172 public override Task WriteLineAsync(String value)
174 WriteLine(value);
175 return Task.CompletedTask;
178 [HostProtection(ExternalThreading = true)]
179 [ComVisible(false)]
180 public override Task WriteLineAsync(char[] buffer, int index, int count)
182 WriteLine(buffer, index, count);
183 return Task.CompletedTask;
186 [HostProtection(ExternalThreading = true)]
187 [ComVisible(false)]
188 public override Task FlushAsync()
190 return Task.CompletedTask;
192 #endregion
193 #endif //FEATURE_ASYNC_IO
195 // Returns a string containing the characters written to this TextWriter
196 // so far.
198 public override String ToString() {
199 return _sb.ToString();