StreamWriter.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 closed = false;
  16. private bool iflush;
  17. // new public static readonly StreamWriter Null;
  18. public StreamWriter (Stream stream)
  19. : this (stream, Encoding.UTF8, 0) {}
  20. public StreamWriter (Stream stream, Encoding encoding)
  21. : this (stream, encoding, 0) {}
  22. [MonoTODO("Nothing is done with bufferSize")]
  23. public StreamWriter (Stream stream, Encoding encoding, int bufferSize)
  24. {
  25. if (null == stream)
  26. throw new ArgumentNullException("stream");
  27. if (null == encoding)
  28. throw new ArgumentNullException("encoding");
  29. if (bufferSize < 0)
  30. throw new ArgumentOutOfRangeException("bufferSize");
  31. if (!stream.CanWrite)
  32. throw new ArgumentException("bufferSize");
  33. internalStream = stream;
  34. internalEncoding = encoding;
  35. }
  36. public StreamWriter (string path)
  37. : this (path, false, Encoding.UTF8, 0) {}
  38. public StreamWriter (string path, bool append)
  39. : this (path, append, Encoding.UTF8, 0) {}
  40. public StreamWriter (string path, bool append, Encoding encoding)
  41. : this (path, append, encoding, 0) {}
  42. public StreamWriter (string path, bool append, Encoding encoding, int bufferSize)
  43. {
  44. if (null == path)
  45. throw new ArgumentNullException("path");
  46. if (String.Empty == path)
  47. throw new ArgumentException("path cannot be empty string");
  48. if (null == encoding)
  49. throw new ArgumentNullException("encoding");
  50. if (bufferSize < 0)
  51. throw new ArgumentOutOfRangeException("bufferSize");
  52. FileMode mode;
  53. if (append)
  54. mode = FileMode.Append;
  55. else
  56. mode = FileMode.Create;
  57. internalStream = new FileStream (path, mode, FileAccess.Write);
  58. if (append)
  59. internalStream.Position = internalStream.Length;
  60. else
  61. internalStream.SetLength (0);
  62. internalEncoding = encoding;
  63. }
  64. public virtual bool AutoFlush
  65. {
  66. get {
  67. return iflush;
  68. }
  69. set {
  70. iflush = value;
  71. }
  72. }
  73. public virtual Stream BaseStream
  74. {
  75. get {
  76. return internalStream;
  77. }
  78. }
  79. public override Encoding Encoding
  80. {
  81. get {
  82. return internalEncoding;
  83. }
  84. }
  85. protected override void Dispose (bool disposing)
  86. {
  87. if (disposing && internalStream != null) {
  88. internalStream.Close ();
  89. internalStream = null;
  90. }
  91. }
  92. public override void Flush ()
  93. {
  94. if (closed)
  95. throw new ObjectDisposedException("TextWriter");
  96. internalStream.Flush ();
  97. }
  98. public override void Write (char[] buffer, int index, int count)
  99. {
  100. byte[] res = new byte [internalEncoding.GetMaxByteCount (buffer.Length)];
  101. int len;
  102. len = internalEncoding.GetBytes (buffer, index, count, res, 0);
  103. internalStream.Write (res, 0, len);
  104. if (iflush)
  105. Flush ();
  106. }
  107. public override void Write(string value)
  108. {
  109. Write (value.ToCharArray (), 0, value.Length);
  110. }
  111. public override void Close()
  112. {
  113. Dispose(true);
  114. closed = true;
  115. }
  116. }
  117. }