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

FileStream.cs « System.IO « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7180992ca84d65f3fa9a50c127fc0de1d89f3b5d (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
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
330
331
332
333
334
335
336
337
338
339
//
// System.IO/FileStream.cs
//
// Authors:
//   Dietmar Maurer (dietmar@ximian.com)
//   Dan Lewis (dihlewis@yahoo.co.uk)
//
// (C) 2001 Ximian, Inc.  http://www.ximian.com
//

using System;
using System.Runtime.CompilerServices;

// FIXME: emit the correct exceptions everywhere. add error handling.

namespace System.IO
{

	public class FileStream : Stream
	{
		// construct from handle
		
		public FileStream (IntPtr handle, FileAccess access)
			: this (handle, access, true, DefaultBufferSize, false) {}

		public FileStream (IntPtr handle, FileAccess access, bool ownsHandle)
			: this (handle, access, ownsHandle, DefaultBufferSize, false) {}
		
		public FileStream (IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize)
			: this (handle, access, ownsHandle, bufferSize, false) {}
		
		public FileStream (IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize, bool isAsync)
		{
			this.handle = handle;
			this.access = access;
			this.owner = ownsHandle;
			this.async = isAsync;

			InitBuffer (bufferSize);
		}

		// construct from filename
		
		public FileStream (string name, FileMode mode)
			: this (name, mode, FileAccess.ReadWrite, FileShare.ReadWrite, DefaultBufferSize, false) { }

		public FileStream (string name, FileMode mode, FileAccess access)
			: this (name, mode, access, FileShare.ReadWrite, DefaultBufferSize, false) { }

		public FileStream (string name, FileMode mode, FileAccess access, FileShare share)
			: this (name, mode, access, share, DefaultBufferSize, false) { }
		
		public FileStream (string name, FileMode mode, FileAccess access, FileShare share, int bufferSize)
			: this (name, mode, access, share, bufferSize, false) { }

		public FileStream (string name, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool isAsync)
		{
			if (name == null)
				throw new ArgumentNullException ();
			if (name == "" || name.IndexOfAny (Path.InvalidPathChars) != -1)
				throw new ArgumentException ();

			// TODO: demand permissions

			this.handle = MonoIO.Open (name, mode, access, share);
			if (handle == MonoIO.InvalidHandle)
				throw MonoIO.GetException ();
			
			this.access = access;
			this.owner = true;
			this.async = isAsync;
			
			InitBuffer (bufferSize);
		}

		// properties
		
		public override bool CanRead {
			get {
				return access == FileAccess.Read ||
				       access == FileAccess.ReadWrite;
			}
		}

                public override bool CanWrite {
                        get {
				return access == FileAccess.Write ||
				       access == FileAccess.ReadWrite;
                        }
                }

		public override bool CanSeek {
                        get {
                                return true;	// FIXME: false for pipes & streams
                        }
                }

		public override long Length {
			get { return MonoIO.GetLength (handle); }
		}

		public override long Position {
			get { return buf_start + buf_offset; }
			set { Seek (value, SeekOrigin.Begin); }
		}

		public virtual IntPtr Handle {
			get { return handle; }
		}

		// methods

		public override int ReadByte ()
		{
			if (buf_offset >= buf_length) {
				RefillBuffer ();

				if (buf_length == 0)
					return -1;
			}

			return buf [buf_offset ++];
		}

		public override void WriteByte (byte value)
		{
			if (buf_offset == buf_size)
				FlushBuffer ();

			buf [buf_offset ++] = value;
			if (buf_offset > buf_length)
				buf_length = buf_offset;

			buf_dirty = true;
		}

		public override int Read (byte[] dest, int dest_offset, int count)
		{
			int copied = 0;
			while (count > 0) {
				int n = ReadSegment (dest, dest_offset + copied, count);
				copied += n;
				count -= n;

				if (count == 0)
					break;

				if (count > buf_size) {	// shortcut for long reads
					FlushBuffer ();

					MonoIO.Seek (handle, buf_start, SeekOrigin.Begin);
					n = MonoIO.Read (handle, dest, dest_offset + copied, count);

					copied += n;
					buf_start += n;
					break;
				}

				RefillBuffer ();
				if (buf_length == 0)
					break;
			}

			return copied;
		}

		public override void Write (byte[] src, int src_offset, int count)
		{
			int copied = 0;
			while (count > 0) {
				int n = WriteSegment (src, src_offset + copied, count);
				copied += n;
				count -= n;

				if (count == 0)
					break;

				FlushBuffer ();

				if (count > buf_size) {	// shortcut for long writes
					MonoIO.Write (handle, src, src_offset + copied, count);
					buf_start += count;
					break;
				}
			}
		}

		public override long Seek (long offset, SeekOrigin origin)
		{
			long pos;

			// make absolute
		
			switch (origin) {
			case SeekOrigin.End:
				pos = Length - offset;
				break;
			
			case SeekOrigin.Current:
				pos = Position + offset;
				break;
				
			case SeekOrigin.Begin: default:
				pos = offset;
				break;
			}

			if (pos >= buf_start && pos <= buf_start + buf_length) {
				buf_offset = (int) (pos - buf_start);
				return pos;
			}

			FlushBuffer ();
			buf_start = MonoIO.Seek (handle, pos, SeekOrigin.Begin);

			return buf_start;
		}

		public override void SetLength (long length)
		{
			Flush ();
			MonoIO.SetLength (handle, length);
		}

		public override void Flush ()
		{
			FlushBuffer ();
			MonoIO.Flush (handle);
		}

		public override void Close ()
		{
			Dispose (true);
			GC.SuppressFinalize (this);	// remove from finalize queue
		}

		// protected

		~FileStream ()
		{
			Dispose (false);
		}

		protected virtual void Dispose (bool disposing) {
			if (handle != MonoIO.InvalidHandle) {
				Flush ();
				MonoIO.Close (handle);

				handle = MonoIO.InvalidHandle;
			}

			if (disposing)
				buf = null;
		}

		// private

		private int ReadSegment (byte [] dest, int dest_offset, int count)
		{
			if (count > buf_length - buf_offset)
				count = buf_length - buf_offset;

			if (count > 0) {
				Buffer.BlockCopy (buf, buf_offset, dest, dest_offset, count);
				buf_offset += count;
			}

			return count;
		}

		private int WriteSegment (byte [] src, int src_offset, int count)
		{
			if (count > buf_size - buf_offset)
				count = buf_size - buf_offset;

			if (count > 0) {
				Buffer.BlockCopy (src, src_offset, buf, buf_offset, count);
				buf_offset += count;
				if (buf_offset > buf_length)
					buf_length = buf_offset;

				buf_dirty = true;
			}

			return count;
		}

		private void FlushBuffer ()
		{
			if (buf_dirty) {
				MonoIO.Seek (handle, buf_start, SeekOrigin.Begin);
				MonoIO.Write (handle, buf, 0, buf_length);
			}

			buf_start += buf_length;
			buf_offset = buf_length = 0;
			buf_dirty = false;
		}

		private void RefillBuffer ()
		{
			FlushBuffer ();
			
			MonoIO.Seek (handle, buf_start, SeekOrigin.Begin);
			buf_length = MonoIO.Read (handle, buf, 0, buf_size);
		}

		private void InitBuffer (int size)
		{
			if (size < 0)
				throw new ArgumentOutOfRangeException ("Buffer size cannot be negative.");
			if (size < 8)
				size = 8;
		
			buf = new byte [size];
			buf_size = size;
			buf_start = 0;
			buf_offset = buf_length = 0;
			buf_dirty = false;
		}

		// fields

		private static int DefaultBufferSize = 8192;

		private FileAccess access;
		private bool owner;
		private bool async;

		private byte [] buf;			// the buffer
		private int buf_size;			// capacity in bytes
		private int buf_length;			// number of valid bytes in buffer
		private int buf_offset;			// position of next byte
		private bool buf_dirty;			// true if buffer has been written to
		private long buf_start;			// location of buffer in file

		IntPtr handle;				// handle to underlying file
	}
}