Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
JsonEncodingStreamWrapper.cs
Go to the documentation of this file.
2using System.IO;
3using System.Text;
4using System.Xml;
5
7
8internal sealed class JsonEncodingStreamWrapper : Stream
9{
10 private enum SupportedEncoding
11 {
12 UTF8,
13 UTF16LE,
14 UTF16BE,
15 None
16 }
17
18 private static readonly UnicodeEncoding s_validatingBEUTF16 = new UnicodeEncoding(bigEndian: true, byteOrderMark: false, throwOnInvalidBytes: true);
19
20 private static readonly UnicodeEncoding s_validatingUTF16 = new UnicodeEncoding(bigEndian: false, byteOrderMark: false, throwOnInvalidBytes: true);
21
23
24 private const int BufferLength = 128;
25
26 private readonly byte[] _byteBuffer = new byte[1];
27
28 private int _byteCount;
29
30 private int _byteOffset;
31
32 private byte[] _bytes;
33
34 private char[] _chars;
35
36 private Decoder _dec;
37
38 private Encoder _enc;
39
41
43
44 private readonly bool _isReading;
45
46 private Stream _stream;
47
48 public override bool CanRead
49 {
50 get
51 {
52 if (!_isReading)
53 {
54 return false;
55 }
56 return _stream.CanRead;
57 }
58 }
59
60 public override bool CanSeek => false;
61
62 public override bool CanTimeout => _stream.CanTimeout;
63
64 public override bool CanWrite
65 {
66 get
67 {
68 if (_isReading)
69 {
70 return false;
71 }
72 return _stream.CanWrite;
73 }
74 }
75
76 public override long Length => _stream.Length;
77
78 public override long Position
79 {
80 get
81 {
82 throw new NotSupportedException();
83 }
84 set
85 {
86 throw new NotSupportedException();
87 }
88 }
89
90 public override int ReadTimeout
91 {
92 get
93 {
94 return _stream.ReadTimeout;
95 }
96 set
97 {
99 }
100 }
101
102 public override int WriteTimeout
103 {
104 get
105 {
106 return _stream.WriteTimeout;
107 }
108 set
109 {
111 }
112 }
113
115 {
117 if (isReader)
118 {
119 InitForReading(stream, encoding);
120 return;
121 }
122 if (encoding == null)
123 {
124 throw new ArgumentNullException("encoding");
125 }
126 InitForWriting(stream, encoding);
127 }
128
150
151 protected override void Dispose(bool disposing)
152 {
153 Flush();
155 base.Dispose(disposing);
156 }
157
158 public override void Flush()
159 {
160 _stream.Flush();
161 }
162
163 public override int Read(byte[] buffer, int offset, int count)
164 {
165 try
166 {
167 if (_byteCount == 0)
168 {
170 {
171 return _stream.Read(buffer, offset, count);
172 }
173 _byteOffset = 0;
174 _byteCount = _stream.Read(_bytes, _byteCount, (_chars.Length - 1) * 2);
175 if (_byteCount == 0)
176 {
177 return 0;
178 }
181 _byteCount = Encoding.UTF8.GetBytes(_chars, 0, chars, _bytes, 0);
182 }
183 if (_byteCount < count)
184 {
186 }
189 _byteCount -= count;
190 return count;
191 }
193 {
195 }
196 }
197
198 public override int ReadByte()
199 {
200 if (_byteCount == 0 && _encodingCode == SupportedEncoding.UTF8)
201 {
202 return _stream.ReadByte();
203 }
204 if (Read(_byteBuffer, 0, 1) == 0)
205 {
206 return -1;
207 }
208 return _byteBuffer[0];
209 }
210
211 public override long Seek(long offset, SeekOrigin origin)
212 {
213 throw new NotSupportedException();
214 }
215
216 public override void SetLength(long value)
217 {
218 throw new NotSupportedException();
219 }
220
221 public override void Write(byte[] buffer, int offset, int count)
222 {
224 {
226 return;
227 }
228 while (count > 0)
229 {
230 int num = ((_chars.Length < count) ? _chars.Length : count);
231 int chars = _dec.GetChars(buffer, offset, num, _chars, 0, flush: false);
232 _byteCount = _enc.GetBytes(_chars, 0, chars, _bytes, 0, flush: false);
234 offset += num;
235 count -= num;
236 }
237 }
238
239 public override void WriteByte(byte b)
240 {
242 {
244 return;
245 }
246 _byteBuffer[0] = b;
247 Write(_byteBuffer, 0, 1);
248 }
249
260
261 private static string GetEncodingName(SupportedEncoding enc)
262 {
263 return enc switch
264 {
265 SupportedEncoding.UTF8 => "utf-8",
266 SupportedEncoding.UTF16LE => "utf-16LE",
267 SupportedEncoding.UTF16BE => "utf-16BE",
269 };
270 }
271
273 {
274 if (encoding == null)
275 {
276 return SupportedEncoding.None;
277 }
278 if (encoding.WebName == s_validatingUTF8.WebName)
279 {
280 return SupportedEncoding.UTF8;
281 }
282 if (encoding.WebName == s_validatingUTF16.WebName)
283 {
284 return SupportedEncoding.UTF16LE;
285 }
286 if (encoding.WebName == s_validatingBEUTF16.WebName)
287 {
288 return SupportedEncoding.UTF16BE;
289 }
291 }
292
293 private static SupportedEncoding ReadEncoding(byte b1, byte b2)
294 {
295 if (b1 == 0 && b2 != 0)
296 {
297 return SupportedEncoding.UTF16BE;
298 }
299 if (b1 != 0 && b2 == 0)
300 {
301 return SupportedEncoding.UTF16LE;
302 }
303 if (b1 == 0 && b2 == 0)
304 {
306 }
307 return SupportedEncoding.UTF8;
308 }
309
314
315 private void CleanupCharBreak()
316 {
317 int num = _byteOffset + _byteCount;
318 if (_byteCount % 2 != 0)
319 {
320 int num2 = _stream.ReadByte();
321 if (num2 < 0)
322 {
324 }
325 _bytes[num++] = (byte)num2;
326 _byteCount++;
327 }
328 int num3 = ((_encodingCode != SupportedEncoding.UTF16LE) ? (_bytes[num - 1] + (_bytes[num - 2] << 8)) : (_bytes[num - 2] + (_bytes[num - 1] << 8)));
329 if ((num3 & 0xDC00) != 56320 && num3 >= 55296 && num3 <= 56319)
330 {
331 int num4 = _stream.ReadByte();
332 int num5 = _stream.ReadByte();
333 if (num5 < 0)
334 {
336 }
337 _bytes[num++] = (byte)num4;
338 _bytes[num++] = (byte)num5;
339 _byteCount += 2;
340 }
341 }
342
343 [MemberNotNull("_chars")]
344 [MemberNotNull("_bytes")]
345 private void EnsureBuffers()
346 {
348 if (_chars == null)
349 {
350 _chars = new char[128];
351 }
352 }
353
354 [MemberNotNull("_bytes")]
355 private void EnsureByteBuffer()
356 {
357 if (_bytes == null)
358 {
359 _bytes = new byte[512];
360 _byteOffset = 0;
361 _byteCount = 0;
362 }
363 }
364
365 private void FillBuffer(int count)
366 {
367 count -= _byteCount;
368 while (count > 0)
369 {
371 if (num != 0)
372 {
373 _byteCount += num;
374 count -= num;
375 continue;
376 }
377 break;
378 }
379 }
380
409
422
424 {
425 int num = _stream.ReadByte();
426 int num2 = _stream.ReadByte();
428 SupportedEncoding result;
429 if (num == -1)
430 {
431 result = SupportedEncoding.UTF8;
432 _byteCount = 0;
433 }
434 else if (num2 == -1)
435 {
436 result = SupportedEncoding.UTF8;
437 _bytes[0] = (byte)num;
438 _byteCount = 1;
439 }
440 else
441 {
442 result = ReadEncoding((byte)num, (byte)num2);
443 _bytes[0] = (byte)num;
444 _bytes[1] = (byte)num2;
445 _byteCount = 2;
446 }
447 return result;
448 }
449}
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
Definition Buffer.cs:102
virtual int ReadByte()
Definition Stream.cs:994
virtual bool CanTimeout
Definition Stream.cs:498
virtual int ReadTimeout
Definition Stream.cs:505
int Read(byte[] buffer, int offset, int count)
void Dispose()
Definition Stream.cs:639
void Write(byte[] buffer, int offset, int count)
virtual void WriteByte(byte value)
Definition Stream.cs:1020
virtual int WriteTimeout
Definition Stream.cs:517
static void ThrowExpectedEncodingMismatch(SupportedEncoding expEnc, SupportedEncoding actualEnc)
JsonEncodingStreamWrapper(Stream stream, Encoding encoding, bool isReader)
void InitForReading(Stream inputStream, Encoding expectedEncoding)
static ArraySegment< byte > ProcessBuffer(byte[] buffer, int offset, int count, Encoding encoding)
static SupportedEncoding GetSupportedEncoding(Encoding encoding)
void InitForWriting(Stream outputStream, Encoding writeEncoding)
override void Write(byte[] buffer, int offset, int count)
override int Read(byte[] buffer, int offset, int count)
static string JsonEncodingNotSupported
Definition SR.cs:610
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string JsonUnexpectedEndOfFile
Definition SR.cs:614
static string JsonExpectedEncoding
Definition SR.cs:612
static string JsonInvalidBytes
Definition SR.cs:518
Definition SR.cs:7
int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool flush)
static Encoding UTF8
Definition Encoding.cs:526
virtual string WebName
Definition Encoding.cs:386
virtual Encoder GetEncoder()
Definition Encoding.cs:1009
virtual char[] GetChars(byte[] bytes)
Definition Encoding.cs:921