Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ReadOnlyMemoryStream.cs
Go to the documentation of this file.
3
4namespace System.IO;
5
6internal sealed class ReadOnlyMemoryStream : Stream
7{
9
10 private int _position;
11
12 private bool _isOpen;
13
14 public override bool CanRead => _isOpen;
15
16 public override bool CanSeek => _isOpen;
17
18 public override bool CanWrite => false;
19
20 public override long Length
21 {
22 get
23 {
25 return _content.Length;
26 }
27 }
28
29 public override long Position
30 {
31 get
32 {
34 return _position;
35 }
36 set
37 {
39 if (value < 0 || value > int.MaxValue)
40 {
41 throw new ArgumentOutOfRangeException("value");
42 }
43 _position = (int)value;
44 }
45 }
46
48 {
49 _content = content;
50 _isOpen = true;
51 }
52
53 private void EnsureNotClosed()
54 {
55 if (!_isOpen)
56 {
58 }
59 }
60
61 public override long Seek(long offset, SeekOrigin origin)
62 {
64 long num = origin switch
65 {
66 SeekOrigin.End => _content.Length + offset,
67 SeekOrigin.Current => _position + offset,
68 SeekOrigin.Begin => offset,
69 _ => throw new ArgumentOutOfRangeException("origin"),
70 };
71 if (num > int.MaxValue)
72 {
73 throw new ArgumentOutOfRangeException("offset");
74 }
75 if (num < 0)
76 {
78 }
79 _position = (int)num;
80 return _position;
81 }
82
83 public override int ReadByte()
84 {
87 if (_position >= span.Length)
88 {
89 return -1;
90 }
91 return span[_position++];
92 }
93
94 public override int Read(byte[] buffer, int offset, int count)
95 {
98 }
99
100 public override int Read(Span<byte> buffer)
101 {
102 return ReadBuffer(buffer);
103 }
104
106 {
108 int num = _content.Length - _position;
109 if (num <= 0 || buffer.Length == 0)
110 {
111 return 0;
112 }
113 ReadOnlySpan<byte> readOnlySpan;
114 if (num <= buffer.Length)
115 {
116 readOnlySpan = _content.Span;
117 readOnlySpan = readOnlySpan.Slice(_position);
118 readOnlySpan.CopyTo(buffer);
120 return num;
121 }
122 readOnlySpan = _content.Span;
123 readOnlySpan = readOnlySpan.Slice(_position, buffer.Length);
124 readOnlySpan.CopyTo(buffer);
125 _position += buffer.Length;
126 return buffer.Length;
127 }
128
130 {
133 if (!cancellationToken.IsCancellationRequested)
134 {
135 return Task.FromResult(ReadBuffer(new Span<byte>(buffer, offset, count)));
136 }
138 }
139
141 {
143 if (!cancellationToken.IsCancellationRequested)
144 {
145 return new ValueTask<int>(ReadBuffer(buffer.Span));
146 }
148 }
149
150 public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
151 {
153 }
154
155 public override int EndRead(IAsyncResult asyncResult)
156 {
159 }
160
161 public override void CopyTo(Stream destination, int bufferSize)
162 {
166 {
167 destination.Write(_content.Span.Slice(_position));
168 }
169 }
170
172 {
176 {
177 return Task.CompletedTask;
178 }
179 return destination.WriteAsync(_content.Slice(_position), cancellationToken).AsTask();
180 }
181
182 public override void Flush()
183 {
184 }
185
187 {
188 return Task.CompletedTask;
189 }
190
191 public override void SetLength(long value)
192 {
193 throw new NotSupportedException();
194 }
195
196 public override void Write(byte[] buffer, int offset, int count)
197 {
198 throw new NotSupportedException();
199 }
200
201 protected override void Dispose(bool disposing)
202 {
203 _isOpen = false;
205 base.Dispose(disposing);
206 }
207}
override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
override int EndRead(IAsyncResult asyncResult)
override void CopyTo(Stream destination, int bufferSize)
ReadOnlyMemoryStream(ReadOnlyMemory< byte > content)
override Task< int > ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
override ValueTask< int > ReadAsync(Memory< byte > buffer, CancellationToken cancellationToken=default(CancellationToken))
override void Write(byte[] buffer, int offset, int count)
override Task FlushAsync(CancellationToken cancellationToken)
override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
override int Read(Span< byte > buffer)
int ReadBuffer(Span< byte > buffer)
override void SetLength(long value)
override int Read(byte[] buffer, int offset, int count)
override void Dispose(bool disposing)
override long Seek(long offset, SeekOrigin origin)
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 string ObjectDisposed_StreamClosed
Definition SR.cs:20
static string IO_SeekBeforeBegin
Definition SR.cs:140
Definition SR.cs:7
static IAsyncResult Begin(Task task, AsyncCallback callback, object state)
Definition TaskToApm.cs:43
static void End(IAsyncResult asyncResult)
Definition TaskToApm.cs:48
static Task FromCanceled(CancellationToken cancellationToken)
Definition Task.cs:3363
static Task CompletedTask
Definition Task.cs:1120
unsafe ReadOnlySpan< T > Span
ReadOnlyMemory< T > Slice(int start)
void CopyTo(Span< T > destination)
ReadOnlySpan< T > Slice(int start)
static ValueTask FromCanceled(CancellationToken cancellationToken)
Definition ValueTask.cs:180