Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
OSFileStreamStrategy.cs
Go to the documentation of this file.
5
7
8internal abstract class OSFileStreamStrategy : FileStreamStrategy
9{
10 protected readonly SafeFileHandle _fileHandle;
11
12 private readonly FileAccess _access;
13
14 protected long _filePosition;
15
16 private long _length = -1L;
17
18 private long _appendStart;
19
20 private bool _lengthCanBeCached;
21
22 internal override bool IsAsync => _fileHandle.IsAsync;
23
24 public sealed override bool CanSeek => _fileHandle.CanSeek;
25
26 public sealed override bool CanRead
27 {
28 get
29 {
31 {
32 return (_access & FileAccess.Read) != 0;
33 }
34 return false;
35 }
36 }
37
38 public sealed override bool CanWrite
39 {
40 get
41 {
43 {
44 return (_access & FileAccess.Write) != 0;
45 }
46 return false;
47 }
48 }
49
50 public sealed override long Length
51 {
52 get
53 {
55 {
57 }
58 if (_length < 0)
59 {
61 }
62 return _length;
63 }
64 }
65
67 {
68 get
69 {
71 return _lengthCanBeCached;
72 }
73 }
74
75 public sealed override long Position
76 {
77 get
78 {
79 return _filePosition;
80 }
81 set
82 {
84 }
85 }
86
87 internal sealed override string Name => _fileHandle.Path ?? SR.IO_UnknownFileName;
88
89 internal sealed override bool IsClosed => _fileHandle.IsClosed;
90
91 internal sealed override SafeFileHandle SafeFileHandle
92 {
93 get
94 {
95 if (CanSeek)
96 {
98 }
99 _lengthCanBeCached = false;
100 _length = -1L;
101 return _fileHandle;
102 }
103 }
104
106 {
107 _access = access;
108 handle.EnsureThreadPoolBindingInitialized();
109 if (handle.CanSeek)
110 {
112 }
113 else
114 {
115 _filePosition = 0L;
116 }
118 }
119
120 internal OSFileStreamStrategy(string path, FileMode mode, FileAccess access, FileShare share, FileOptions options, long preallocationSize)
121 {
122 string fullPath = Path.GetFullPath(path);
123 _access = access;
124 _lengthCanBeCached = (share & FileShare.Write) == 0 && (access & FileAccess.Write) == 0;
125 _fileHandle = SafeFileHandle.Open(fullPath, mode, access, share, options, preallocationSize);
126 try
127 {
128 if (mode == FileMode.Append && CanSeek)
129 {
131 }
132 else
133 {
134 _appendStart = -1L;
135 }
136 }
137 catch
138 {
140 _fileHandle = null;
141 throw;
142 }
143 }
144
145 internal void OnIncompleteOperation(int expectedBytesTransferred, int actualBytesTransferred)
146 {
147 Interlocked.Add(ref _filePosition, actualBytesTransferred - expectedBytesTransferred);
148 }
149
150 public sealed override ValueTask DisposeAsync()
151 {
152 if (_fileHandle != null && !_fileHandle.IsClosed)
153 {
156 }
158 }
159
160 internal sealed override void DisposeInternal(bool disposing)
161 {
162 Dispose(disposing);
163 }
164
165 protected sealed override void Dispose(bool disposing)
166 {
167 if (disposing && _fileHandle != null && !_fileHandle.IsClosed)
168 {
171 }
172 }
173
174 public sealed override void Flush()
175 {
176 }
177
179 {
180 return Task.CompletedTask;
181 }
182
183 internal sealed override void Flush(bool flushToDisk)
184 {
185 if (flushToDisk && CanWrite)
186 {
188 }
189 }
190
191 public sealed override long Seek(long offset, SeekOrigin origin)
192 {
193 if (origin < SeekOrigin.Begin || origin > SeekOrigin.End)
194 {
196 }
198 {
200 }
201 if (!CanSeek)
202 {
204 }
205 long filePosition = _filePosition;
206 long num = origin switch
207 {
208 SeekOrigin.Begin => offset,
209 SeekOrigin.End => Length + offset,
210 _ => _filePosition + offset,
211 };
212 if (num >= 0)
213 {
214 _filePosition = num;
215 }
216 else
217 {
219 }
220 if (_appendStart != -1 && num < _appendStart)
221 {
222 _filePosition = filePosition;
224 }
225 return num;
226 }
227
228 internal sealed override void Lock(long position, long length)
229 {
231 }
232
233 internal sealed override void Unlock(long position, long length)
234 {
236 }
237
238 public sealed override void SetLength(long value)
239 {
240 if (_appendStart != -1 && value < _appendStart)
241 {
243 }
245 }
246
247 protected void SetLengthCore(long value)
248 {
251 {
252 _length = value;
253 }
254 if (_filePosition > value)
255 {
257 }
258 }
259
260 public unsafe sealed override int ReadByte()
261 {
262 System.Runtime.CompilerServices.Unsafe.SkipInit(out byte result);
263 if (Read(new Span<byte>(&result, 1)) == 0)
264 {
265 return -1;
266 }
267 return result;
268 }
269
270 public sealed override int Read(byte[] buffer, int offset, int count)
271 {
272 return Read(new Span<byte>(buffer, offset, count));
273 }
274
275 public sealed override int Read(Span<byte> buffer)
276 {
278 {
280 }
281 else if ((_access & FileAccess.Read) == 0)
282 {
284 }
286 _filePosition += num;
287 return num;
288 }
289
290 public unsafe sealed override void WriteByte(byte value)
291 {
293 }
294
295 public override void Write(byte[] buffer, int offset, int count)
296 {
298 }
299
313
314 public sealed override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
315 {
316 return TaskToApm.Begin(WriteAsync(buffer, offset, count), callback, state);
317 }
318
319 public sealed override void EndWrite(IAsyncResult asyncResult)
320 {
322 }
323
324 public sealed override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
325 {
327 }
328
330 {
331 long fileOffset = (CanSeek ? (Interlocked.Add(ref _filePosition, source.Length) - source.Length) : (-1));
333 }
334
335 public sealed override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
336 {
337 return TaskToApm.Begin(ReadAsync(buffer, offset, count), callback, state);
338 }
339
340 public sealed override int EndRead(IAsyncResult asyncResult)
341 {
342 return TaskToApm.End<int>(asyncResult);
343 }
344
345 public sealed override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
346 {
348 }
349
351 {
352 if (!CanSeek)
353 {
355 }
357 {
358 return ValueTask.FromResult(0);
359 }
360 long fileOffset = Interlocked.Add(ref _filePosition, destination.Length) - destination.Length;
362 }
363}
static SafeFileHandle Open(string fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, long preallocationSize)
static string GetFullPath(string path)
Definition Path.cs:881
static unsafe void WriteAtOffset(SafeFileHandle handle, ReadOnlySpan< byte > buffer, long fileOffset)
static unsafe int ReadAtOffset(SafeFileHandle handle, Span< byte > buffer, long fileOffset)
static ValueTask WriteAtOffsetAsync(SafeFileHandle handle, ReadOnlyMemory< byte > buffer, long fileOffset, CancellationToken cancellationToken, OSFileStreamStrategy strategy=null)
static unsafe long GetFileLength(SafeFileHandle handle)
static ValueTask< int > ReadAtOffsetAsync(SafeFileHandle handle, Memory< byte > buffer, long fileOffset, CancellationToken cancellationToken, OSFileStreamStrategy strategy=null)
static void SetFileLength(SafeFileHandle handle, long length)
static void Lock(SafeFileHandle handle, bool canWrite, long position, long length)
static void Unlock(SafeFileHandle handle, long position, long length)
static void FlushToDisk(SafeFileHandle handle)
static long Seek(SafeFileHandle handle, long offset, SeekOrigin origin, bool closeInvalidHandle=false)
static void ThrowInvalidArgument(SafeFileHandle handle)
override void Write(ReadOnlySpan< byte > buffer)
override int Read(Span< byte > buffer)
OSFileStreamStrategy(SafeFileHandle handle, FileAccess access)
override Task FlushAsync(CancellationToken cancellationToken)
override void Lock(long position, long length)
override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
override long Seek(long offset, SeekOrigin origin)
override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
override ValueTask< int > ReadAsync(Memory< byte > destination, CancellationToken cancellationToken)
override void Write(byte[] buffer, int offset, int count)
override int EndRead(IAsyncResult asyncResult)
OSFileStreamStrategy(string path, FileMode mode, FileAccess access, FileShare share, FileOptions options, long preallocationSize)
override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
override void EndWrite(IAsyncResult asyncResult)
override ValueTask WriteAsync(ReadOnlyMemory< byte > source, CancellationToken cancellationToken)
override int Read(byte[] buffer, int offset, int count)
override Task< int > ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
override void Unlock(long position, long length)
void OnIncompleteOperation(int expectedBytesTransferred, int actualBytesTransferred)
override void DisposeInternal(bool disposing)
unsafe override void WriteByte(byte value)
void Dispose()
Definition Stream.cs:639
static string IO_SeekAppendOverwrite
Definition SR.cs:1576
static string IO_UnknownFileName
Definition SR.cs:1596
static string IO_SetLengthAppendTruncate
Definition SR.cs:1580
static string Argument_InvalidSeekOrigin
Definition SR.cs:736
Definition SR.cs:7
static int Add(ref int location1, int value)
static IAsyncResult Begin(Task task, AsyncCallback callback, object state)
Definition TaskToApm.cs:43
static void End(IAsyncResult asyncResult)
Definition TaskToApm.cs:48
static Task CompletedTask
Definition Task.cs:1120
static bool Read(ref bool location)
Definition Volatile.cs:67
static void ThrowNotSupportedException_UnwritableStream()
static void ThrowNotSupportedException_UnseekableStream()
static void ThrowObjectDisposedException_FileClosed()
static void ThrowNotSupportedException_UnreadableStream()
static ValueTask CompletedTask
Definition ValueTask.cs:71