Files
Dec2017-Script-Dump/mscorlib/System/IO/StreamWriter.cs
T
2026-06-04 11:42:34 +02:00

559 lines
25 KiB
C#

using System;
using System.Runtime.InteropServices;
using System.Text;
namespace System.IO
{
/// <summary>Implements a <see cref="T:System.IO.TextWriter" /> for writing characters to a stream in a particular encoding.</summary>
/// <filterpriority>1</filterpriority>
// Token: 0x020001D0 RID: 464
[ComVisible(true)]
[Serializable]
public class StreamWriter : TextWriter
{
/// <summary>Initializes a new instance of the <see cref="T:System.IO.StreamWriter" /> class for the specified stream, using UTF-8 encoding and the default buffer size.</summary>
/// <param name="stream">The stream to write to. </param>
/// <exception cref="T:System.ArgumentException">
/// <paramref name="stream" /> is not writable. </exception>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="stream" /> is null. </exception>
// Token: 0x060017C2 RID: 6082 RVA: 0x0005B840 File Offset: 0x00059A40
public StreamWriter(Stream stream)
: this(stream, Encoding.UTF8Unmarked, 1024)
{
}
/// <summary>Initializes a new instance of the <see cref="T:System.IO.StreamWriter" /> class for the specified stream, using the specified encoding and the default buffer size.</summary>
/// <param name="stream">The stream to write to. </param>
/// <param name="encoding">The character encoding to use. </param>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="stream" /> or <paramref name="encoding" /> is null. </exception>
/// <exception cref="T:System.ArgumentException">
/// <paramref name="stream" /> is not writable. </exception>
// Token: 0x060017C3 RID: 6083 RVA: 0x0005B854 File Offset: 0x00059A54
public StreamWriter(Stream stream, Encoding encoding)
: this(stream, encoding, 1024)
{
}
/// <summary>Initializes a new instance of the <see cref="T:System.IO.StreamWriter" /> class for the specified stream, using the specified encoding and buffer size.</summary>
/// <param name="stream">The stream to write to. </param>
/// <param name="encoding">The character encoding to use. </param>
/// <param name="bufferSize">Sets the buffer size. </param>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="stream" /> or <paramref name="encoding" /> is null. </exception>
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// <paramref name="bufferSize" /> is negative. </exception>
/// <exception cref="T:System.ArgumentException">
/// <paramref name="stream" /> is not writable. </exception>
// Token: 0x060017C4 RID: 6084 RVA: 0x0005B864 File Offset: 0x00059A64
public StreamWriter(Stream stream, Encoding encoding, int bufferSize)
{
if (stream == null)
{
throw new ArgumentNullException("stream");
}
if (encoding == null)
{
throw new ArgumentNullException("encoding");
}
if (bufferSize <= 0)
{
throw new ArgumentOutOfRangeException("bufferSize");
}
if (!stream.CanWrite)
{
throw new ArgumentException("Can not write to stream");
}
this.internalStream = stream;
this.Initialize(encoding, bufferSize);
}
/// <summary>Initializes a new instance of the <see cref="T:System.IO.StreamWriter" /> class for the specified file on the specified path, using the default encoding and buffer size.</summary>
/// <param name="path">The complete file path to write to. <paramref name="path" /> can be a file name. </param>
/// <exception cref="T:System.UnauthorizedAccessException">Access is denied. </exception>
/// <exception cref="T:System.ArgumentException">
/// <paramref name="path" /> is an empty string(""). -or-<paramref name="path" /> contains the name of a system device (com1, com2, and so on).</exception>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="path" /> is null. </exception>
/// <exception cref="T:System.IO.DirectoryNotFoundException">The specified path is invalid, such as being on an unmapped drive. </exception>
/// <exception cref="T:System.IO.PathTooLongException">The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters. </exception>
/// <exception cref="T:System.IO.IOException">
/// <paramref name="path" /> includes an incorrect or invalid syntax for file name, directory name, or volume label syntax. </exception>
/// <exception cref="T:System.Security.SecurityException">The caller does not have the required permission. </exception>
// Token: 0x060017C5 RID: 6085 RVA: 0x0005B8D0 File Offset: 0x00059AD0
public StreamWriter(string path)
: this(path, false, Encoding.UTF8Unmarked, 4096)
{
}
/// <summary>Initializes a new instance of the <see cref="T:System.IO.StreamWriter" /> class for the specified file on the specified path, using the default encoding and buffer size. If the file exists, it can be either overwritten or appended to. If the file does not exist, this constructor creates a new file.</summary>
/// <param name="path">The complete file path to write to. </param>
/// <param name="append">Determines whether data is to be appended to the file. If the file exists and <paramref name="append" /> is false, the file is overwritten. If the file exists and <paramref name="append" /> is true, the data is appended to the file. Otherwise, a new file is created. </param>
/// <exception cref="T:System.UnauthorizedAccessException">Access is denied. </exception>
/// <exception cref="T:System.ArgumentException">
/// <paramref name="path" /> is empty. -or-<paramref name="path" /> contains the name of a system device (com1, com2, and so on).</exception>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="path" /> is null. </exception>
/// <exception cref="T:System.IO.DirectoryNotFoundException">The specified path is invalid, such as being on an unmapped drive. </exception>
/// <exception cref="T:System.IO.IOException">
/// <paramref name="path" /> includes an incorrect or invalid syntax for file name, directory name, or volume label syntax. </exception>
/// <exception cref="T:System.IO.PathTooLongException">The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters. </exception>
/// <exception cref="T:System.Security.SecurityException">The caller does not have the required permission. </exception>
// Token: 0x060017C6 RID: 6086 RVA: 0x0005B8E4 File Offset: 0x00059AE4
public StreamWriter(string path, bool append)
: this(path, append, Encoding.UTF8Unmarked, 4096)
{
}
/// <summary>Initializes a new instance of the <see cref="T:System.IO.StreamWriter" /> class for the specified file on the specified path, using the specified encoding and default buffer size. If the file exists, it can be either overwritten or appended to. If the file does not exist, this constructor creates a new file.</summary>
/// <param name="path">The complete file path to write to. </param>
/// <param name="append">Determines whether data is to be appended to the file. If the file exists and <paramref name="append" /> is false, the file is overwritten. If the file exists and <paramref name="append" /> is true, the data is appended to the file. Otherwise, a new file is created. </param>
/// <param name="encoding">The character encoding to use. </param>
/// <exception cref="T:System.UnauthorizedAccessException">Access is denied. </exception>
/// <exception cref="T:System.ArgumentException">
/// <paramref name="path" /> is empty. -or-<paramref name="path" /> contains the name of a system device (com1, com2, etc).</exception>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="path" /> is null. </exception>
/// <exception cref="T:System.IO.DirectoryNotFoundException">The specified path is invalid, such as being on an unmapped drive. </exception>
/// <exception cref="T:System.IO.IOException">
/// <paramref name="path" /> includes an incorrect or invalid syntax for file name, directory name, or volume label syntax. </exception>
/// <exception cref="T:System.IO.PathTooLongException">The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters. </exception>
/// <exception cref="T:System.Security.SecurityException">The caller does not have the required permission. </exception>
// Token: 0x060017C7 RID: 6087 RVA: 0x0005B8F8 File Offset: 0x00059AF8
public StreamWriter(string path, bool append, Encoding encoding)
: this(path, append, encoding, 4096)
{
}
/// <summary>Initializes a new instance of the <see cref="T:System.IO.StreamWriter" /> class for the specified file on the specified path, using the specified encoding and buffer size. If the file exists, it can be either overwritten or appended to. If the file does not exist, this constructor creates a new file.</summary>
/// <param name="path">The complete file path to write to. </param>
/// <param name="append">Determines whether data is to be appended to the file. If the file exists and <paramref name="append" /> is false, the file is overwritten. If the file exists and <paramref name="append" /> is true, the data is appended to the file. Otherwise, a new file is created. </param>
/// <param name="encoding">The character encoding to use. </param>
/// <param name="bufferSize">Sets the buffer size. </param>
/// <exception cref="T:System.ArgumentException">
/// <paramref name="path" /> is an empty string (""). -or-<paramref name="path" /> contains the name of a system device (com1, com2, etc).</exception>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="path" /> or <paramref name="encoding" /> is null. </exception>
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// <paramref name="bufferSize" /> is negative. </exception>
/// <exception cref="T:System.IO.IOException">
/// <paramref name="path" /> includes an incorrect or invalid syntax for file name, directory name, or volume label syntax. </exception>
/// <exception cref="T:System.Security.SecurityException">The caller does not have the required permission. </exception>
/// <exception cref="T:System.UnauthorizedAccessException">Access is denied. </exception>
/// <exception cref="T:System.IO.DirectoryNotFoundException">The specified path is invalid, such as being on an unmapped drive. </exception>
/// <exception cref="T:System.IO.PathTooLongException">The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters. </exception>
// Token: 0x060017C8 RID: 6088 RVA: 0x0005B908 File Offset: 0x00059B08
public StreamWriter(string path, bool append, Encoding encoding, int bufferSize)
{
if (encoding == null)
{
throw new ArgumentNullException("encoding");
}
if (bufferSize <= 0)
{
throw new ArgumentOutOfRangeException("bufferSize");
}
FileMode fileMode;
if (append)
{
fileMode = FileMode.Append;
}
else
{
fileMode = FileMode.Create;
}
this.internalStream = new FileStream(path, fileMode, FileAccess.Write, FileShare.Read);
if (append)
{
this.internalStream.Position = this.internalStream.Length;
}
else
{
this.internalStream.SetLength(0L);
}
this.Initialize(encoding, bufferSize);
}
// Token: 0x060017CA RID: 6090 RVA: 0x0005B9AC File Offset: 0x00059BAC
internal void Initialize(Encoding encoding, int bufferSize)
{
this.internalEncoding = encoding;
this.decode_pos = (this.byte_pos = 0);
int num = Math.Max(bufferSize, 256);
this.decode_buf = new char[num];
this.byte_buf = new byte[encoding.GetMaxByteCount(num)];
if (this.internalStream.CanSeek && this.internalStream.Position > 0L)
{
this.preamble_done = true;
}
}
/// <summary>Gets or sets a value indicating whether the <see cref="T:System.IO.StreamWriter" /> will flush its buffer to the underlying stream after every call to <see cref="M:System.IO.StreamWriter.Write(System.Char)" />.</summary>
/// <returns>true to force <see cref="T:System.IO.StreamWriter" /> to flush its buffer; otherwise, false.</returns>
/// <filterpriority>1</filterpriority>
// Token: 0x17000438 RID: 1080
// (get) Token: 0x060017CB RID: 6091 RVA: 0x0005BA24 File Offset: 0x00059C24
// (set) Token: 0x060017CC RID: 6092 RVA: 0x0005BA2C File Offset: 0x00059C2C
public virtual bool AutoFlush
{
get
{
return this.iflush;
}
set
{
this.iflush = value;
if (this.iflush)
{
this.Flush();
}
}
}
/// <summary>Gets the underlying stream that interfaces with a backing store.</summary>
/// <returns>The stream this StreamWriter is writing to.</returns>
/// <filterpriority>2</filterpriority>
// Token: 0x17000439 RID: 1081
// (get) Token: 0x060017CD RID: 6093 RVA: 0x0005BA48 File Offset: 0x00059C48
public virtual Stream BaseStream
{
get
{
return this.internalStream;
}
}
/// <summary>Gets the <see cref="T:System.Text.Encoding" /> in which the output is written.</summary>
/// <returns>The <see cref="T:System.Text.Encoding" /> specified in the constructor for the current instance, or <see cref="T:System.Text.UTF8Encoding" /> if an encoding was not specified.</returns>
/// <filterpriority>2</filterpriority>
// Token: 0x1700043A RID: 1082
// (get) Token: 0x060017CE RID: 6094 RVA: 0x0005BA50 File Offset: 0x00059C50
public override Encoding Encoding
{
get
{
return this.internalEncoding;
}
}
/// <summary>Releases the unmanaged resources used by the <see cref="T:System.IO.StreamWriter" /> and optionally releases the managed resources.</summary>
/// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources. </param>
/// <exception cref="T:System.Text.EncoderFallbackException">The current encoding does not support displaying half of a Unicode surrogate pair.</exception>
// Token: 0x060017CF RID: 6095 RVA: 0x0005BA58 File Offset: 0x00059C58
protected override void Dispose(bool disposing)
{
Exception ex = null;
if (!this.DisposedAlready && disposing && this.internalStream != null)
{
try
{
this.Flush();
}
catch (Exception ex2)
{
ex = ex2;
}
this.DisposedAlready = true;
try
{
this.internalStream.Close();
}
catch (Exception ex3)
{
if (ex == null)
{
ex = ex3;
}
}
}
this.internalStream = null;
this.byte_buf = null;
this.internalEncoding = null;
this.decode_buf = null;
if (ex != null)
{
throw ex;
}
}
/// <summary>Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream.</summary>
/// <exception cref="T:System.ObjectDisposedException">The current writer is closed. </exception>
/// <exception cref="T:System.IO.IOException">An I/O error has occurred. </exception>
/// <exception cref="T:System.Text.EncoderFallbackException">The current encoding does not support displaying half of a Unicode surrogate pair. </exception>
/// <filterpriority>1</filterpriority>
// Token: 0x060017D0 RID: 6096 RVA: 0x0005BB14 File Offset: 0x00059D14
public override void Flush()
{
if (this.DisposedAlready)
{
throw new ObjectDisposedException("StreamWriter");
}
this.Decode();
if (this.byte_pos > 0)
{
this.FlushBytes();
this.internalStream.Flush();
}
}
// Token: 0x060017D1 RID: 6097 RVA: 0x0005BB5C File Offset: 0x00059D5C
private void FlushBytes()
{
if (!this.preamble_done && this.byte_pos > 0)
{
byte[] preamble = this.internalEncoding.GetPreamble();
if (preamble.Length > 0)
{
this.internalStream.Write(preamble, 0, preamble.Length);
}
this.preamble_done = true;
}
this.internalStream.Write(this.byte_buf, 0, this.byte_pos);
this.byte_pos = 0;
}
// Token: 0x060017D2 RID: 6098 RVA: 0x0005BBCC File Offset: 0x00059DCC
private void Decode()
{
if (this.byte_pos > 0)
{
this.FlushBytes();
}
if (this.decode_pos > 0)
{
int bytes = this.internalEncoding.GetBytes(this.decode_buf, 0, this.decode_pos, this.byte_buf, this.byte_pos);
this.byte_pos += bytes;
this.decode_pos = 0;
}
}
/// <summary>Writes a subarray of characters to the stream.</summary>
/// <param name="buffer">A character array containing the data to write. </param>
/// <param name="index">The index into <paramref name="buffer" /> at which to begin writing. </param>
/// <param name="count">The number of characters to read from <paramref name="buffer" />. </param>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="buffer" /> is null. </exception>
/// <exception cref="T:System.ArgumentException">The buffer length minus <paramref name="index" /> is less than <paramref name="count" />. </exception>
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// <paramref name="index" /> or <paramref name="count" /> is negative. </exception>
/// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
/// <exception cref="T:System.ObjectDisposedException">
/// <see cref="P:System.IO.StreamWriter.AutoFlush" /> is true or the <see cref="T:System.IO.StreamWriter" /> buffer is full, and current writer is closed. </exception>
/// <exception cref="T:System.NotSupportedException">
/// <see cref="P:System.IO.StreamWriter.AutoFlush" /> is true or the <see cref="T:System.IO.StreamWriter" /> buffer is full, and the contents of the buffer cannot be written to the underlying fixed size stream because the <see cref="T:System.IO.StreamWriter" /> is at the end the stream. </exception>
/// <filterpriority>1</filterpriority>
// Token: 0x060017D3 RID: 6099 RVA: 0x0005BC34 File Offset: 0x00059E34
public override void Write(char[] buffer, int index, int count)
{
if (this.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");
}
if (index > buffer.Length - count)
{
throw new ArgumentException("index + count > buffer.Length");
}
this.LowLevelWrite(buffer, index, count);
if (this.iflush)
{
this.Flush();
}
}
// Token: 0x060017D4 RID: 6100 RVA: 0x0005BCC8 File Offset: 0x00059EC8
private void LowLevelWrite(char[] buffer, int index, int count)
{
while (count > 0)
{
int num = this.decode_buf.Length - this.decode_pos;
if (num == 0)
{
this.Decode();
num = this.decode_buf.Length;
}
if (num > count)
{
num = count;
}
Buffer.BlockCopy(buffer, index * 2, this.decode_buf, this.decode_pos * 2, num * 2);
count -= num;
index += num;
this.decode_pos += num;
}
}
// Token: 0x060017D5 RID: 6101 RVA: 0x0005BD44 File Offset: 0x00059F44
private void LowLevelWrite(string s)
{
int i = s.Length;
int num = 0;
while (i > 0)
{
int num2 = this.decode_buf.Length - this.decode_pos;
if (num2 == 0)
{
this.Decode();
num2 = this.decode_buf.Length;
}
if (num2 > i)
{
num2 = i;
}
for (int j = 0; j < num2; j++)
{
this.decode_buf[j + this.decode_pos] = s[j + num];
}
i -= num2;
num += num2;
this.decode_pos += num2;
}
}
/// <summary>Writes a character to the stream.</summary>
/// <param name="value">The character to write to the text stream. </param>
/// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
/// <exception cref="T:System.ObjectDisposedException">
/// <see cref="P:System.IO.StreamWriter.AutoFlush" /> is true or the <see cref="T:System.IO.StreamWriter" /> buffer is full, and current writer is closed. </exception>
/// <exception cref="T:System.NotSupportedException">
/// <see cref="P:System.IO.StreamWriter.AutoFlush" /> is true or the <see cref="T:System.IO.StreamWriter" /> buffer is full, and the contents of the buffer cannot be written to the underlying fixed size stream because the <see cref="T:System.IO.StreamWriter" /> is at the end the stream. </exception>
/// <filterpriority>1</filterpriority>
// Token: 0x060017D6 RID: 6102 RVA: 0x0005BDD4 File Offset: 0x00059FD4
public override void Write(char value)
{
if (this.DisposedAlready)
{
throw new ObjectDisposedException("StreamWriter");
}
if (this.decode_pos >= this.decode_buf.Length)
{
this.Decode();
}
this.decode_buf[this.decode_pos++] = value;
if (this.iflush)
{
this.Flush();
}
}
/// <summary>Writes a character array to the stream.</summary>
/// <param name="buffer">A character array containing the data to write. If <paramref name="buffer" /> is null, nothing is written. </param>
/// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
/// <exception cref="T:System.ObjectDisposedException">
/// <see cref="P:System.IO.StreamWriter.AutoFlush" /> is true or the <see cref="T:System.IO.StreamWriter" /> buffer is full, and current writer is closed. </exception>
/// <exception cref="T:System.NotSupportedException">
/// <see cref="P:System.IO.StreamWriter.AutoFlush" /> is true or the <see cref="T:System.IO.StreamWriter" /> buffer is full, and the contents of the buffer cannot be written to the underlying fixed size stream because the <see cref="T:System.IO.StreamWriter" /> is at the end the stream. </exception>
/// <filterpriority>1</filterpriority>
// Token: 0x060017D7 RID: 6103 RVA: 0x0005BE3C File Offset: 0x0005A03C
public override void Write(char[] buffer)
{
if (this.DisposedAlready)
{
throw new ObjectDisposedException("StreamWriter");
}
if (buffer != null)
{
this.LowLevelWrite(buffer, 0, buffer.Length);
}
if (this.iflush)
{
this.Flush();
}
}
/// <summary>Writes a string to the stream.</summary>
/// <param name="value">The string to write to the stream. If <paramref name="value" /> is null, nothing is written. </param>
/// <exception cref="T:System.ObjectDisposedException">
/// <see cref="P:System.IO.StreamWriter.AutoFlush" /> is true or the <see cref="T:System.IO.StreamWriter" /> buffer is full, and current writer is closed. </exception>
/// <exception cref="T:System.NotSupportedException">
/// <see cref="P:System.IO.StreamWriter.AutoFlush" /> is true or the <see cref="T:System.IO.StreamWriter" /> buffer is full, and the contents of the buffer cannot be written to the underlying fixed size stream because the <see cref="T:System.IO.StreamWriter" /> is at the end the stream. </exception>
/// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
/// <filterpriority>1</filterpriority>
// Token: 0x060017D8 RID: 6104 RVA: 0x0005BE84 File Offset: 0x0005A084
public override void Write(string value)
{
if (this.DisposedAlready)
{
throw new ObjectDisposedException("StreamWriter");
}
if (value != null)
{
this.LowLevelWrite(value);
}
if (this.iflush)
{
this.Flush();
}
}
/// <summary>Closes the current StreamWriter object and the underlying stream.</summary>
/// <exception cref="T:System.Text.EncoderFallbackException">The current encoding does not support displaying half of a Unicode surrogate pair.</exception>
/// <filterpriority>1</filterpriority>
// Token: 0x060017D9 RID: 6105 RVA: 0x0005BEC8 File Offset: 0x0005A0C8
public override void Close()
{
this.Dispose(true);
}
/// <summary>Frees the resources of the current <see cref="T:System.IO.StreamWriter" /> before it is reclaimed by the garbage collector.</summary>
// Token: 0x060017DA RID: 6106 RVA: 0x0005BED4 File Offset: 0x0005A0D4
~StreamWriter()
{
this.Dispose(false);
}
// Token: 0x04000711 RID: 1809
private const int DefaultBufferSize = 1024;
// Token: 0x04000712 RID: 1810
private const int DefaultFileBufferSize = 4096;
// Token: 0x04000713 RID: 1811
private const int MinimumBufferSize = 256;
// Token: 0x04000714 RID: 1812
private Encoding internalEncoding;
// Token: 0x04000715 RID: 1813
private Stream internalStream;
// Token: 0x04000716 RID: 1814
private bool iflush;
// Token: 0x04000717 RID: 1815
private byte[] byte_buf;
// Token: 0x04000718 RID: 1816
private int byte_pos;
// Token: 0x04000719 RID: 1817
private char[] decode_buf;
// Token: 0x0400071A RID: 1818
private int decode_pos;
// Token: 0x0400071B RID: 1819
private bool DisposedAlready;
// Token: 0x0400071C RID: 1820
private bool preamble_done;
/// <summary>Provides a StreamWriter with no backing store that can be written to, but not read from.</summary>
/// <filterpriority>1</filterpriority>
// Token: 0x0400071D RID: 1821
public new static readonly StreamWriter Null = new StreamWriter(Stream.Null, Encoding.UTF8Unmarked, 1);
}
}