Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
MemoryStream.cs
Go to the documentation of this file.
5
6namespace System.IO;
7
8public class MemoryStream : Stream
9{
10 private byte[] _buffer;
11
12 private readonly int _origin;
13
14 private int _position;
15
16 private int _length;
17
18 private int _capacity;
19
20 private bool _expandable;
21
22 private bool _writable;
23
24 private readonly bool _exposable;
25
26 private bool _isOpen;
27
29
30 public override bool CanRead => _isOpen;
31
32 public override bool CanSeek => _isOpen;
33
34 public override bool CanWrite => _writable;
35
36 public virtual int Capacity
37 {
38 get
39 {
41 return _capacity - _origin;
42 }
43 set
44 {
45 if (value < Length)
46 {
48 }
50 if (!_expandable && value != Capacity)
51 {
53 }
54 if (!_expandable || value == _capacity)
55 {
56 return;
57 }
58 if (value > 0)
59 {
60 byte[] array = new byte[value];
61 if (_length > 0)
62 {
64 }
65 _buffer = array;
66 }
67 else
68 {
69 _buffer = Array.Empty<byte>();
70 }
72 }
73 }
74
75 public override long Length
76 {
77 get
78 {
80 return _length - _origin;
81 }
82 }
83
84 public override long Position
85 {
86 get
87 {
89 return _position - _origin;
90 }
91 set
92 {
93 if (value < 0)
94 {
96 }
98 if (value > int.MaxValue)
99 {
101 }
102 _position = _origin + (int)value;
103 }
104 }
105
107 : this(0)
108 {
109 }
110
112 {
113 if (capacity < 0)
114 {
116 }
117 _buffer = ((capacity != 0) ? new byte[capacity] : Array.Empty<byte>());
119 _expandable = true;
120 _writable = true;
121 _exposable = true;
122 _isOpen = true;
123 }
124
125 public MemoryStream(byte[] buffer)
126 : this(buffer, writable: true)
127 {
128 }
129
130 public MemoryStream(byte[] buffer, bool writable)
131 {
132 if (buffer == null)
133 {
134 throw new ArgumentNullException("buffer", SR.ArgumentNull_Buffer);
135 }
136 _buffer = buffer;
137 _length = (_capacity = buffer.Length);
138 _writable = writable;
139 _isOpen = true;
140 }
141
142 public MemoryStream(byte[] buffer, int index, int count)
143 : this(buffer, index, count, writable: true, publiclyVisible: false)
144 {
145 }
146
147 public MemoryStream(byte[] buffer, int index, int count, bool writable)
148 : this(buffer, index, count, writable, publiclyVisible: false)
149 {
150 }
151
152 public MemoryStream(byte[] buffer, int index, int count, bool writable, bool publiclyVisible)
153 {
154 if (buffer == null)
155 {
156 throw new ArgumentNullException("buffer", SR.ArgumentNull_Buffer);
157 }
158 if (index < 0)
159 {
161 }
162 if (count < 0)
163 {
165 }
166 if (buffer.Length - index < count)
167 {
169 }
170 _buffer = buffer;
171 _origin = (_position = index);
173 _writable = writable;
174 _exposable = publiclyVisible;
175 _isOpen = true;
176 }
177
178 private void EnsureNotClosed()
179 {
180 if (!_isOpen)
181 {
183 }
184 }
185
186 private void EnsureWriteable()
187 {
188 if (!CanWrite)
189 {
191 }
192 }
193
194 protected override void Dispose(bool disposing)
195 {
196 try
197 {
198 if (disposing)
199 {
200 _isOpen = false;
201 _writable = false;
202 _expandable = false;
203 _lastReadTask = null;
204 }
205 }
206 finally
207 {
208 base.Dispose(disposing);
209 }
210 }
211
212 private bool EnsureCapacity(int value)
213 {
214 if (value < 0)
215 {
217 }
218 if (value > _capacity)
219 {
220 int num = Math.Max(value, 256);
221 if (num < _capacity * 2)
222 {
223 num = _capacity * 2;
224 }
225 if ((uint)(_capacity * 2) > Array.MaxLength)
226 {
227 num = Math.Max(value, Array.MaxLength);
228 }
229 Capacity = num;
230 return true;
231 }
232 return false;
233 }
234
235 public override void Flush()
236 {
237 }
238
240 {
241 if (cancellationToken.IsCancellationRequested)
242 {
244 }
245 try
246 {
247 Flush();
248 return Task.CompletedTask;
249 }
250 catch (Exception exception)
251 {
253 }
254 }
255
256 public virtual byte[] GetBuffer()
257 {
258 if (!_exposable)
259 {
261 }
262 return _buffer;
263 }
264
265 public virtual bool TryGetBuffer(out ArraySegment<byte> buffer)
266 {
267 if (!_exposable)
268 {
269 buffer = default(ArraySegment<byte>);
270 return false;
271 }
273 return true;
274 }
275
276 internal byte[] InternalGetBuffer()
277 {
278 return _buffer;
279 }
280
281 internal int InternalGetPosition()
282 {
283 return _position;
284 }
285
286 [MethodImpl(MethodImplOptions.AggressiveInlining)]
288 {
290 int position = _position;
291 int num = position + count;
292 if ((uint)num > (uint)_length)
293 {
296 }
297 ReadOnlySpan<byte> result = new ReadOnlySpan<byte>(_buffer, position, count);
298 _position = num;
299 return result;
300 }
301
302 internal int InternalEmulateRead(int count)
303 {
305 int num = _length - _position;
306 if (num > count)
307 {
308 num = count;
309 }
310 if (num < 0)
311 {
312 num = 0;
313 }
314 _position += num;
315 return num;
316 }
317
318 public override int Read(byte[] buffer, int offset, int count)
319 {
322 int num = _length - _position;
323 if (num > count)
324 {
325 num = count;
326 }
327 if (num <= 0)
328 {
329 return 0;
330 }
331 if (num <= 8)
332 {
333 int num2 = num;
334 while (--num2 >= 0)
335 {
336 buffer[offset + num2] = _buffer[_position + num2];
337 }
338 }
339 else
340 {
342 }
343 _position += num;
344 return num;
345 }
346
347 public override int Read(Span<byte> buffer)
348 {
349 if (GetType() != typeof(MemoryStream))
350 {
351 return base.Read(buffer);
352 }
354 int num = Math.Min(_length - _position, buffer.Length);
355 if (num <= 0)
356 {
357 return 0;
358 }
360 _position += num;
361 return num;
362 }
363
365 {
367 if (cancellationToken.IsCancellationRequested)
368 {
370 }
371 try
372 {
373 int num = Read(buffer, offset, count);
374 Task<int> lastReadTask = _lastReadTask;
375 return (lastReadTask != null && lastReadTask.Result == num) ? lastReadTask : (_lastReadTask = Task.FromResult(num));
376 }
378 {
379 return Task.FromCanceled<int>(exception);
380 }
381 catch (Exception exception2)
382 {
383 return Task.FromException<int>(exception2);
384 }
385 }
386
388 {
389 if (cancellationToken.IsCancellationRequested)
390 {
392 }
393 try
394 {
395 ArraySegment<byte> segment;
396 return new ValueTask<int>(MemoryMarshal.TryGetArray((ReadOnlyMemory<byte>)buffer, out segment) ? Read(segment.Array, segment.Offset, segment.Count) : Read(buffer.Span));
397 }
399 {
400 return new ValueTask<int>(Task.FromCanceled<int>(exception));
401 }
402 catch (Exception exception2)
403 {
404 return ValueTask.FromException<int>(exception2);
405 }
406 }
407
408 public override int ReadByte()
409 {
411 if (_position >= _length)
412 {
413 return -1;
414 }
415 return _buffer[_position++];
416 }
417
418 public override void CopyTo(Stream destination, int bufferSize)
419 {
420 if (GetType() != typeof(MemoryStream))
421 {
422 base.CopyTo(destination, bufferSize);
423 return;
424 }
427 int position = _position;
428 int num = InternalEmulateRead(_length - position);
429 if (num > 0)
430 {
431 destination.Write(_buffer, position, num);
432 }
433 }
434
436 {
439 if (GetType() != typeof(MemoryStream))
440 {
441 return base.CopyToAsync(destination, bufferSize, cancellationToken);
442 }
443 if (cancellationToken.IsCancellationRequested)
444 {
446 }
447 int position = _position;
449 if (num == 0)
450 {
451 return Task.CompletedTask;
452 }
453 if (!(destination is MemoryStream memoryStream))
454 {
455 return destination.WriteAsync(_buffer, position, num, cancellationToken);
456 }
457 try
458 {
459 memoryStream.Write(_buffer, position, num);
460 return Task.CompletedTask;
461 }
462 catch (Exception exception)
463 {
465 }
466 }
467
468 public override long Seek(long offset, SeekOrigin loc)
469 {
471 if (offset > int.MaxValue)
472 {
474 }
475 switch (loc)
476 {
477 case SeekOrigin.Begin:
478 {
479 int num3 = _origin + (int)offset;
480 if (offset < 0 || num3 < _origin)
481 {
483 }
484 _position = num3;
485 break;
486 }
487 case SeekOrigin.Current:
488 {
489 int num2 = _position + (int)offset;
490 if (_position + offset < _origin || num2 < _origin)
491 {
493 }
494 _position = num2;
495 break;
496 }
497 case SeekOrigin.End:
498 {
499 int num = _length + (int)offset;
500 if (_length + offset < _origin || num < _origin)
501 {
503 }
504 _position = num;
505 break;
506 }
507 default:
509 }
510 return _position;
511 }
512
513 public override void SetLength(long value)
514 {
515 if (value < 0 || value > int.MaxValue)
516 {
518 }
520 if (value > int.MaxValue - _origin)
521 {
523 }
524 int num = _origin + (int)value;
525 if (!EnsureCapacity(num) && num > _length)
526 {
528 }
529 _length = num;
530 if (_position > num)
531 {
532 _position = num;
533 }
534 }
535
536 public virtual byte[] ToArray()
537 {
538 int num = _length - _origin;
539 if (num == 0)
540 {
541 return Array.Empty<byte>();
542 }
543 byte[] array = GC.AllocateUninitializedArray<byte>(num);
544 _buffer.AsSpan(_origin, num).CopyTo(array);
545 return array;
546 }
547
548 public override void Write(byte[] buffer, int offset, int count)
549 {
553 int num = _position + count;
554 if (num < 0)
555 {
557 }
558 if (num > _length)
559 {
560 bool flag = _position > _length;
561 if (num > _capacity && EnsureCapacity(num))
562 {
563 flag = false;
564 }
565 if (flag)
566 {
568 }
569 _length = num;
570 }
571 if (count <= 8 && buffer != _buffer)
572 {
573 int num2 = count;
574 while (--num2 >= 0)
575 {
576 _buffer[_position + num2] = buffer[offset + num2];
577 }
578 }
579 else
580 {
582 }
583 _position = num;
584 }
585
586 public override void Write(ReadOnlySpan<byte> buffer)
587 {
588 if (GetType() != typeof(MemoryStream))
589 {
590 base.Write(buffer);
591 return;
592 }
595 int num = _position + buffer.Length;
596 if (num < 0)
597 {
599 }
600 if (num > _length)
601 {
602 bool flag = _position > _length;
603 if (num > _capacity && EnsureCapacity(num))
604 {
605 flag = false;
606 }
607 if (flag)
608 {
610 }
611 _length = num;
612 }
613 buffer.CopyTo(new Span<byte>(_buffer, _position, buffer.Length));
614 _position = num;
615 }
616
618 {
620 if (cancellationToken.IsCancellationRequested)
621 {
623 }
624 try
625 {
627 return Task.CompletedTask;
628 }
630 {
632 }
633 catch (Exception exception2)
634 {
635 return Task.FromException(exception2);
636 }
637 }
638
640 {
641 if (cancellationToken.IsCancellationRequested)
642 {
644 }
645 try
646 {
647 if (MemoryMarshal.TryGetArray(buffer, out var segment))
648 {
649 Write(segment.Array, segment.Offset, segment.Count);
650 }
651 else
652 {
653 Write(buffer.Span);
654 }
655 return default(ValueTask);
656 }
658 {
660 }
661 catch (Exception exception2)
662 {
663 return ValueTask.FromException(exception2);
664 }
665 }
666
667 public override void WriteByte(byte value)
668 {
671 if (_position >= _length)
672 {
673 int num = _position + 1;
674 bool flag = _position > _length;
675 if (num >= _capacity && EnsureCapacity(num))
676 {
677 flag = false;
678 }
679 if (flag)
680 {
682 }
683 _length = num;
684 }
686 }
687
688 public virtual void WriteTo(Stream stream)
689 {
690 if (stream == null)
691 {
692 throw new ArgumentNullException("stream", SR.ArgumentNull_Stream);
693 }
696 }
697}
static int MaxLength
Definition Array.cs:471
static unsafe void Clear(Array array)
Definition Array.cs:755
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
Definition Buffer.cs:102
Definition GC.cs:8
Task< int > _lastReadTask
override int ReadByte()
override void Flush()
virtual void WriteTo(Stream stream)
MemoryStream(byte[] buffer, int index, int count)
override long Seek(long offset, SeekOrigin loc)
bool EnsureCapacity(int value)
override Task FlushAsync(CancellationToken cancellationToken)
override Task< int > ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
MemoryStream(byte[] buffer, int index, int count, bool writable)
override bool CanRead
virtual bool TryGetBuffer(out ArraySegment< byte > buffer)
override ValueTask WriteAsync(ReadOnlyMemory< byte > buffer, CancellationToken cancellationToken=default(CancellationToken))
override bool CanWrite
ReadOnlySpan< byte > InternalReadSpan(int count)
override void SetLength(long value)
override void Write(ReadOnlySpan< byte > buffer)
MemoryStream(int capacity)
int InternalEmulateRead(int count)
MemoryStream(byte[] buffer)
MemoryStream(byte[] buffer, bool writable)
MemoryStream(byte[] buffer, int index, int count, bool writable, bool publiclyVisible)
override ValueTask< int > ReadAsync(Memory< byte > buffer, CancellationToken cancellationToken=default(CancellationToken))
override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
override void Dispose(bool disposing)
override bool CanSeek
override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
virtual byte[] ToArray()
override void CopyTo(Stream destination, int bufferSize)
virtual byte[] GetBuffer()
override int Read(byte[] buffer, int offset, int count)
override void Write(byte[] buffer, int offset, int count)
override void WriteByte(byte value)
override int Read(Span< byte > buffer)
readonly bool _exposable
static void ValidateBufferArguments(byte[] buffer, int offset, int count)
Definition Stream.cs:1044
static void ValidateCopyToArguments(Stream destination, int bufferSize)
Definition Stream.cs:1060
static byte Min(byte val1, byte val2)
Definition Math.cs:912
static byte Max(byte val1, byte val2)
Definition Math.cs:738
static string ArgumentOutOfRange_SmallCapacity
Definition SR.cs:34
static string IO_SeekBeforeBegin
Definition SR.cs:140
static string ArgumentOutOfRange_NegativeCapacity
Definition SR.cs:1078
static string ArgumentNull_Stream
Definition SR.cs:958
static string IO_StreamTooLong
Definition SR.cs:1586
static string NotSupported_MemStreamNotExpandable
Definition SR.cs:1684
static string Argument_InvalidSeekOrigin
Definition SR.cs:736
static string ArgumentNull_Buffer
Definition SR.cs:22
static string Argument_InvalidOffLen
Definition SR.cs:22
static string UnauthorizedAccess_MemStreamBuffer
Definition SR.cs:2024
static string ArgumentOutOfRange_NeedNonNegNum
Definition SR.cs:32
static string ArgumentOutOfRange_StreamLength
Definition SR.cs:1110
Definition SR.cs:7
static Task FromException(Exception exception)
Definition Task.cs:3341
static Task FromCanceled(CancellationToken cancellationToken)
Definition Task.cs:3363
static Task CompletedTask
Definition Task.cs:1120
static void ThrowNotSupportedException_UnwritableStream()
static void ThrowEndOfFileException()
static void ThrowObjectDisposedException_StreamClosed(string objectName)
void CopyTo(Span< T > destination)
Definition Span.cs:224
static ValueTask FromCanceled(CancellationToken cancellationToken)
Definition ValueTask.cs:180
static ValueTask FromException(Exception exception)
Definition ValueTask.cs:190