Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ChunkedMemoryStream.cs
Go to the documentation of this file.
3
4namespace System.IO;
5
6internal sealed class ChunkedMemoryStream : Stream
7{
8 private sealed class MemoryChunk
9 {
10 internal readonly byte[] _buffer;
11
12 internal int _freeOffset;
13
15
16 internal MemoryChunk(int bufferSize)
17 {
18 _buffer = new byte[bufferSize];
19 }
20 }
21
23
25
26 private int _totalLength;
27
28 public override bool CanRead => false;
29
30 public override bool CanSeek => false;
31
32 public override bool CanWrite => true;
33
34 public override long Length => _totalLength;
35
36 public override long Position
37 {
38 get
39 {
40 throw new NotSupportedException();
41 }
42 set
43 {
44 throw new NotSupportedException();
45 }
46 }
47
49 {
50 }
51
52 public byte[] ToArray()
53 {
54 byte[] array = new byte[_totalLength];
55 int num = 0;
56 for (MemoryChunk memoryChunk = _headChunk; memoryChunk != null; memoryChunk = memoryChunk._next)
57 {
58 Buffer.BlockCopy(memoryChunk._buffer, 0, array, num, memoryChunk._freeOffset);
59 num += memoryChunk._freeOffset;
60 }
61 return array;
62 }
63
64 public override void Write(byte[] buffer, int offset, int count)
65 {
67 }
68
69 public override void Write(ReadOnlySpan<byte> buffer)
70 {
71 while (!buffer.IsEmpty)
72 {
73 if (_currentChunk != null)
74 {
75 int num = _currentChunk._buffer.Length - _currentChunk._freeOffset;
76 if (num > 0)
77 {
78 int num2 = Math.Min(num, buffer.Length);
79 buffer.Slice(0, num2).CopyTo(new Span<byte>(_currentChunk._buffer, _currentChunk._freeOffset, num2));
80 buffer = buffer.Slice(num2);
81 _totalLength += num2;
82 _currentChunk._freeOffset += num2;
83 continue;
84 }
85 }
86 AppendChunk(buffer.Length);
87 }
88 }
89
91 {
92 if (cancellationToken.IsCancellationRequested)
93 {
95 }
97 return Task.CompletedTask;
98 }
99
101 {
102 if (cancellationToken.IsCancellationRequested)
103 {
105 }
106 Write(buffer.Span);
108 }
109
110 private void AppendChunk(long count)
111 {
112 int num = ((_currentChunk != null) ? (_currentChunk._buffer.Length * 2) : 1024);
113 if (count > num)
114 {
115 num = (int)Math.Min(count, 1048576L);
116 }
117 MemoryChunk memoryChunk = new MemoryChunk(num);
118 if (_currentChunk == null)
119 {
120 _headChunk = (_currentChunk = memoryChunk);
121 return;
122 }
123 _currentChunk._next = memoryChunk;
124 _currentChunk = memoryChunk;
125 }
126
127 public override void Flush()
128 {
129 }
130
132 {
133 return Task.CompletedTask;
134 }
135
136 public override int Read(byte[] buffer, int offset, int count)
137 {
138 throw new NotSupportedException();
139 }
140
141 public override long Seek(long offset, SeekOrigin origin)
142 {
143 throw new NotSupportedException();
144 }
145
146 public override void SetLength(long value)
147 {
148 if (_currentChunk != null)
149 {
150 throw new NotSupportedException();
151 }
153 }
154}
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
Definition Buffer.cs:102
override void SetLength(long value)
override int Read(byte[] buffer, int offset, int count)
override ValueTask WriteAsync(ReadOnlyMemory< byte > buffer, CancellationToken cancellationToken=default(CancellationToken))
override void Write(byte[] buffer, int offset, int count)
override Task FlushAsync(CancellationToken cancellationToken)
override void Write(ReadOnlySpan< byte > buffer)
override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
override long Seek(long offset, SeekOrigin origin)
static byte Min(byte val1, byte val2)
Definition Math.cs:912
static Task FromCanceled(CancellationToken cancellationToken)
Definition Task.cs:3363
static Task CompletedTask
Definition Task.cs:1120
static ValueTask CompletedTask
Definition ValueTask.cs:71
static ValueTask FromCanceled(CancellationToken cancellationToken)
Definition ValueTask.cs:180