Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
BinaryReader.cs
Go to the documentation of this file.
3using System.Text;
4
5namespace System.IO;
6
8{
9 private readonly Stream _stream;
10
11 private readonly byte[] _buffer;
12
13 private readonly Decoder _decoder;
14
15 private byte[] _charBytes;
16
17 private char[] _charBuffer;
18
19 private readonly int _maxCharsSize;
20
21 private readonly bool _2BytesPerChar;
22
23 private readonly bool _isMemoryStream;
24
25 private readonly bool _leaveOpen;
26
27 private bool _disposed;
28
29 public virtual Stream BaseStream => _stream;
30
32 : this(input, Encoding.UTF8, leaveOpen: false)
33 {
34 }
35
37 : this(input, encoding, leaveOpen: false)
38 {
39 }
40
41 public BinaryReader(Stream input, Encoding encoding, bool leaveOpen)
42 {
43 if (input == null)
44 {
45 throw new ArgumentNullException("input");
46 }
47 if (encoding == null)
48 {
49 throw new ArgumentNullException("encoding");
50 }
51 if (!input.CanRead)
52 {
54 }
55 _stream = input;
56 _decoder = encoding.GetDecoder();
57 _maxCharsSize = encoding.GetMaxCharCount(128);
58 int num = encoding.GetMaxByteCount(1);
59 if (num < 16)
60 {
61 num = 16;
62 }
63 _buffer = new byte[num];
64 _2BytesPerChar = encoding is UnicodeEncoding;
65 _isMemoryStream = _stream.GetType() == typeof(MemoryStream);
66 _leaveOpen = leaveOpen;
67 }
68
69 protected virtual void Dispose(bool disposing)
70 {
71 if (!_disposed)
72 {
73 if (disposing && !_leaveOpen)
74 {
75 _stream.Close();
76 }
77 _disposed = true;
78 }
79 }
80
81 public void Dispose()
82 {
83 Dispose(disposing: true);
84 }
85
86 public virtual void Close()
87 {
88 Dispose(disposing: true);
89 }
90
91 private void ThrowIfDisposed()
92 {
93 if (_disposed)
94 {
96 }
97 }
98
99 public virtual int PeekChar()
100 {
102 if (!_stream.CanSeek)
103 {
104 return -1;
105 }
106 long position = _stream.Position;
107 int result = Read();
108 _stream.Position = position;
109 return result;
110 }
111
112 public virtual int Read()
113 {
115 int num = 0;
116 long num2 = 0L;
117 if (_stream.CanSeek)
118 {
119 num2 = _stream.Position;
120 }
121 if (_charBytes == null)
122 {
123 _charBytes = new byte[128];
124 }
125 Span<char> chars = stackalloc char[1];
126 while (num == 0)
127 {
128 int num3 = ((!_2BytesPerChar) ? 1 : 2);
129 int num4 = _stream.ReadByte();
130 _charBytes[0] = (byte)num4;
131 if (num4 == -1)
132 {
133 num3 = 0;
134 }
135 if (num3 == 2)
136 {
137 num4 = _stream.ReadByte();
138 _charBytes[1] = (byte)num4;
139 if (num4 == -1)
140 {
141 num3 = 1;
142 }
143 }
144 if (num3 == 0)
145 {
146 return -1;
147 }
148 try
149 {
150 num = _decoder.GetChars(new ReadOnlySpan<byte>(_charBytes, 0, num3), chars, flush: false);
151 }
152 catch
153 {
154 if (_stream.CanSeek)
155 {
156 _stream.Seek(num2 - _stream.Position, SeekOrigin.Current);
157 }
158 throw;
159 }
160 }
161 return chars[0];
162 }
163
164 public virtual byte ReadByte()
165 {
166 return InternalReadByte();
167 }
168
169 [MethodImpl(MethodImplOptions.AggressiveInlining)]
170 private byte InternalReadByte()
171 {
173 int num = _stream.ReadByte();
174 if (num == -1)
175 {
177 }
178 return (byte)num;
179 }
180
181 [CLSCompliant(false)]
182 public virtual sbyte ReadSByte()
183 {
184 return (sbyte)InternalReadByte();
185 }
186
187 public virtual bool ReadBoolean()
188 {
189 return InternalReadByte() != 0;
190 }
191
192 public virtual char ReadChar()
193 {
194 int num = Read();
195 if (num == -1)
196 {
198 }
199 return (char)num;
200 }
201
202 public virtual short ReadInt16()
203 {
205 }
206
207 [CLSCompliant(false)]
208 public virtual ushort ReadUInt16()
209 {
211 }
212
213 public virtual int ReadInt32()
214 {
216 }
217
218 [CLSCompliant(false)]
219 public virtual uint ReadUInt32()
220 {
222 }
223
224 public virtual long ReadInt64()
225 {
227 }
228
229 [CLSCompliant(false)]
230 public virtual ulong ReadUInt64()
231 {
233 }
234
239
244
249
250 public virtual decimal ReadDecimal()
251 {
253 try
254 {
255 return decimal.ToDecimal(span);
256 }
257 catch (ArgumentException innerException)
258 {
259 throw new IOException(SR.Arg_DecBitCtor, innerException);
260 }
261 }
262
263 public virtual string ReadString()
264 {
266 int num = 0;
267 int num2 = Read7BitEncodedInt();
268 if (num2 < 0)
269 {
271 }
272 if (num2 == 0)
273 {
274 return string.Empty;
275 }
276 if (_charBytes == null)
277 {
278 _charBytes = new byte[128];
279 }
280 if (_charBuffer == null)
281 {
282 _charBuffer = new char[_maxCharsSize];
283 }
284 StringBuilder stringBuilder = null;
285 do
286 {
287 int count = ((num2 - num > 128) ? 128 : (num2 - num));
288 int num3 = _stream.Read(_charBytes, 0, count);
289 if (num3 == 0)
290 {
292 }
293 int chars = _decoder.GetChars(_charBytes, 0, num3, _charBuffer, 0);
294 if (num == 0 && num3 == num2)
295 {
296 return new string(_charBuffer, 0, chars);
297 }
298 if (stringBuilder == null)
299 {
300 stringBuilder = StringBuilderCache.Acquire(Math.Min(num2, 360));
301 }
302 stringBuilder.Append(_charBuffer, 0, chars);
303 num += num3;
304 }
305 while (num < num2);
306 return StringBuilderCache.GetStringAndRelease(stringBuilder);
307 }
308
309 public virtual int Read(char[] buffer, int index, int count)
310 {
311 if (buffer == null)
312 {
313 throw new ArgumentNullException("buffer", SR.ArgumentNull_Buffer);
314 }
315 if (index < 0)
316 {
318 }
319 if (count < 0)
320 {
322 }
323 if (buffer.Length - index < count)
324 {
326 }
329 }
330
331 public virtual int Read(Span<char> buffer)
332 {
335 }
336
338 {
339 int num = 0;
340 while (!buffer.IsEmpty)
341 {
342 int num2 = buffer.Length;
343 if (_2BytesPerChar)
344 {
345 num2 <<= 1;
346 }
347 if (num2 > 1 && !(_decoder is DecoderNLS { HasState: false }))
348 {
349 num2--;
350 if (_2BytesPerChar && num2 > 2)
351 {
352 num2 -= 2;
353 }
354 }
356 if (_isMemoryStream)
357 {
358 MemoryStream memoryStream = (MemoryStream)_stream;
359 int start = memoryStream.InternalGetPosition();
360 num2 = memoryStream.InternalEmulateRead(num2);
361 bytes = new ReadOnlySpan<byte>(memoryStream.InternalGetBuffer(), start, num2);
362 }
363 else
364 {
365 if (_charBytes == null)
366 {
367 _charBytes = new byte[128];
368 }
369 if (num2 > 128)
370 {
371 num2 = 128;
372 }
373 num2 = _stream.Read(_charBytes, 0, num2);
374 bytes = new ReadOnlySpan<byte>(_charBytes, 0, num2);
375 }
376 if (bytes.IsEmpty)
377 {
378 break;
379 }
380 int chars = _decoder.GetChars(bytes, buffer, flush: false);
381 buffer = buffer.Slice(chars);
382 num += chars;
383 }
384 return num;
385 }
386
387 public virtual char[] ReadChars(int count)
388 {
389 if (count < 0)
390 {
392 }
394 if (count == 0)
395 {
396 return Array.Empty<char>();
397 }
398 char[] array = new char[count];
399 int num = InternalReadChars(new Span<char>(array));
400 if (num != count)
401 {
402 char[] array2 = new char[num];
403 Buffer.BlockCopy(array, 0, array2, 0, 2 * num);
404 array = array2;
405 }
406 return array;
407 }
408
409 public virtual int Read(byte[] buffer, int index, int count)
410 {
411 if (buffer == null)
412 {
413 throw new ArgumentNullException("buffer", SR.ArgumentNull_Buffer);
414 }
415 if (index < 0)
416 {
418 }
419 if (count < 0)
420 {
422 }
423 if (buffer.Length - index < count)
424 {
426 }
428 return _stream.Read(buffer, index, count);
429 }
430
431 public virtual int Read(Span<byte> buffer)
432 {
434 return _stream.Read(buffer);
435 }
436
437 public virtual byte[] ReadBytes(int count)
438 {
439 if (count < 0)
440 {
442 }
444 if (count == 0)
445 {
446 return Array.Empty<byte>();
447 }
448 byte[] array = new byte[count];
449 int num = 0;
450 do
451 {
452 int num2 = _stream.Read(array, num, count);
453 if (num2 == 0)
454 {
455 break;
456 }
457 num += num2;
458 count -= num2;
459 }
460 while (count > 0);
461 if (num != array.Length)
462 {
463 byte[] array2 = new byte[num];
464 Buffer.BlockCopy(array, 0, array2, 0, num);
465 array = array2;
466 }
467 return array;
468 }
469
470 private ReadOnlySpan<byte> InternalRead(int numBytes)
471 {
472 if (_isMemoryStream)
473 {
474 return ((MemoryStream)_stream).InternalReadSpan(numBytes);
475 }
477 int num = 0;
478 do
479 {
480 int num2 = _stream.Read(_buffer, num, numBytes - num);
481 if (num2 == 0)
482 {
484 }
485 num += num2;
486 }
487 while (num < numBytes);
488 return _buffer;
489 }
490
491 protected virtual void FillBuffer(int numBytes)
492 {
493 if (numBytes < 0 || numBytes > _buffer.Length)
494 {
496 }
497 int num = 0;
498 int num2 = 0;
500 if (numBytes == 1)
501 {
502 num2 = _stream.ReadByte();
503 if (num2 == -1)
504 {
506 }
507 _buffer[0] = (byte)num2;
508 return;
509 }
510 do
511 {
512 num2 = _stream.Read(_buffer, num, numBytes - num);
513 if (num2 == 0)
514 {
516 }
517 num += num2;
518 }
519 while (num < numBytes);
520 }
521
523 {
524 uint num = 0u;
525 byte b;
526 for (int i = 0; i < 28; i += 7)
527 {
528 b = ReadByte();
529 num |= (uint)((b & 0x7F) << i);
530 if ((uint)b <= 127u)
531 {
532 return (int)num;
533 }
534 }
535 b = ReadByte();
536 if ((uint)b > 15u)
537 {
539 }
540 return (int)num | (b << 28);
541 }
542
544 {
545 ulong num = 0uL;
546 byte b;
547 for (int i = 0; i < 63; i += 7)
548 {
549 b = ReadByte();
550 num |= ((ulong)b & 0x7FuL) << i;
551 if ((uint)b <= 127u)
552 {
553 return (long)num;
554 }
555 }
556 b = ReadByte();
557 if ((uint)b > 1u)
558 {
560 }
561 return (long)(num | ((ulong)b << 63));
562 }
563}
static unsafe float Int32BitsToSingle(int value)
static unsafe double Int64BitsToDouble(long value)
static unsafe Half Int16BitsToHalf(short value)
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
Definition Buffer.cs:102
static short ReadInt16LittleEndian(ReadOnlySpan< byte > source)
static ulong ReadUInt64LittleEndian(ReadOnlySpan< byte > source)
static ushort ReadUInt16LittleEndian(ReadOnlySpan< byte > source)
static uint ReadUInt32LittleEndian(ReadOnlySpan< byte > source)
static int ReadInt32LittleEndian(ReadOnlySpan< byte > source)
static long ReadInt64LittleEndian(ReadOnlySpan< byte > source)
virtual int PeekChar()
virtual int Read(byte[] buffer, int index, int count)
ReadOnlySpan< byte > InternalRead(int numBytes)
readonly bool _leaveOpen
virtual bool ReadBoolean()
virtual double ReadDouble()
virtual byte ReadByte()
BinaryReader(Stream input, Encoding encoding, bool leaveOpen)
virtual int Read(Span< byte > buffer)
int InternalReadChars(Span< char > buffer)
readonly Decoder _decoder
BinaryReader(Stream input, Encoding encoding)
virtual string ReadString()
virtual void Dispose(bool disposing)
readonly Stream _stream
virtual ulong ReadUInt64()
virtual float ReadSingle()
virtual void FillBuffer(int numBytes)
BinaryReader(Stream input)
virtual sbyte ReadSByte()
virtual ushort ReadUInt16()
virtual char[] ReadChars(int count)
readonly bool _2BytesPerChar
virtual int ReadInt32()
readonly int _maxCharsSize
virtual Stream BaseStream
virtual void Close()
readonly bool _isMemoryStream
virtual short ReadInt16()
virtual int Read(Span< char > buffer)
virtual byte[] ReadBytes(int count)
virtual int Read(char[] buffer, int index, int count)
readonly byte[] _buffer
virtual decimal ReadDecimal()
virtual long ReadInt64()
virtual uint ReadUInt32()
virtual Half ReadHalf()
virtual char ReadChar()
int InternalEmulateRead(int count)
virtual int ReadByte()
Definition Stream.cs:994
long Seek(long offset, SeekOrigin origin)
virtual void Close()
Definition Stream.cs:644
int Read(byte[] buffer, int offset, int count)
static byte Min(byte val1, byte val2)
Definition Math.cs:912
static string Format_Bad7BitInt
Definition SR.cs:1318
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Arg_DecBitCtor
Definition SR.cs:114
static string Argument_StreamNotReadable
Definition SR.cs:876
static string IO_InvalidStringLen_Len
Definition SR.cs:1574
static string ArgumentNull_Buffer
Definition SR.cs:22
static string ArgumentOutOfRange_BinaryReaderFillBuffer
Definition SR.cs:978
static string Argument_InvalidOffLen
Definition SR.cs:22
static string ArgumentOutOfRange_NeedNonNegNum
Definition SR.cs:32
Definition SR.cs:7
int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
int GetMaxCharCount(int byteCount)
int GetMaxByteCount(int charCount)
virtual Decoder GetDecoder()
Definition Encoding.cs:1004
static string GetStringAndRelease(StringBuilder sb)
static StringBuilder Acquire(int capacity=16)
StringBuilder Append(char value, int repeatCount)
static void ThrowObjectDisposedException_FileClosed()
static void ThrowEndOfFileException()