Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
FileStream.cs
Go to the documentation of this file.
7
8namespace System.IO;
9
10public class FileStream : Stream
11{
12 private readonly FileStreamStrategy _strategy;
13
14 [Obsolete("FileStream.Handle has been deprecated. Use FileStream's SafeFileHandle property instead.")]
15 public virtual IntPtr Handle => _strategy.Handle;
16
17 public override bool CanRead => _strategy.CanRead;
18
19 public override bool CanWrite => _strategy.CanWrite;
20
22
23 public virtual string Name => _strategy.Name;
24
25 public virtual bool IsAsync => _strategy.IsAsync;
26
27 public override long Length
28 {
29 get
30 {
32 {
34 }
35 else if (!CanSeek)
36 {
38 }
39 return _strategy.Length;
40 }
41 }
42
43 public override long Position
44 {
45 get
46 {
48 {
50 }
51 else if (!CanSeek)
52 {
54 }
55 return _strategy.Position;
56 }
57 set
58 {
59 if (value < 0)
60 {
62 }
64 }
65 }
66
67 public override bool CanSeek => _strategy.CanSeek;
68
69 [EditorBrowsable(EditorBrowsableState.Never)]
70 [Obsolete("This constructor has been deprecated. Use FileStream(SafeFileHandle handle, FileAccess access) instead.")]
72 : this(handle, access, ownsHandle: true, 4096, isAsync: false)
73 {
74 }
75
76 [EditorBrowsable(EditorBrowsableState.Never)]
77 [Obsolete("This constructor has been deprecated. Use FileStream(SafeFileHandle handle, FileAccess access) and optionally make a new SafeFileHandle with ownsHandle=false if needed instead.")]
78 public FileStream(IntPtr handle, FileAccess access, bool ownsHandle)
79 : this(handle, access, ownsHandle, 4096, isAsync: false)
80 {
81 }
82
83 [EditorBrowsable(EditorBrowsableState.Never)]
84 [Obsolete("This constructor has been deprecated. Use FileStream(SafeFileHandle handle, FileAccess access, int bufferSize) and optionally make a new SafeFileHandle with ownsHandle=false if needed instead.")]
85 public FileStream(IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize)
86 : this(handle, access, ownsHandle, bufferSize, isAsync: false)
87 {
88 }
89
90 [EditorBrowsable(EditorBrowsableState.Never)]
91 [Obsolete("This constructor has been deprecated. Use FileStream(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) and optionally make a new SafeFileHandle with ownsHandle=false if needed instead.")]
92 public FileStream(IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize, bool isAsync)
93 {
94 SafeFileHandle safeFileHandle = new SafeFileHandle(handle, ownsHandle);
95 try
96 {
97 ValidateHandle(safeFileHandle, access, bufferSize, isAsync);
98 _strategy = FileStreamHelpers.ChooseStrategy(this, safeFileHandle, access, bufferSize, isAsync);
99 }
100 catch
101 {
102 GC.SuppressFinalize(safeFileHandle);
103 throw;
104 }
105 }
106
107 private static void ValidateHandle(SafeFileHandle handle, FileAccess access, int bufferSize)
108 {
109 if (handle.IsInvalid)
110 {
111 throw new ArgumentException(SR.Arg_InvalidHandle, "handle");
112 }
113 if (access < FileAccess.Read || access > FileAccess.ReadWrite)
114 {
116 }
117 if (bufferSize < 0)
118 {
120 }
121 else if (handle.IsClosed)
122 {
124 }
125 }
126
127 private static void ValidateHandle(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync)
128 {
129 ValidateHandle(handle, access, bufferSize);
130 if (isAsync && !handle.IsAsync)
131 {
133 }
134 else if (!isAsync && handle.IsAsync)
135 {
137 }
138 }
139
141 : this(handle, access, 4096)
142 {
143 }
144
145 public FileStream(SafeFileHandle handle, FileAccess access, int bufferSize)
146 {
147 ValidateHandle(handle, access, bufferSize);
148 _strategy = FileStreamHelpers.ChooseStrategy(this, handle, access, bufferSize, handle.IsAsync);
149 }
150
151 public FileStream(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync)
152 {
153 ValidateHandle(handle, access, bufferSize, isAsync);
154 _strategy = FileStreamHelpers.ChooseStrategy(this, handle, access, bufferSize, isAsync);
155 }
156
157 public FileStream(string path, FileMode mode)
158 : this(path, mode, (mode == FileMode.Append) ? FileAccess.Write : FileAccess.ReadWrite, FileShare.Read, 4096, useAsync: false)
159 {
160 }
161
162 public FileStream(string path, FileMode mode, FileAccess access)
163 : this(path, mode, access, FileShare.Read, 4096, useAsync: false)
164 {
165 }
166
167 public FileStream(string path, FileMode mode, FileAccess access, FileShare share)
168 : this(path, mode, access, share, 4096, useAsync: false)
169 {
170 }
171
172 public FileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize)
173 : this(path, mode, access, share, bufferSize, useAsync: false)
174 {
175 }
176
177 public FileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync)
178 : this(path, mode, access, share, bufferSize, useAsync ? FileOptions.Asynchronous : FileOptions.None)
179 {
180 }
181
182 public FileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options)
183 : this(path, mode, access, share, bufferSize, options, 0L)
184 {
185 }
186
188 {
189 if (path == null)
190 {
191 throw new ArgumentNullException("path", SR.ArgumentNull_Path);
192 }
193 if (path.Length == 0)
194 {
195 throw new ArgumentException(SR.Argument_EmptyPath, "path");
196 }
197 if (options == null)
198 {
199 throw new ArgumentNullException("options");
200 }
201 if ((options.Access & FileAccess.Read) != 0 && options.Mode == FileMode.Append)
202 {
203 throw new ArgumentException(SR.Argument_InvalidAppendMode, "options");
204 }
205 if ((options.Access & FileAccess.Write) == 0 && (options.Mode == FileMode.Truncate || options.Mode == FileMode.CreateNew || options.Mode == FileMode.Create || options.Mode == FileMode.Append))
206 {
208 }
209 if (options.PreallocationSize > 0)
210 {
212 }
214 _strategy = FileStreamHelpers.ChooseStrategy(this, path, options.Mode, options.Access, options.Share, options.BufferSize, options.Options, options.PreallocationSize);
215 }
216
217 private FileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options, long preallocationSize)
218 {
219 FileStreamHelpers.ValidateArguments(path, mode, access, share, bufferSize, options, preallocationSize);
220 _strategy = FileStreamHelpers.ChooseStrategy(this, path, mode, access, share, bufferSize, options, preallocationSize);
221 }
222
223 [UnsupportedOSPlatform("ios")]
224 [UnsupportedOSPlatform("macos")]
225 [UnsupportedOSPlatform("tvos")]
226 public virtual void Lock(long position, long length)
227 {
228 if (position < 0 || length < 0)
229 {
230 ThrowHelper.ThrowArgumentOutOfRangeException_NeedNonNegNum((position < 0) ? "position" : "length");
231 }
232 else if (_strategy.IsClosed)
233 {
235 }
236 _strategy.Lock(position, length);
237 }
238
239 [UnsupportedOSPlatform("ios")]
240 [UnsupportedOSPlatform("macos")]
241 [UnsupportedOSPlatform("tvos")]
242 public virtual void Unlock(long position, long length)
243 {
244 if (position < 0 || length < 0)
245 {
246 ThrowHelper.ThrowArgumentOutOfRangeException_NeedNonNegNum((position < 0) ? "position" : "length");
247 }
248 else if (_strategy.IsClosed)
249 {
251 }
252 _strategy.Unlock(position, length);
253 }
254
256 {
257 if (cancellationToken.IsCancellationRequested)
258 {
260 }
262 {
264 }
266 }
267
268 public override int Read(byte[] buffer, int offset, int count)
269 {
271 return _strategy.Read(buffer, offset, count);
272 }
273
274 public override int Read(Span<byte> buffer)
275 {
276 return _strategy.Read(buffer);
277 }
278
296
313
314 public override void Write(byte[] buffer, int offset, int count)
315 {
318 }
319
320 public override void Write(ReadOnlySpan<byte> buffer)
321 {
323 }
324
342
359
360 public override void Flush()
361 {
362 Flush(flushToDisk: false);
363 }
364
365 public virtual void Flush(bool flushToDisk)
366 {
368 {
370 }
371 _strategy.Flush(flushToDisk);
372 }
373
382
383 public override void SetLength(long value)
384 {
385 if (value < 0)
386 {
388 }
389 else if (_strategy.IsClosed)
390 {
392 }
393 else if (!CanSeek)
394 {
396 }
397 else if (!CanWrite)
398 {
400 }
402 }
403
404 public override int ReadByte()
405 {
406 return _strategy.ReadByte();
407 }
408
409 public override void WriteByte(byte value)
410 {
412 }
413
414 protected override void Dispose(bool disposing)
415 {
416 _strategy.DisposeInternal(disposing);
417 }
418
419 internal void DisposeInternal(bool disposing)
420 {
421 Dispose(disposing);
422 }
423
424 public override ValueTask DisposeAsync()
425 {
426 return _strategy.DisposeAsync();
427 }
428
429 public override void CopyTo(Stream destination, int bufferSize)
430 {
432 _strategy.CopyTo(destination, bufferSize);
433 }
434
440
441 public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
442 {
445 {
447 }
448 else if (!CanRead)
449 {
451 }
452 return _strategy.BeginRead(buffer, offset, count, callback, state);
453 }
454
455 public override int EndRead(IAsyncResult asyncResult)
456 {
457 if (asyncResult == null)
458 {
459 throw new ArgumentNullException("asyncResult");
460 }
462 }
463
464 public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
465 {
468 {
470 }
471 else if (!CanWrite)
472 {
474 }
475 return _strategy.BeginWrite(buffer, offset, count, callback, state);
476 }
477
478 public override void EndWrite(IAsyncResult asyncResult)
479 {
480 if (asyncResult == null)
481 {
482 throw new ArgumentNullException("asyncResult");
483 }
485 }
486
487 public override long Seek(long offset, SeekOrigin origin)
488 {
489 return _strategy.Seek(offset, origin);
490 }
491
493 {
494 return base.FlushAsync(cancellationToken);
495 }
496
498 {
499 return base.Read(buffer);
500 }
501
503 {
504 return base.ReadAsync(buffer, offset, count, cancellationToken);
505 }
506
511
513 {
514 base.Write(buffer);
515 }
516
518 {
519 return base.WriteAsync(buffer, offset, count, cancellationToken);
520 }
521
526
528 {
529 return base.DisposeAsync();
530 }
531
533 {
534 return base.CopyToAsync(destination, bufferSize, cancellationToken);
535 }
536
537 internal IAsyncResult BaseBeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
538 {
539 return base.BeginRead(buffer, offset, count, callback, state);
540 }
541
543 {
544 return base.EndRead(asyncResult);
545 }
546
547 internal IAsyncResult BaseBeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
548 {
549 return base.BeginWrite(buffer, offset, count, callback, state);
550 }
551
553 {
554 base.EndWrite(asyncResult);
555 }
556}
static void SuppressFinalize(object obj)
Definition GC.cs:202
Definition GC.cs:8
int BaseEndRead(IAsyncResult asyncResult)
Task BaseCopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
int BaseRead(Span< byte > buffer)
override long Length
Definition FileStream.cs:28
void DisposeInternal(bool disposing)
override bool CanRead
Definition FileStream.cs:17
FileStream(SafeFileHandle handle, FileAccess access)
readonly FileStreamStrategy _strategy
Definition FileStream.cs:12
virtual SafeFileHandle SafeFileHandle
Definition FileStream.cs:21
override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
virtual bool IsAsync
Definition FileStream.cs:25
override ValueTask WriteAsync(ReadOnlyMemory< byte > buffer, CancellationToken cancellationToken=default(CancellationToken))
override bool CanWrite
Definition FileStream.cs:19
FileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options, long preallocationSize)
override Task< int > ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
override void EndWrite(IAsyncResult asyncResult)
static void ValidateHandle(SafeFileHandle handle, FileAccess access, int bufferSize)
override void Flush()
FileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options)
override bool CanSeek
Definition FileStream.cs:67
virtual void Unlock(long position, long length)
void BaseWrite(ReadOnlySpan< byte > buffer)
FileStream(SafeFileHandle handle, FileAccess access, int bufferSize)
ValueTask BaseDisposeAsync()
override void Dispose(bool disposing)
FileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize)
FileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync)
FileStream(string path, FileStreamOptions options)
virtual string Name
Definition FileStream.cs:23
override ValueTask< int > ReadAsync(Memory< byte > buffer, CancellationToken cancellationToken=default(CancellationToken))
override void CopyTo(Stream destination, int bufferSize)
Task< int > BaseReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
override void SetLength(long value)
override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
override int Read(Span< byte > buffer)
ValueTask< int > BaseReadAsync(Memory< byte > buffer, CancellationToken cancellationToken=default(CancellationToken))
void ValidateReadWriteArgs(byte[] buffer, int offset, int count)
FileStream(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync)
override void WriteByte(byte value)
Task BaseFlushAsync(CancellationToken cancellationToken)
FileStream(IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize)
Definition FileStream.cs:85
Task BaseWriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
override int EndRead(IAsyncResult asyncResult)
override ValueTask DisposeAsync()
FileStream(string path, FileMode mode, FileAccess access)
override long Seek(long offset, SeekOrigin origin)
static void ValidateHandle(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync)
override Task FlushAsync(CancellationToken cancellationToken)
override int ReadByte()
FileStream(IntPtr handle, FileAccess access)
Definition FileStream.cs:71
FileStream(IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize, bool isAsync)
Definition FileStream.cs:92
override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
override void Write(byte[] buffer, int offset, int count)
override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
FileStream(IntPtr handle, FileAccess access, bool ownsHandle)
Definition FileStream.cs:78
virtual void Flush(bool flushToDisk)
virtual IntPtr Handle
Definition FileStream.cs:15
override int Read(byte[] buffer, int offset, int count)
ValueTask BaseWriteAsync(ReadOnlyMemory< byte > buffer, CancellationToken cancellationToken=default(CancellationToken))
IAsyncResult BaseBeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
FileStream(string path, FileMode mode)
override void Write(ReadOnlySpan< byte > buffer)
void BaseEndWrite(IAsyncResult asyncResult)
FileStream(string path, FileMode mode, FileAccess access, FileShare share)
IAsyncResult BaseBeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
virtual void Lock(long position, long length)
static void ValidateArguments(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options, long preallocationSize)
static FileStreamStrategy ChooseStrategy(FileStream fileStream, SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync)
static void SerializationGuard(FileAccess access)
static void ValidateArgumentsForPreallocation(FileMode mode, FileAccess access)
void Unlock(long position, long length)
void Lock(long position, long length)
Task FlushAsync()
Definition Stream.cs:669
virtual int ReadByte()
Definition Stream.cs:994
static void ValidateBufferArguments(byte[] buffer, int offset, int count)
Definition Stream.cs:1044
void SetLength(long value)
long Seek(long offset, SeekOrigin origin)
static void ValidateCopyToArguments(Stream destination, int bufferSize)
Definition Stream.cs:1060
Task WriteAsync(byte[] buffer, int offset, int count)
Definition Stream.cs:914
virtual void EndWrite(IAsyncResult asyncResult)
Definition Stream.cs:889
int Read(byte[] buffer, int offset, int count)
void CopyTo(Stream destination)
Definition Stream.cs:540
void Dispose()
Definition Stream.cs:639
Task< int > ReadAsync(byte[] buffer, int offset, int count)
Definition Stream.cs:762
void Write(byte[] buffer, int offset, int count)
virtual IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
Definition Stream.cs:688
virtual void WriteByte(byte value)
Definition Stream.cs:1020
virtual IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
Definition Stream.cs:813
virtual int EndRead(IAsyncResult asyncResult)
Definition Stream.cs:737
Task CopyToAsync(Stream destination)
Definition Stream.cs:571
virtual ValueTask DisposeAsync()
Definition Stream.cs:654
static string Arg_InvalidHandle
Definition SR.cs:14
static string ArgumentOutOfRange_Enum
Definition SR.cs:18
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string ArgumentNull_Path
Definition SR.cs:954
static string Argument_InvalidFileModeAndAccessCombo
Definition SR.cs:686
static string Argument_EmptyPath
Definition SR.cs:38
static string Argument_InvalidAppendMode
Definition SR.cs:634
Definition SR.cs:7
static Task FromCanceled(CancellationToken cancellationToken)
Definition Task.cs:3363
static void ThrowNotSupportedException_UnwritableStream()
static void ThrowArgumentOutOfRangeException(System.ExceptionArgument argument)
static void ThrowArgumentOutOfRangeException_NeedNonNegNum(string paramName)
static void ThrowArgumentException_HandleNotSync(string paramName)
static void ThrowNotSupportedException_UnseekableStream()
static void ThrowArgumentException_HandleNotAsync(string paramName)
static void ThrowObjectDisposedException_FileClosed()
static void ThrowNotSupportedException_UnreadableStream()
static ValueTask FromCanceled(CancellationToken cancellationToken)
Definition ValueTask.cs:180