1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
//
// System.IO.StreamWriter.cs
//
// Authors:
// Dietmar Maurer (dietmar@ximian.com)
// Paolo Molaro (lupus@ximian.com)
//
// (C) Ximian, Inc. http://www.ximian.com
//
//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Text;
using System;
#if NET_2_0
using System.Runtime.InteropServices;
#endif
namespace System.IO {
[Serializable]
#if NET_2_0
[ComVisible (true)]
#endif
public class StreamWriter : TextWriter {
private Encoding internalEncoding;
private Stream internalStream;
private bool iflush;
private const int DefaultBufferSize = 1024;
private const int DefaultFileBufferSize = 4096;
private const int MinimumBufferSize = 256;
private byte[] byte_buf;
private int byte_pos;
private char[] decode_buf;
private int decode_pos;
private bool DisposedAlready;
private bool preamble_done;
public new static readonly StreamWriter Null = new StreamWriter (Stream.Null, Encoding.UTF8Unmarked, 1);
public StreamWriter (Stream stream)
: this (stream, Encoding.UTF8Unmarked, DefaultBufferSize) {}
public StreamWriter (Stream stream, Encoding encoding)
: this (stream, encoding, DefaultBufferSize) {}
internal void Initialize(Encoding encoding, int bufferSize) {
internalEncoding = encoding;
decode_pos = byte_pos = 0;
int BufferSize = Math.Max(bufferSize, MinimumBufferSize);
decode_buf = new char [BufferSize];
byte_buf = new byte [encoding.GetMaxByteCount (BufferSize)];
// Fixes bug http://bugzilla.ximian.com/show_bug.cgi?id=74513
if (internalStream.CanSeek && internalStream.Position > 0)
preamble_done = true;
}
public StreamWriter (Stream stream, Encoding encoding, int bufferSize) {
if (null == stream)
throw new ArgumentNullException("stream");
if (null == encoding)
throw new ArgumentNullException("encoding");
if (bufferSize <= 0)
throw new ArgumentOutOfRangeException("bufferSize");
if (!stream.CanWrite)
throw new ArgumentException ("Can not write to stream");
internalStream = stream;
Initialize(encoding, bufferSize);
}
public StreamWriter (string path)
: this (path, false, Encoding.UTF8Unmarked, DefaultFileBufferSize) {}
public StreamWriter (string path, bool append)
: this (path, append, Encoding.UTF8Unmarked, DefaultFileBufferSize) {}
public StreamWriter (string path, bool append, Encoding encoding)
: this (path, append, encoding, DefaultFileBufferSize) {}
public StreamWriter (string path, bool append, Encoding encoding, int bufferSize) {
if (null == encoding)
throw new ArgumentNullException("encoding");
if (bufferSize <= 0)
throw new ArgumentOutOfRangeException("bufferSize");
FileMode mode;
if (append)
mode = FileMode.Append;
else
mode = FileMode.Create;
internalStream = new FileStream (path, mode, FileAccess.Write, FileShare.Read);
if (append)
internalStream.Position = internalStream.Length;
else
internalStream.SetLength (0);
Initialize(encoding, bufferSize);
}
public virtual bool AutoFlush {
get {
return iflush;
}
set {
iflush = value;
if (iflush)
Flush ();
}
}
public virtual Stream BaseStream {
get {
return internalStream;
}
}
public override Encoding Encoding {
get {
return internalEncoding;
}
}
protected override void Dispose (bool disposing)
{
Exception exc = null;
if (!DisposedAlready && disposing && internalStream != null) {
try {
Flush();
} catch (Exception e) {
exc = e;
}
DisposedAlready = true;
try {
internalStream.Close ();
} catch (Exception e) {
if (exc == null)
exc = e;
}
}
internalStream = null;
byte_buf = null;
internalEncoding = null;
decode_buf = null;
if (exc != null)
throw exc;
}
public override void Flush ()
{
if (DisposedAlready)
throw new ObjectDisposedException("StreamWriter");
Decode ();
if (byte_pos > 0) {
FlushBytes ();
internalStream.Flush ();
}
}
// how the speedup works:
// the Write () methods simply copy the characters in a buffer of chars (decode_buf)
// Decode () is called when the buffer is full or we need to flash.
// Decode () will use the encoding to get the bytes and but them inside
// byte_buf. From byte_buf the data is finally outputted to the stream.
void FlushBytes ()
{
// write the encoding preamble only at the start of the stream
if (!preamble_done && byte_pos > 0) {
byte[] preamble = internalEncoding.GetPreamble ();
if (preamble.Length > 0)
internalStream.Write (preamble, 0, preamble.Length);
preamble_done = true;
}
internalStream.Write (byte_buf, 0, byte_pos);
byte_pos = 0;
}
void Decode ()
{
if (byte_pos > 0)
FlushBytes ();
if (decode_pos > 0) {
int len = internalEncoding.GetBytes (decode_buf, 0, decode_pos, byte_buf, byte_pos);
byte_pos += len;
decode_pos = 0;
}
}
public override void Write (char[] buffer, int index, int count)
{
if (DisposedAlready)
throw new ObjectDisposedException("StreamWriter");
if (buffer == null)
throw new ArgumentNullException ("buffer");
if (index < 0)
throw new ArgumentOutOfRangeException ("index", "< 0");
if (count < 0)
throw new ArgumentOutOfRangeException ("count", "< 0");
// re-ordered to avoid possible integer overflow
if (index > buffer.Length - count)
throw new ArgumentException ("index + count > buffer.Length");
LowLevelWrite (buffer, index, count);
if (iflush)
Flush();
}
void LowLevelWrite (char[] buffer, int index, int count)
{
while (count > 0) {
int todo = decode_buf.Length - decode_pos;
if (todo == 0) {
Decode ();
todo = decode_buf.Length;
}
if (todo > count)
todo = count;
Buffer.BlockCopy (buffer, index * 2, decode_buf, decode_pos * 2, todo * 2);
count -= todo;
index += todo;
decode_pos += todo;
}
}
void LowLevelWrite (string s)
{
int count = s.Length;
int index = 0;
while (count > 0) {
int todo = decode_buf.Length - decode_pos;
if (todo == 0) {
Decode ();
todo = decode_buf.Length;
}
if (todo > count)
todo = count;
for (int i = 0; i < todo; i ++)
decode_buf [i + decode_pos] = s [i + index];
count -= todo;
index += todo;
decode_pos += todo;
}
}
public override void Write (char value)
{
if (DisposedAlready)
throw new ObjectDisposedException("StreamWriter");
// the size of decode_buf is always > 0 and
// we check for overflow right away
if (decode_pos >= decode_buf.Length)
Decode ();
decode_buf [decode_pos++] = value;
if (iflush)
Flush ();
}
public override void Write (char[] buffer)
{
if (DisposedAlready)
throw new ObjectDisposedException ("StreamWriter");
if (buffer != null)
LowLevelWrite (buffer, 0, buffer.Length);
if (iflush)
Flush ();
}
public override void Write (string value)
{
if (DisposedAlready)
throw new ObjectDisposedException("StreamWriter");
if (value != null)
LowLevelWrite (value);
if (iflush)
Flush ();
}
public override void Close()
{
Dispose (true);
}
~StreamWriter ()
{
Dispose(false);
}
}
}
|