The Labs \ Source Viewer \ SSCLI \ System.IO \ StringWriter

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. /*============================================================
  16. **
  17. ** Class:  StringWriter
  18. **
  19. ** Purpose: For writing text to a string
  20. **
  21. **
  22. ===========================================================*/
  23. using System;
  24. using System.Text;
  25. using System.Globalization;
  26. namespace System.IO
  27. {
  28.     // This class implements a text writer that writes to a string buffer and allows
  29.     // the resulting sequence of characters to be presented as a string.
  30.     //
  31.     [Serializable()]
  32.     [System.Runtime.InteropServices.ComVisible(true)]
  33.     public class StringWriter : TextWriter
  34.     {
  35.         private static UnicodeEncoding m_encoding = null;
  36.        
  37.         private StringBuilder _sb;
  38.         private bool _isOpen;
  39.        
  40.         // Constructs a new StringWriter. A new StringBuilder is automatically
  41.         // created and associated with the new StringWriter.
  42.         public StringWriter() : this(new StringBuilder(), CultureInfo.CurrentCulture)
  43.         {
  44.         }
  45.        
  46.         public StringWriter(IFormatProvider formatProvider) : this(new StringBuilder(), formatProvider)
  47.         {
  48.         }
  49.        
  50.         // Constructs a new StringWriter that writes to the given StringBuilder.
  51.         //
  52.         public StringWriter(StringBuilder sb) : this(sb, CultureInfo.CurrentCulture)
  53.         {
  54.         }
  55.        
  56.         public StringWriter(StringBuilder sb, IFormatProvider formatProvider) : base(formatProvider)
  57.         {
  58.             if (sb == null)
  59.                 throw new ArgumentNullException("sb", Environment.GetResourceString("ArgumentNull_Buffer"));
  60.             _sb = sb;
  61.             _isOpen = true;
  62.         }
  63.        
  64.         public override void Close()
  65.         {
  66.             Dispose(true);
  67.         }
  68.        
  69.         protected override void Dispose(bool disposing)
  70.         {
  71.             // Do not destroy _sb, so that we can extract this after we are
  72.             // done writing (similar to MemoryStream's GetBuffer & ToArray methods)
  73.             _isOpen = false;
  74.             base.Dispose(disposing);
  75.         }
  76.        
  77.        
  78.         public override Encoding Encoding {
  79.             get {
  80.                 if (m_encoding == null) {
  81.                     m_encoding = new UnicodeEncoding(false, false);
  82.                 }
  83.                 return m_encoding;
  84.             }
  85.         }
  86.        
  87.         // Returns the underlying StringBuilder. This is either the StringBuilder
  88.         // that was passed to the constructor, or the StringBuilder that was
  89.         // automatically created.
  90.         //
  91.         public virtual StringBuilder GetStringBuilder()
  92.         {
  93.             return _sb;
  94.         }
  95.        
  96.         // Writes a character to the underlying string buffer.
  97.         //
  98.         public override void Write(char value)
  99.         {
  100.             if (!_isOpen)
  101.                 __Error.WriterClosed();
  102.             _sb.Append(value);
  103.         }
  104.        
  105.         // Writes a range of a character array to the underlying string buffer.
  106.         // This method will write count characters of data into this
  107.         // StringWriter from the buffer character array starting at position
  108.         // index.
  109.         //
  110.         public override void Write(char[] buffer, int index, int count)
  111.         {
  112.             if (!_isOpen)
  113.                 __Error.WriterClosed();
  114.             if (buffer == null)
  115.                 throw new ArgumentNullException("buffer", Environment.GetResourceString("ArgumentNull_Buffer"));
  116.             if (index < 0)
  117.                 throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
  118.             if (count < 0)
  119.                 throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
  120.             if (buffer.Length - index < count)
  121.                 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
  122.            
  123.             _sb.Append(buffer, index, count);
  124.         }
  125.        
  126.         // Writes a string to the underlying string buffer. If the given string is
  127.         // null, nothing is written.
  128.         //
  129.         public override void Write(string value)
  130.         {
  131.             if (!_isOpen)
  132.                 __Error.WriterClosed();
  133.             if (value != null)
  134.                 _sb.Append(value);
  135.         }
  136.        
  137.         // Returns a string containing the characters written to this TextWriter
  138.         // so far.
  139.         //
  140.         public override string ToString()
  141.         {
  142.             return _sb.ToString();
  143.         }
  144.     }
  145. }

Developer Fusion