Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
XmlStreamNodeWriter.cs
Go to the documentation of this file.
1using System.IO;
2using System.Text;
4
5namespace System.Xml;
6
7internal abstract class XmlStreamNodeWriter : XmlNodeWriter
8{
9 private Stream _stream;
10
11 private readonly byte[] _buffer;
12
13 private int _offset;
14
15 private bool _ownsStream;
16
17 private const int bufferLength = 512;
18
19 private const int maxBytesPerChar = 3;
20
22
24
25 public byte[] StreamBuffer => _buffer;
26
27 public int BufferOffset => _offset;
28
29 public int Position => (int)_stream.Position + _offset;
30
32 {
33 _buffer = new byte[512];
34 }
35
36 protected void SetOutput(Stream stream, bool ownsStream, Encoding encoding)
37 {
40 _offset = 0;
41 _encoding = encoding;
42 }
43
44 private int GetByteCount(char[] chars)
45 {
46 if (_encoding == null)
47 {
48 return s_UTF8Encoding.GetByteCount(chars);
49 }
51 }
52
53 protected byte[] GetBuffer(int count, out int offset)
54 {
55 int offset2 = _offset;
56 if (offset2 + count <= 512)
57 {
59 }
60 else
61 {
63 offset = 0;
64 }
65 return _buffer;
66 }
67
69 {
70 int offset = _offset;
71 int offset2;
72 if (offset + count <= 512)
73 {
75 }
76 else
77 {
79 offset2 = 0;
80 }
81 return new BytesWithOffset(_buffer, offset2);
82 }
83
84 protected void Advance(int count)
85 {
86 _offset += count;
87 }
88
89 private void EnsureByte()
90 {
91 if (_offset >= 512)
92 {
94 }
95 }
96
97 protected void WriteByte(byte b)
98 {
99 EnsureByte();
100 _buffer[_offset++] = b;
101 }
102
103 protected Task WriteByteAsync(byte b)
104 {
105 if (_offset >= 512)
106 {
108 }
109 _buffer[_offset++] = b;
110 return Task.CompletedTask;
111 }
112
118
119 protected void WriteByte(char ch)
120 {
121 WriteByte((byte)ch);
122 }
123
124 protected Task WriteByteAsync(char ch)
125 {
126 return WriteByteAsync((byte)ch);
127 }
128
129 protected void WriteBytes(byte b1, byte b2)
130 {
131 byte[] buffer = _buffer;
132 int num = _offset;
133 if (num + 1 >= 512)
134 {
135 FlushBuffer();
136 num = 0;
137 }
138 buffer[num] = b1;
139 buffer[num + 1] = b2;
140 _offset += 2;
141 }
142
143 protected Task WriteBytesAsync(byte b1, byte b2)
144 {
145 if (_offset + 1 >= 512)
146 {
148 }
149 _buffer[_offset++] = b1;
150 _buffer[_offset++] = b2;
151 return Task.CompletedTask;
152 }
153
160
161 protected void WriteBytes(char ch1, char ch2)
162 {
163 WriteBytes((byte)ch1, (byte)ch2);
164 }
165
166 protected Task WriteBytesAsync(char ch1, char ch2)
167 {
168 return WriteBytesAsync((byte)ch1, (byte)ch2);
169 }
170
171 public void WriteBytes(byte[] byteBuffer, int byteOffset, int byteCount)
172 {
173 if (byteCount < 512)
174 {
175 int offset;
179 }
180 else
181 {
182 FlushBuffer();
184 }
185 }
186
187 protected unsafe void UnsafeWriteBytes(byte* bytes, int byteCount)
188 {
189 FlushBuffer();
190 byte[] buffer = _buffer;
191 while (byteCount >= 512)
192 {
193 for (int i = 0; i < 512; i++)
194 {
195 buffer[i] = bytes[i];
196 }
197 _stream.Write(buffer, 0, 512);
198 bytes += 512;
199 byteCount -= 512;
200 }
201 for (int j = 0; j < byteCount; j++)
202 {
203 buffer[j] = bytes[j];
204 }
206 }
207
208 protected unsafe void WriteUTF8Char(int ch)
209 {
210 if (ch < 128)
211 {
212 WriteByte((byte)ch);
213 }
214 else if (ch <= 65535)
215 {
216 char* ptr = stackalloc char[1];
217 *ptr = (char)ch;
219 }
220 else
221 {
223 char* ptr2 = stackalloc char[2];
224 *ptr2 = surrogateChar.HighChar;
225 ptr2[1] = surrogateChar.LowChar;
227 }
228 }
229
230 protected void WriteUTF8Chars(byte[] chars, int charOffset, int charCount)
231 {
232 if (charCount < 512)
233 {
234 int offset;
238 }
239 else
240 {
241 FlushBuffer();
243 }
244 }
245
246 protected unsafe void WriteUTF8Chars(string value)
247 {
248 int length = value.Length;
249 if (length > 0)
250 {
251 fixed (char* chars = value)
252 {
254 }
255 }
256 }
257
258 protected unsafe void UnsafeWriteUTF8Chars(char* chars, int charCount)
259 {
260 while (charCount > 170)
261 {
262 int num = 170;
263 if ((chars[num - 1] & 0xFC00) == 55296)
264 {
265 num--;
266 }
267 int offset;
268 byte[] buffer = GetBuffer(num * 3, out offset);
270 charCount -= num;
271 chars += num;
272 }
273 if (charCount > 0)
274 {
275 int offset2;
276 byte[] buffer2 = GetBuffer(charCount * 3, out offset2);
278 }
279 }
280
281 protected unsafe void UnsafeWriteUnicodeChars(char* chars, int charCount)
282 {
283 while (charCount > 256)
284 {
285 int num = 256;
286 if ((chars[num - 1] & 0xFC00) == 55296)
287 {
288 num--;
289 }
290 int offset;
291 byte[] buffer = GetBuffer(num * 2, out offset);
293 charCount -= num;
294 chars += num;
295 }
296 if (charCount > 0)
297 {
298 int offset2;
299 byte[] buffer2 = GetBuffer(charCount * 2, out offset2);
301 }
302 }
303
304 protected unsafe int UnsafeGetUnicodeChars(char* chars, int charCount, byte[] buffer, int offset)
305 {
306 char* ptr = chars + charCount;
307 while (chars < ptr)
308 {
309 char c = *(chars++);
310 buffer[offset++] = (byte)c;
311 c = (char)((int)c >> 8);
312 buffer[offset++] = (byte)c;
313 }
314 return charCount * 2;
315 }
316
317 protected unsafe int UnsafeGetUTF8Length(char* chars, int charCount)
318 {
319 char* ptr = chars + charCount;
320 while (chars < ptr && *chars < '\u0080')
321 {
322 chars++;
323 }
324 if (chars == ptr)
325 {
326 return charCount;
327 }
328 char[] array = new char[ptr - chars];
329 for (int i = 0; i < array.Length; i++)
330 {
331 array[i] = chars[i];
332 }
333 return (int)(chars - (ptr - charCount)) + GetByteCount(array);
334 }
335
336 protected unsafe int UnsafeGetUTF8Chars(char* chars, int charCount, byte[] buffer, int offset)
337 {
338 if (charCount > 0)
339 {
340 fixed (byte* ptr = &buffer[offset])
341 {
342 byte* ptr2 = ptr;
343 byte* ptr3 = ptr2 + (buffer.Length - offset);
344 char* ptr4 = chars + charCount;
345 do
346 {
347 IL_0045:
348 if (chars < ptr4)
349 {
350 char c = *chars;
351 if (c < '\u0080')
352 {
353 *ptr2 = (byte)c;
354 ptr2++;
355 chars++;
356 goto IL_0045;
357 }
358 }
359 if (chars >= ptr4)
360 {
361 break;
362 }
363 char* ptr5 = chars;
364 while (chars < ptr4 && *chars >= '\u0080')
365 {
366 chars++;
367 }
368 ptr2 += (_encoding ?? s_UTF8Encoding).GetBytes(ptr5, (int)(chars - ptr5), ptr2, (int)(ptr3 - ptr2));
369 }
370 while (chars < ptr4);
371 return (int)(ptr2 - ptr);
372 }
373 }
374 return 0;
375 }
376
377 protected virtual void FlushBuffer()
378 {
379 if (_offset != 0)
380 {
382 _offset = 0;
383 }
384 }
385
386 protected virtual Task FlushBufferAsync()
387 {
388 if (_offset != 0)
389 {
390 Task result = _stream.WriteAsync(_buffer, 0, _offset);
391 _offset = 0;
392 return result;
393 }
394 return Task.CompletedTask;
395 }
396
397 public override void Flush()
398 {
399 FlushBuffer();
400 _stream.Flush();
401 }
402
408
409 public override void Close()
410 {
411 if (_stream != null)
412 {
413 if (_ownsStream)
414 {
416 }
417 _stream = null;
418 }
419 }
420}
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
Definition Buffer.cs:102
Task FlushAsync()
Definition Stream.cs:669
Task WriteAsync(byte[] buffer, int offset, int count)
Definition Stream.cs:914
void Dispose()
Definition Stream.cs:639
void Write(byte[] buffer, int offset, int count)
virtual int GetByteCount(char[] chars)
Definition Encoding.cs:713
new ConfiguredTaskAwaitable< TResult > ConfigureAwait(bool continueOnCapturedContext)
Definition Task.cs:226
static Task CompletedTask
Definition Task.cs:1120
unsafe void UnsafeWriteUnicodeChars(char *chars, int charCount)
unsafe void UnsafeWriteUTF8Chars(char *chars, int charCount)
unsafe int UnsafeGetUTF8Chars(char *chars, int charCount, byte[] buffer, int offset)
unsafe int UnsafeGetUTF8Length(char *chars, int charCount)
void WriteBytes(char ch1, char ch2)
async Task FlushAndWriteBytesAsync(byte b1, byte b2)
unsafe int UnsafeGetUnicodeChars(char *chars, int charCount, byte[] buffer, int offset)
byte[] GetBuffer(int count, out int offset)
async Task FlushBufferAndWriteByteAsync(byte b)
void SetOutput(Stream stream, bool ownsStream, Encoding encoding)
void WriteUTF8Chars(byte[] chars, int charOffset, int charCount)
Task WriteBytesAsync(char ch1, char ch2)
unsafe void WriteUTF8Chars(string value)
async Task< BytesWithOffset > GetBufferAsync(int count)
unsafe void UnsafeWriteBytes(byte *bytes, int byteCount)
static readonly UTF8Encoding s_UTF8Encoding
Task WriteBytesAsync(byte b1, byte b2)
void WriteBytes(byte[] byteBuffer, int byteOffset, int byteCount)