StreamWriter.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // System.IO.StreamWriter.cs
  3. //
  4. // Author:
  5. // Dietmar Maurer ([email protected])
  6. //
  7. // (C) Ximian, Inc. http://www.ximian.com
  8. //
  9. using System.Text;
  10. namespace System.IO {
  11. [Serializable]
  12. public class StreamWriter : TextWriter {
  13. private Encoding internalEncoding;
  14. private Stream internalStream;
  15. private bool iflush;
  16. // new public static readonly StreamWriter Null;
  17. public StreamWriter (Stream stream)
  18. : this (stream, null, 0) {}
  19. public StreamWriter (Stream stream, Encoding encoding)
  20. : this (stream, encoding, 0) {}
  21. public StreamWriter (Stream stream, Encoding encoding, int bufferSize)
  22. {
  23. internalStream = stream;
  24. if (encoding == null)
  25. internalEncoding = Encoding.UTF8;
  26. else
  27. internalEncoding = encoding;
  28. }
  29. public StreamWriter (string path)
  30. : this (path, false, null, 0) {}
  31. public StreamWriter (string path, bool append)
  32. : this (path, append, null, 0) {}
  33. public StreamWriter (string path, bool append, Encoding encoding)
  34. : this (path, append, encoding, 0) {}
  35. public StreamWriter (string path, bool append, Encoding encoding, int bufferSize)
  36. {
  37. FileMode mode;
  38. if (append)
  39. mode = FileMode.Append;
  40. else
  41. mode = FileMode.Create;
  42. internalStream = new FileStream (path, mode, FileAccess.Write);
  43. if (append)
  44. internalStream.Position = internalStream.Length;
  45. else
  46. internalStream.SetLength (0);
  47. if (encoding == null)
  48. internalEncoding = Encoding.UTF8;
  49. else
  50. internalEncoding = encoding;
  51. }
  52. public virtual bool AutoFlush
  53. {
  54. get {
  55. return iflush;
  56. }
  57. set {
  58. iflush = value;
  59. }
  60. }
  61. public virtual Stream BaseStream
  62. {
  63. get {
  64. return internalStream;
  65. }
  66. }
  67. public override Encoding Encoding
  68. {
  69. get {
  70. return internalEncoding;
  71. }
  72. }
  73. protected override void Dispose (bool disposing)
  74. {
  75. if (disposing && internalStream != null) {
  76. internalStream.Close ();
  77. internalStream = null;
  78. }
  79. }
  80. public override void Flush ()
  81. {
  82. internalStream.Flush ();
  83. }
  84. public override void Write (char[] buffer, int index, int count)
  85. {
  86. byte[] res = new byte [internalEncoding.GetMaxByteCount (buffer.Length)];
  87. int len;
  88. len = internalEncoding.GetBytes (buffer, index, count, res, 0);
  89. internalStream.Write (res, 0, len);
  90. if (iflush)
  91. Flush ();
  92. }
  93. public override void Write(string value)
  94. {
  95. Write (value.ToCharArray (), 0, value.Length);
  96. }
  97. }
  98. }