2 // System.IO.StreamWriter.cs
\r
5 // Dietmar Maurer (dietmar@ximian.com)
\r
6 // Paolo Molaro (lupus@ximian.com)
\r
8 // (C) Ximian, Inc. http://www.ximian.com
\r
14 namespace System.IO {
\r
17 public class StreamWriter : TextWriter {
\r
19 private Encoding internalEncoding;
\r
21 private Stream internalStream;
\r
22 private bool closed = false;
\r
24 private bool iflush;
\r
26 private const int DefaultBufferSize = 1024;
\r
27 private const int DefaultFileBufferSize = 4096;
\r
28 private const int MinimumBufferSize = 256;
\r
30 private byte[] byte_buf;
\r
31 private int byte_pos;
\r
32 private char[] decode_buf;
\r
33 private int decode_pos;
\r
35 private bool DisposedAlready = false;
\r
36 private bool preamble_done = false;
\r
38 public new static readonly StreamWriter Null = new StreamWriter (Stream.Null, Encoding.UTF8Unmarked, 0);
\r
40 public StreamWriter (Stream stream)
\r
41 : this (stream, Encoding.UTF8Unmarked, DefaultBufferSize) {}
\r
43 public StreamWriter (Stream stream, Encoding encoding)
\r
44 : this (stream, encoding, DefaultBufferSize) {}
\r
46 internal void Initialize(Encoding encoding, int bufferSize) {
\r
47 internalEncoding = encoding;
\r
48 decode_pos = byte_pos = 0;
\r
49 int BufferSize = Math.Max(bufferSize, MinimumBufferSize);
\r
50 decode_buf = new char [BufferSize];
\r
51 byte_buf = new byte [encoding.GetMaxByteCount (BufferSize)];
\r
54 //[MonoTODO("Nothing is done with bufferSize")]
\r
55 public StreamWriter (Stream stream, Encoding encoding, int bufferSize) {
\r
57 throw new ArgumentNullException("stream");
\r
58 if (null == encoding)
\r
59 throw new ArgumentNullException("encoding");
\r
61 throw new ArgumentOutOfRangeException("bufferSize");
\r
62 if (!stream.CanWrite)
\r
63 throw new ArgumentException("Can not write to stream", "stream");
\r
65 internalStream = stream;
\r
67 Initialize(encoding, bufferSize);
\r
70 public StreamWriter (string path)
\r
71 : this (path, false, Encoding.UTF8Unmarked, DefaultFileBufferSize) {}
\r
73 public StreamWriter (string path, bool append)
\r
74 : this (path, append, Encoding.UTF8Unmarked, DefaultFileBufferSize) {}
\r
76 public StreamWriter (string path, bool append, Encoding encoding)
\r
77 : this (path, append, encoding, DefaultFileBufferSize) {}
\r
79 public StreamWriter (string path, bool append, Encoding encoding, int bufferSize) {
\r
81 throw new ArgumentNullException("path");
\r
82 if (String.Empty == path)
\r
83 throw new ArgumentException("path cannot be empty string");
\r
84 if (path.IndexOfAny (Path.InvalidPathChars) != -1)
\r
85 throw new ArgumentException("path contains invalid characters");
\r
87 if (null == encoding)
\r
88 throw new ArgumentNullException("encoding");
\r
90 throw new ArgumentOutOfRangeException("bufferSize");
\r
92 string DirName = Path.GetDirectoryName(path);
\r
93 if (DirName != String.Empty && !Directory.Exists(DirName))
\r
94 throw new DirectoryNotFoundException();
\r
99 mode = FileMode.Append;
\r
101 mode = FileMode.Create;
\r
103 internalStream = new FileStream (path, mode, FileAccess.Write);
\r
106 internalStream.Position = internalStream.Length;
\r
108 internalStream.SetLength (0);
\r
110 Initialize(encoding, bufferSize);
\r
113 public virtual bool AutoFlush {
\r
122 public virtual Stream BaseStream {
\r
124 return internalStream;
\r
128 public override Encoding Encoding {
\r
130 return internalEncoding;
\r
134 protected override void Dispose (bool disposing) {
\r
135 if (!DisposedAlready && disposing && internalStream != null) {
\r
137 DisposedAlready = true;
\r
138 internalStream.Close ();
\r
141 internalStream = null;
\r
143 internalEncoding = null;
\r
147 public override void Flush () {
\r
148 if (DisposedAlready)
\r
149 throw new ObjectDisposedException("StreamWriter");
\r
152 if (byte_pos > 0) {
\r
154 internalStream.Flush ();
\r
158 // how the speedup works:
\r
159 // the Write () methods simply copy the characters in a buffer of chars (decode_buf)
\r
160 // Decode () is called when the buffer is full or we need to flash.
\r
161 // Decode () will use the encoding to get the bytes and but them inside
\r
162 // byte_buf. From byte_buf the data is finally outputted to the stream.
\r
163 void FlushBytes () {
\r
164 // write the encoding preamble only at the start of the stream
\r
165 if (!preamble_done && byte_pos > 0) {
\r
166 byte[] preamble = internalEncoding.GetPreamble ();
\r
167 if (preamble.Length > 0)
\r
168 internalStream.Write (preamble, 0, preamble.Length);
\r
169 preamble_done = true;
\r
171 internalStream.Write (byte_buf, 0, byte_pos);
\r
178 if (decode_pos > 0) {
\r
179 int len = internalEncoding.GetBytes (decode_buf, 0, decode_pos, byte_buf, byte_pos);
\r
185 public override void Write (char[] buffer, int index, int count) {
\r
186 if (DisposedAlready)
\r
187 throw new ObjectDisposedException("StreamWriter");
\r
189 if (buffer == null)
\r
190 throw new ArgumentNullException ("buffer");
\r
191 if (index < 0 || index > buffer.Length)
\r
192 throw new ArgumentOutOfRangeException ("index");
\r
193 if (count < 0 || (index + count) > buffer.Length)
\r
194 throw new ArgumentOutOfRangeException ("count");
\r
196 LowLevelWrite (buffer, index, count);
\r
201 void LowLevelWrite (char[] buffer, int index, int count) {
\r
202 while (count > 0) {
\r
203 int todo = decode_buf.Length - decode_pos;
\r
206 todo = decode_buf.Length;
\r
210 Buffer.BlockCopy (buffer, index * 2, decode_buf, decode_pos * 2, todo * 2);
\r
213 decode_pos += todo;
\r
217 public override void Write (char value)
\r
219 // the size of decode_buf is always > 0 and
\r
220 // we check for overflow right away
\r
221 if (decode_pos >= decode_buf.Length)
\r
223 decode_buf [decode_pos++] = value;
\r
228 public override void Write (char [] value)
\r
230 LowLevelWrite (value, 0, value.Length);
\r
235 public override void Write (string value) {
\r
236 if (DisposedAlready)
\r
237 throw new ObjectDisposedException("StreamWriter");
\r
240 LowLevelWrite (value.ToCharArray (), 0, value.Length);
\r
246 public override void Close()
\r