Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ReadOnlyUnmanagedMemoryStream.cs
Go to the documentation of this file.
1using System.IO;
3
5
6internal sealed class ReadOnlyUnmanagedMemoryStream : Stream
7{
8 private unsafe readonly byte* _data;
9
10 private readonly int _length;
11
12 private int _position;
13
14 public override bool CanRead => true;
15
16 public override bool CanSeek => true;
17
18 public override bool CanWrite => false;
19
20 public override long Length => _length;
21
22 public override long Position
23 {
24 get
25 {
26 return _position;
27 }
28 set
29 {
30 Seek(value, SeekOrigin.Begin);
31 }
32 }
33
34 public unsafe ReadOnlyUnmanagedMemoryStream(byte* data, int length)
35 {
36 _data = data;
38 }
39
40 public unsafe override int ReadByte()
41 {
42 if (_position >= _length)
43 {
44 return -1;
45 }
46 return _data[_position++];
47 }
48
49 public unsafe override int Read(byte[] buffer, int offset, int count)
50 {
51 int num = Math.Min(count, _length - _position);
53 _position += num;
54 return num;
55 }
56
57 public override void Flush()
58 {
59 }
60
61 public override long Seek(long offset, SeekOrigin origin)
62 {
63 long num;
64 try
65 {
66 num = checked(origin switch
67 {
68 SeekOrigin.Begin => offset,
69 SeekOrigin.Current => offset + _position,
70 SeekOrigin.End => offset + _length,
71 _ => throw new ArgumentOutOfRangeException("origin"),
72 });
73 }
74 catch (OverflowException)
75 {
76 throw new ArgumentOutOfRangeException("offset");
77 }
78 if (num < 0 || num > int.MaxValue)
79 {
80 throw new ArgumentOutOfRangeException("offset");
81 }
82 _position = (int)num;
83 return num;
84 }
85
86 public override void SetLength(long value)
87 {
88 throw new NotSupportedException();
89 }
90
91 public override void Write(byte[] buffer, int offset, int count)
92 {
93 throw new NotSupportedException();
94 }
95}
static byte Min(byte val1, byte val2)
Definition Math.cs:912
override void Write(byte[] buffer, int offset, int count)
unsafe override int Read(byte[] buffer, int offset, int count)
static void Copy(int[] source, int startIndex, IntPtr destination, int length)
Definition Marshal.cs:800