Welcome to mirror list, hosted at ThFree Co, Russian Federation.

StreamWriter.cs « System.IO « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b4103e3b53f86d621ca531e12e70818fc765a6fd (plain)
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
//
// System.IO.StreamWriter.cs
//
// Author:
//   Dietmar Maurer (dietmar@ximian.com)
//
// (C) Ximian, Inc.  http://www.ximian.com
//

using System.Text;
using System;

namespace System.IO {
	
	[Serializable]
	public class StreamWriter : TextWriter {

		private Encoding internalEncoding;

		private Stream internalStream;
		private bool closed = false;

		private bool iflush;
		
		private const int DefaultBufferSize = 1024;
		private const int DefaultFileBufferSize = 4096;
		private const int MinimumBufferSize = 2;

		private int pos;
		private int BufferSize;
		private byte[] TheBuffer;

		private bool DisposedAlready = false;

		// new public static readonly StreamWriter Null;

		public StreamWriter (Stream stream)
			: this (stream, Encoding.UTF8, DefaultBufferSize) {}

		public StreamWriter (Stream stream, Encoding encoding)
			: this (stream, encoding, DefaultBufferSize) {}

		protected void Initialize(Encoding encoding, int bufferSize) {
			internalEncoding = encoding;
			pos = 0;
			BufferSize = Math.Max(bufferSize, MinimumBufferSize);
			TheBuffer = new byte[BufferSize];
		}

		//[MonoTODO("Nothing is done with bufferSize")]
		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("bufferSize");

			internalStream = stream;

			Initialize(encoding, bufferSize);
		}

		public StreamWriter (string path)
			: this (path, false, Encoding.UTF8, DefaultFileBufferSize) {}

		public StreamWriter (string path, bool append)
			: this (path, append, Encoding.UTF8, 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 == path)
				throw new ArgumentNullException("path");
			if (String.Empty == path)
				throw new ArgumentException("path cannot be empty string");
			if (path.IndexOfAny (Path.InvalidPathChars) != -1)
				throw new ArgumentException("path contains invalid characters");

			if (null == encoding)
				throw new ArgumentNullException("encoding");
			if (bufferSize < 0)
				throw new ArgumentOutOfRangeException("bufferSize");

			string DirName = Path.GetDirectoryName(path);
			if (DirName != String.Empty && !Directory.Exists(DirName))
				throw new DirectoryNotFoundException();

			FileMode mode;

			if (append)
				mode = FileMode.Append;
			else
				mode = FileMode.Create;
			
			internalStream = new FileStream (path, mode, FileAccess.Write);

			if (append)
				internalStream.Position = internalStream.Length;
			else
				internalStream.SetLength (0);

			Initialize(encoding, bufferSize);
		}

		public virtual bool AutoFlush {
			get {
				return iflush;
			}
			set {
				iflush = value;
			}
		}

		public virtual Stream BaseStream {
			get {
				return internalStream;
			}
		}

		public override Encoding Encoding {
			get {
				return internalEncoding;
			}
		}

		public void Dispose() {
			Dispose(true);
			// Take yourself off of the Finalization queue 
			// to prevent finalization code for this object
			// from executing a second time.
			GC.SuppressFinalize(this);
		}

		protected override void Dispose (bool disposing) {
			if (!DisposedAlready && disposing && internalStream != null) {
				Flush();
				internalStream.Close ();
				internalStream = null;
				TheBuffer = null;
			}
			DisposedAlready = true;
		}

		public override void Flush () {
			if (DisposedAlready)
				throw new ObjectDisposedException("StreamWriter");

			if (pos > 0) {
				internalStream.Write (TheBuffer, 0, pos);
				internalStream.Flush ();
				pos = 0;
			}
		}
		
		public override void Write (char[] buffer, int index, int count) {
			if (DisposedAlready)
				throw new ObjectDisposedException("StreamWriter");

			byte[] res = new byte [internalEncoding.GetMaxByteCount (buffer.Length)];
			int len;
			int BytesToBuffer;
			int resPos = 0;

			len = internalEncoding.GetBytes (buffer, index, count, res, 0);

			// if they want AutoFlush, don't bother buffering
			if (iflush) {
				Flush();
				internalStream.Write (res, 0, len);
				internalStream.Flush ();
			} else {
				// otherwise use the buffer.
				// NOTE: this logic is not optimized for performance.
				while (resPos < len) {
					// fill the buffer if we've got more bytes than will fit
					BytesToBuffer = Math.Min(BufferSize - pos, len - resPos);
					Array.Copy(res, resPos, TheBuffer, pos, BytesToBuffer);
					resPos += BytesToBuffer;
					pos += BytesToBuffer;
					// if the buffer is full, flush it out.
					if (pos == BufferSize) Flush();
				}
			}
		}

		public override void Write(string value) {
			if (DisposedAlready)
				throw new ObjectDisposedException("StreamWriter");

			if (value != null)
				Write (value.ToCharArray (), 0, value.Length);
		}

		public override void Close() {
			Dispose();
		}

		~StreamWriter() {
			Dispose(false);
		}
	}
}