StreamWriter.cs 2.6 KB

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