StreamWriter.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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, true, 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 (encoding == null)
  43. internalEncoding = Encoding.UTF8;
  44. else
  45. internalEncoding = encoding;
  46. }
  47. public virtual bool AutoFlush
  48. {
  49. get {
  50. return iflush;
  51. }
  52. set {
  53. iflush = value;
  54. }
  55. }
  56. public virtual Stream BaseStream
  57. {
  58. get {
  59. return internalStream;
  60. }
  61. }
  62. public override Encoding Encoding
  63. {
  64. get {
  65. return internalEncoding;
  66. }
  67. }
  68. protected override void Dispose( bool disposing )
  69. {
  70. // fixme: implement me
  71. }
  72. public override void Flush ()
  73. {
  74. // fixme: implement me
  75. }
  76. public override void Write (char[] buffer, int index, int count)
  77. {
  78. byte[] res = new byte [internalEncoding.GetMaxByteCount (buffer.Length)];
  79. int len;
  80. len = internalEncoding.GetBytes (buffer, index, count, res, 0);
  81. internalStream.Write (res, 0, len);
  82. }
  83. public override void Write(string value)
  84. {
  85. Write (value.ToCharArray (), 0, value.Length);
  86. }
  87. }
  88. }