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
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
\r
14 // Permission is hereby granted, free of charge, to any person obtaining
\r
15 // a copy of this software and associated documentation files (the
\r
16 // "Software"), to deal in the Software without restriction, including
\r
17 // without limitation the rights to use, copy, modify, merge, publish,
\r
18 // distribute, sublicense, and/or sell copies of the Software, and to
\r
19 // permit persons to whom the Software is furnished to do so, subject to
\r
20 // the following conditions:
\r
22 // The above copyright notice and this permission notice shall be
\r
23 // included in all copies or substantial portions of the Software.
\r
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
38 using System.Runtime.InteropServices;
\r
41 namespace System.IO {
\r
47 public class StreamWriter : TextWriter {
\r
49 private Encoding internalEncoding;
\r
51 private Stream internalStream;
\r
52 private bool closed = false;
\r
54 private bool iflush;
\r
56 private const int DefaultBufferSize = 1024;
\r
57 private const int DefaultFileBufferSize = 4096;
\r
58 private const int MinimumBufferSize = 256;
\r
60 private byte[] byte_buf;
\r
61 private int byte_pos;
\r
62 private char[] decode_buf;
\r
63 private int decode_pos;
\r
65 private bool DisposedAlready = false;
\r
66 private bool preamble_done = false;
\r
68 public new static readonly StreamWriter Null = new StreamWriter (Stream.Null, Encoding.UTF8Unmarked, 0);
\r
70 public StreamWriter (Stream stream)
\r
71 : this (stream, Encoding.UTF8Unmarked, DefaultBufferSize) {}
\r
73 public StreamWriter (Stream stream, Encoding encoding)
\r
74 : this (stream, encoding, DefaultBufferSize) {}
\r
76 internal void Initialize(Encoding encoding, int bufferSize) {
\r
77 internalEncoding = encoding;
\r
78 decode_pos = byte_pos = 0;
\r
79 int BufferSize = Math.Max(bufferSize, MinimumBufferSize);
\r
80 decode_buf = new char [BufferSize];
\r
81 byte_buf = new byte [encoding.GetMaxByteCount (BufferSize)];
\r
83 // Fixes bug http://bugzilla.ximian.com/show_bug.cgi?id=74513
\r
84 if (internalStream.CanSeek && internalStream.Position > 0)
\r
85 preamble_done = true;
\r
88 //[MonoTODO("Nothing is done with bufferSize")]
\r
89 public StreamWriter (Stream stream, Encoding encoding, int bufferSize) {
\r
91 throw new ArgumentNullException("stream");
\r
92 if (null == encoding)
\r
93 throw new ArgumentNullException("encoding");
\r
95 throw new ArgumentOutOfRangeException("bufferSize");
\r
96 if (!stream.CanWrite)
\r
97 throw new ArgumentException("Can not write to stream", "stream");
\r
99 internalStream = stream;
\r
101 Initialize(encoding, bufferSize);
\r
104 public StreamWriter (string path)
\r
105 : this (path, false, Encoding.UTF8Unmarked, DefaultFileBufferSize) {}
\r
107 public StreamWriter (string path, bool append)
\r
108 : this (path, append, Encoding.UTF8Unmarked, DefaultFileBufferSize) {}
\r
110 public StreamWriter (string path, bool append, Encoding encoding)
\r
111 : this (path, append, encoding, DefaultFileBufferSize) {}
\r
113 public StreamWriter (string path, bool append, Encoding encoding, int bufferSize) {
\r
115 throw new ArgumentNullException("path");
\r
116 if (String.Empty == path)
\r
117 throw new ArgumentException("path cannot be empty string");
\r
118 if (path.IndexOfAny (Path.InvalidPathChars) != -1)
\r
119 throw new ArgumentException("path contains invalid characters");
\r
121 if (null == encoding)
\r
122 throw new ArgumentNullException("encoding");
\r
123 if (bufferSize < 0)
\r
124 throw new ArgumentOutOfRangeException("bufferSize");
\r
126 string DirName = Path.GetDirectoryName(path);
\r
127 if (DirName != String.Empty && !Directory.Exists(DirName))
\r
128 throw new DirectoryNotFoundException();
\r
133 mode = FileMode.Append;
\r
135 mode = FileMode.Create;
\r
137 internalStream = new FileStream (path, mode, FileAccess.Write, FileShare.Read);
\r
140 internalStream.Position = internalStream.Length;
\r
142 internalStream.SetLength (0);
\r
144 Initialize(encoding, bufferSize);
\r
147 public virtual bool AutoFlush {
\r
152 if (DisposedAlready)
\r
153 throw new ObjectDisposedException("StreamWriter");
\r
162 public virtual Stream BaseStream {
\r
164 return internalStream;
\r
168 public override Encoding Encoding {
\r
170 return internalEncoding;
\r
174 protected override void Dispose (bool disposing)
\r
176 if (!DisposedAlready && disposing && internalStream != null) {
\r
178 DisposedAlready = true;
\r
179 internalStream.Close ();
\r
182 internalStream = null;
\r
184 internalEncoding = null;
\r
188 public override void Flush ()
\r
190 if (DisposedAlready)
\r
191 throw new ObjectDisposedException("StreamWriter");
\r
194 if (byte_pos > 0) {
\r
196 internalStream.Flush ();
\r
200 // how the speedup works:
\r
201 // the Write () methods simply copy the characters in a buffer of chars (decode_buf)
\r
202 // Decode () is called when the buffer is full or we need to flash.
\r
203 // Decode () will use the encoding to get the bytes and but them inside
\r
204 // byte_buf. From byte_buf the data is finally outputted to the stream.
\r
205 void FlushBytes ()
\r
207 // write the encoding preamble only at the start of the stream
\r
208 if (!preamble_done && byte_pos > 0) {
\r
209 byte[] preamble = internalEncoding.GetPreamble ();
\r
210 if (preamble.Length > 0)
\r
211 internalStream.Write (preamble, 0, preamble.Length);
\r
212 preamble_done = true;
\r
214 internalStream.Write (byte_buf, 0, byte_pos);
\r
222 if (decode_pos > 0) {
\r
223 int len = internalEncoding.GetBytes (decode_buf, 0, decode_pos, byte_buf, byte_pos);
\r
229 public override void Write (char[] buffer, int index, int count)
\r
231 if (DisposedAlready)
\r
232 throw new ObjectDisposedException("StreamWriter");
\r
233 if (buffer == null)
\r
234 throw new ArgumentNullException ("buffer");
\r
236 throw new ArgumentOutOfRangeException ("index", "< 0");
\r
238 throw new ArgumentOutOfRangeException ("count", "< 0");
\r
239 // re-ordered to avoid possible integer overflow
\r
240 if (index > buffer.Length - count)
\r
241 throw new ArgumentException ("index + count > buffer.Length");
\r
243 LowLevelWrite (buffer, index, count);
\r
248 void LowLevelWrite (char[] buffer, int index, int count)
\r
250 while (count > 0) {
\r
251 int todo = decode_buf.Length - decode_pos;
\r
254 todo = decode_buf.Length;
\r
258 Buffer.BlockCopy (buffer, index * 2, decode_buf, decode_pos * 2, todo * 2);
\r
261 decode_pos += todo;
\r
265 void LowLevelWrite (string s)
\r
267 int count = s.Length;
\r
269 while (count > 0) {
\r
270 int todo = decode_buf.Length - decode_pos;
\r
273 todo = decode_buf.Length;
\r
278 for (int i = 0; i < todo; i ++)
\r
279 decode_buf [i + decode_pos] = s [i + index];
\r
283 decode_pos += todo;
\r
287 public override void Write (char value)
\r
289 if (DisposedAlready)
\r
290 throw new ObjectDisposedException("StreamWriter");
\r
292 // the size of decode_buf is always > 0 and
\r
293 // we check for overflow right away
\r
294 if (decode_pos >= decode_buf.Length)
\r
296 decode_buf [decode_pos++] = value;
\r
301 public override void Write (char[] value)
\r
303 if (DisposedAlready)
\r
304 throw new ObjectDisposedException("StreamWriter");
\r
307 LowLevelWrite (value, 0, value.Length);
\r
312 public override void Write (string value)
\r
314 if (DisposedAlready)
\r
315 throw new ObjectDisposedException("StreamWriter");
\r
318 LowLevelWrite (value);
\r
324 public override void Close()
\r