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
37 using System.Runtime.InteropServices;
\r
39 namespace System.IO {
\r
43 public class StreamWriter : TextWriter {
\r
45 private Encoding internalEncoding;
\r
47 private Stream internalStream;
\r
49 private const int DefaultBufferSize = 1024;
\r
50 private const int DefaultFileBufferSize = 4096;
\r
51 private const int MinimumBufferSize = 256;
\r
53 private byte[] byte_buf;
\r
54 private char[] decode_buf;
\r
55 private int byte_pos;
\r
56 private int decode_pos;
\r
58 private bool iflush;
\r
59 private bool DisposedAlready;
\r
60 private bool preamble_done;
\r
62 public new static readonly StreamWriter Null = new StreamWriter (Stream.Null, Encoding.UTF8Unmarked, 1);
\r
64 public StreamWriter (Stream stream)
\r
65 : this (stream, Encoding.UTF8Unmarked, DefaultBufferSize) {}
\r
67 public StreamWriter (Stream stream, Encoding encoding)
\r
68 : this (stream, encoding, DefaultBufferSize) {}
\r
70 internal void Initialize(Encoding encoding, int bufferSize) {
\r
71 internalEncoding = encoding;
\r
72 decode_pos = byte_pos = 0;
\r
73 int BufferSize = Math.Max(bufferSize, MinimumBufferSize);
\r
74 decode_buf = new char [BufferSize];
\r
75 byte_buf = new byte [encoding.GetMaxByteCount (BufferSize)];
\r
77 // Fixes bug http://bugzilla.ximian.com/show_bug.cgi?id=74513
\r
78 if (internalStream.CanSeek && internalStream.Position > 0)
\r
79 preamble_done = true;
\r
82 public StreamWriter (Stream stream, Encoding encoding, int bufferSize) {
\r
84 throw new ArgumentNullException("stream");
\r
85 if (null == encoding)
\r
86 throw new ArgumentNullException("encoding");
\r
87 if (bufferSize <= 0)
\r
88 throw new ArgumentOutOfRangeException("bufferSize");
\r
89 if (!stream.CanWrite)
\r
90 throw new ArgumentException ("Can not write to stream");
\r
92 internalStream = stream;
\r
94 Initialize(encoding, bufferSize);
\r
97 public StreamWriter (string path)
\r
98 : this (path, false, Encoding.UTF8Unmarked, DefaultFileBufferSize) {}
\r
100 public StreamWriter (string path, bool append)
\r
101 : this (path, append, Encoding.UTF8Unmarked, DefaultFileBufferSize) {}
\r
103 public StreamWriter (string path, bool append, Encoding encoding)
\r
104 : this (path, append, encoding, DefaultFileBufferSize) {}
\r
106 public StreamWriter (string path, bool append, Encoding encoding, int bufferSize) {
\r
107 if (null == encoding)
\r
108 throw new ArgumentNullException("encoding");
\r
109 if (bufferSize <= 0)
\r
110 throw new ArgumentOutOfRangeException("bufferSize");
\r
115 mode = FileMode.Append;
\r
117 mode = FileMode.Create;
\r
119 internalStream = new FileStream (path, mode, FileAccess.Write, FileShare.Read);
\r
122 internalStream.Position = internalStream.Length;
\r
124 internalStream.SetLength (0);
\r
126 Initialize(encoding, bufferSize);
\r
129 public virtual bool AutoFlush {
\r
140 public virtual Stream BaseStream {
\r
142 return internalStream;
\r
146 public override Encoding Encoding {
\r
148 return internalEncoding;
\r
152 protected override void Dispose (bool disposing)
\r
154 Exception exc = null;
\r
155 if (!DisposedAlready && disposing && internalStream != null) {
\r
158 } catch (Exception e) {
\r
161 DisposedAlready = true;
\r
163 internalStream.Close ();
\r
164 } catch (Exception e) {
\r
170 internalStream = null;
\r
172 internalEncoding = null;
\r
178 public override void Flush ()
\r
180 if (DisposedAlready)
\r
181 throw new ObjectDisposedException("StreamWriter");
\r
184 if (byte_pos > 0) {
\r
186 internalStream.Flush ();
\r
190 // how the speedup works:
\r
191 // the Write () methods simply copy the characters in a buffer of chars (decode_buf)
\r
192 // Decode () is called when the buffer is full or we need to flash.
\r
193 // Decode () will use the encoding to get the bytes and but them inside
\r
194 // byte_buf. From byte_buf the data is finally outputted to the stream.
\r
195 void FlushBytes ()
\r
197 // write the encoding preamble only at the start of the stream
\r
198 if (!preamble_done && byte_pos > 0) {
\r
199 byte[] preamble = internalEncoding.GetPreamble ();
\r
200 if (preamble.Length > 0)
\r
201 internalStream.Write (preamble, 0, preamble.Length);
\r
202 preamble_done = true;
\r
204 internalStream.Write (byte_buf, 0, byte_pos);
\r
212 if (decode_pos > 0) {
\r
213 int len = internalEncoding.GetBytes (decode_buf, 0, decode_pos, byte_buf, byte_pos);
\r
219 public override void Write (char[] buffer, int index, int count)
\r
221 if (DisposedAlready)
\r
222 throw new ObjectDisposedException("StreamWriter");
\r
223 if (buffer == null)
\r
224 throw new ArgumentNullException ("buffer");
\r
226 throw new ArgumentOutOfRangeException ("index", "< 0");
\r
228 throw new ArgumentOutOfRangeException ("count", "< 0");
\r
229 // re-ordered to avoid possible integer overflow
\r
230 if (index > buffer.Length - count)
\r
231 throw new ArgumentException ("index + count > buffer.Length");
\r
233 LowLevelWrite (buffer, index, count);
\r
238 void LowLevelWrite (char[] buffer, int index, int count)
\r
240 while (count > 0) {
\r
241 int todo = decode_buf.Length - decode_pos;
\r
244 todo = decode_buf.Length;
\r
248 Buffer.BlockCopy (buffer, index * 2, decode_buf, decode_pos * 2, todo * 2);
\r
251 decode_pos += todo;
\r
255 void LowLevelWrite (string s)
\r
257 int count = s.Length;
\r
259 while (count > 0) {
\r
260 int todo = decode_buf.Length - decode_pos;
\r
263 todo = decode_buf.Length;
\r
268 for (int i = 0; i < todo; i ++)
\r
269 decode_buf [i + decode_pos] = s [i + index];
\r
273 decode_pos += todo;
\r
277 public override void Write (char value)
\r
279 if (DisposedAlready)
\r
280 throw new ObjectDisposedException("StreamWriter");
\r
282 // the size of decode_buf is always > 0 and
\r
283 // we check for overflow right away
\r
284 if (decode_pos >= decode_buf.Length)
\r
286 decode_buf [decode_pos++] = value;
\r
291 public override void Write (char[] buffer)
\r
293 if (DisposedAlready)
\r
294 throw new ObjectDisposedException ("StreamWriter");
\r
296 if (buffer != null)
\r
297 LowLevelWrite (buffer, 0, buffer.Length);
\r
302 public override void Write (string value)
\r
304 if (DisposedAlready)
\r
305 throw new ObjectDisposedException("StreamWriter");
\r
308 LowLevelWrite (value);
\r
314 public override void Close()
\r