mscorlib(4.0.0.0) API with additions
BinaryReader.cs
2 using System.Security;
3 using System.Text;
4 
5 namespace System.IO
6 {
8  [ComVisible(true)]
9  [__DynamicallyInvokable]
10  public class BinaryReader : IDisposable
11  {
12  private const int MaxCharBytesSize = 128;
13 
14  private Stream m_stream;
15 
16  private byte[] m_buffer;
17 
18  private Decoder m_decoder;
19 
20  private byte[] m_charBytes;
21 
22  private char[] m_singleChar;
23 
24  private char[] m_charBuffer;
25 
26  private int m_maxCharsSize;
27 
28  private bool m_2BytesPerChar;
29 
30  private bool m_isMemoryStream;
31 
32  private bool m_leaveOpen;
33 
36  [__DynamicallyInvokable]
37  public virtual Stream BaseStream
38  {
39  [__DynamicallyInvokable]
40  get
41  {
42  return m_stream;
43  }
44  }
45 
49  [__DynamicallyInvokable]
50  public BinaryReader(Stream input)
51  : this(input, new UTF8Encoding(), leaveOpen: false)
52  {
53  }
54 
61  [__DynamicallyInvokable]
62  public BinaryReader(Stream input, Encoding encoding)
63  : this(input, encoding, leaveOpen: false)
64  {
65  }
66 
75  [__DynamicallyInvokable]
76  public BinaryReader(Stream input, Encoding encoding, bool leaveOpen)
77  {
78  if (input == null)
79  {
80  throw new ArgumentNullException("input");
81  }
82  if (encoding == null)
83  {
84  throw new ArgumentNullException("encoding");
85  }
86  if (!input.CanRead)
87  {
88  throw new ArgumentException(Environment.GetResourceString("Argument_StreamNotReadable"));
89  }
90  m_stream = input;
91  m_decoder = encoding.GetDecoder();
92  m_maxCharsSize = encoding.GetMaxCharCount(128);
93  int num = encoding.GetMaxByteCount(1);
94  if (num < 16)
95  {
96  num = 16;
97  }
98  m_buffer = new byte[num];
99  m_2BytesPerChar = (encoding is UnicodeEncoding);
100  m_isMemoryStream = (m_stream.GetType() == typeof(MemoryStream));
101  m_leaveOpen = leaveOpen;
102  }
103 
105  public virtual void Close()
106  {
107  Dispose(disposing: true);
108  }
109 
113  [__DynamicallyInvokable]
114  protected virtual void Dispose(bool disposing)
115  {
116  if (disposing)
117  {
118  Stream stream = m_stream;
119  m_stream = null;
120  if (stream != null && !m_leaveOpen)
121  {
122  stream.Close();
123  }
124  }
125  m_stream = null;
126  m_buffer = null;
127  m_decoder = null;
128  m_charBytes = null;
129  m_singleChar = null;
130  m_charBuffer = null;
131  }
132 
134  [__DynamicallyInvokable]
135  public void Dispose()
136  {
137  Dispose(disposing: true);
138  }
139 
144  [__DynamicallyInvokable]
145  public virtual int PeekChar()
146  {
147  if (m_stream == null)
148  {
149  __Error.FileNotOpen();
150  }
151  if (!m_stream.CanSeek)
152  {
153  return -1;
154  }
155  long position = m_stream.Position;
156  int result = Read();
157  m_stream.Position = position;
158  return result;
159  }
160 
165  [__DynamicallyInvokable]
166  public virtual int Read()
167  {
168  if (m_stream == null)
169  {
170  __Error.FileNotOpen();
171  }
172  return InternalReadOneChar();
173  }
174 
181  [__DynamicallyInvokable]
182  public virtual bool ReadBoolean()
183  {
184  FillBuffer(1);
185  return m_buffer[0] != 0;
186  }
187 
193  [__DynamicallyInvokable]
194  public virtual byte ReadByte()
195  {
196  if (m_stream == null)
197  {
198  __Error.FileNotOpen();
199  }
200  int num = m_stream.ReadByte();
201  if (num == -1)
202  {
203  __Error.EndOfFile();
204  }
205  return (byte)num;
206  }
207 
213  [CLSCompliant(false)]
214  [__DynamicallyInvokable]
215  public virtual sbyte ReadSByte()
216  {
217  FillBuffer(1);
218  return (sbyte)m_buffer[0];
219  }
220 
227  [__DynamicallyInvokable]
228  public virtual char ReadChar()
229  {
230  int num = Read();
231  if (num == -1)
232  {
233  __Error.EndOfFile();
234  }
235  return (char)num;
236  }
237 
243  [__DynamicallyInvokable]
244  public virtual short ReadInt16()
245  {
246  FillBuffer(2);
247  return (short)(m_buffer[0] | (m_buffer[1] << 8));
248  }
249 
255  [CLSCompliant(false)]
256  [__DynamicallyInvokable]
257  public virtual ushort ReadUInt16()
258  {
259  FillBuffer(2);
260  return (ushort)(m_buffer[0] | (m_buffer[1] << 8));
261  }
262 
268  [__DynamicallyInvokable]
269  public virtual int ReadInt32()
270  {
271  if (m_isMemoryStream)
272  {
273  if (m_stream == null)
274  {
275  __Error.FileNotOpen();
276  }
277  MemoryStream memoryStream = m_stream as MemoryStream;
278  return memoryStream.InternalReadInt32();
279  }
280  FillBuffer(4);
281  return m_buffer[0] | (m_buffer[1] << 8) | (m_buffer[2] << 16) | (m_buffer[3] << 24);
282  }
283 
289  [CLSCompliant(false)]
290  [__DynamicallyInvokable]
291  public virtual uint ReadUInt32()
292  {
293  FillBuffer(4);
294  return (uint)(m_buffer[0] | (m_buffer[1] << 8) | (m_buffer[2] << 16) | (m_buffer[3] << 24));
295  }
296 
302  [__DynamicallyInvokable]
303  public virtual long ReadInt64()
304  {
305  FillBuffer(8);
306  uint num = (uint)(m_buffer[0] | (m_buffer[1] << 8) | (m_buffer[2] << 16) | (m_buffer[3] << 24));
307  uint num2 = (uint)(m_buffer[4] | (m_buffer[5] << 8) | (m_buffer[6] << 16) | (m_buffer[7] << 24));
308  return (long)(((ulong)num2 << 32) | num);
309  }
310 
316  [CLSCompliant(false)]
317  [__DynamicallyInvokable]
318  public virtual ulong ReadUInt64()
319  {
320  FillBuffer(8);
321  uint num = (uint)(m_buffer[0] | (m_buffer[1] << 8) | (m_buffer[2] << 16) | (m_buffer[3] << 24));
322  uint num2 = (uint)(m_buffer[4] | (m_buffer[5] << 8) | (m_buffer[6] << 16) | (m_buffer[7] << 24));
323  return ((ulong)num2 << 32) | num;
324  }
325 
331  [SecuritySafeCritical]
332  [__DynamicallyInvokable]
333  public unsafe virtual float ReadSingle()
334  {
335  FillBuffer(4);
336  uint num = (uint)(m_buffer[0] | (m_buffer[1] << 8) | (m_buffer[2] << 16) | (m_buffer[3] << 24));
337  return *(float*)(&num);
338  }
339 
345  [SecuritySafeCritical]
346  [__DynamicallyInvokable]
347  public unsafe virtual double ReadDouble()
348  {
349  FillBuffer(8);
350  uint num = (uint)(m_buffer[0] | (m_buffer[1] << 8) | (m_buffer[2] << 16) | (m_buffer[3] << 24));
351  uint num2 = (uint)(m_buffer[4] | (m_buffer[5] << 8) | (m_buffer[6] << 16) | (m_buffer[7] << 24));
352  ulong num3 = ((ulong)num2 << 32) | num;
353  return *(double*)(&num3);
354  }
355 
361  [__DynamicallyInvokable]
362  public virtual decimal ReadDecimal()
363  {
364  FillBuffer(16);
365  try
366  {
367  return decimal.ToDecimal(m_buffer);
368  }
369  catch (ArgumentException innerException)
370  {
371  throw new IOException(Environment.GetResourceString("Arg_DecBitCtor"), innerException);
372  }
373  }
374 
380  [__DynamicallyInvokable]
381  public virtual string ReadString()
382  {
383  if (m_stream == null)
384  {
385  __Error.FileNotOpen();
386  }
387  int num = 0;
388  int num2 = Read7BitEncodedInt();
389  if (num2 < 0)
390  {
391  throw new IOException(Environment.GetResourceString("IO.IO_InvalidStringLen_Len", num2));
392  }
393  if (num2 == 0)
394  {
395  return string.Empty;
396  }
397  if (m_charBytes == null)
398  {
399  m_charBytes = new byte[128];
400  }
401  if (m_charBuffer == null)
402  {
403  m_charBuffer = new char[m_maxCharsSize];
404  }
405  StringBuilder stringBuilder = null;
406  do
407  {
408  int count = (num2 - num > 128) ? 128 : (num2 - num);
409  int num3 = m_stream.Read(m_charBytes, 0, count);
410  if (num3 == 0)
411  {
412  __Error.EndOfFile();
413  }
414  int chars = m_decoder.GetChars(m_charBytes, 0, num3, m_charBuffer, 0);
415  if (num == 0 && num3 == num2)
416  {
417  return new string(m_charBuffer, 0, chars);
418  }
419  if (stringBuilder == null)
420  {
421  stringBuilder = StringBuilderCache.Acquire(num2);
422  }
423  stringBuilder.Append(m_charBuffer, 0, chars);
424  num += num3;
425  }
426  while (num < num2);
427  return StringBuilderCache.GetStringAndRelease(stringBuilder);
428  }
429 
442  [SecuritySafeCritical]
443  [__DynamicallyInvokable]
444  public virtual int Read(char[] buffer, int index, int count)
445  {
446  if (buffer == null)
447  {
448  throw new ArgumentNullException("buffer", Environment.GetResourceString("ArgumentNull_Buffer"));
449  }
450  if (index < 0)
451  {
452  throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
453  }
454  if (count < 0)
455  {
456  throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
457  }
458  if (buffer.Length - index < count)
459  {
460  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
461  }
462  if (m_stream == null)
463  {
464  __Error.FileNotOpen();
465  }
466  return InternalReadChars(buffer, index, count);
467  }
468 
469  [SecurityCritical]
470  private unsafe int InternalReadChars(char[] buffer, int index, int count)
471  {
472  int num = 0;
473  int num2 = count;
474  if (m_charBytes == null)
475  {
476  m_charBytes = new byte[128];
477  }
478  while (num2 > 0)
479  {
480  int num3 = 0;
481  num = num2;
482  DecoderNLS decoderNLS = m_decoder as DecoderNLS;
483  if (decoderNLS != null && decoderNLS.HasState && num > 1)
484  {
485  num--;
486  }
487  if (m_2BytesPerChar)
488  {
489  num <<= 1;
490  }
491  if (num > 128)
492  {
493  num = 128;
494  }
495  int num4 = 0;
496  byte[] array = null;
497  if (m_isMemoryStream)
498  {
499  MemoryStream memoryStream = m_stream as MemoryStream;
500  num4 = memoryStream.InternalGetPosition();
501  num = memoryStream.InternalEmulateRead(num);
502  array = memoryStream.InternalGetBuffer();
503  }
504  else
505  {
506  num = m_stream.Read(m_charBytes, 0, num);
507  array = m_charBytes;
508  }
509  if (num == 0)
510  {
511  return count - num2;
512  }
513  byte[] array2;
514  checked
515  {
516  if (num4 < 0 || num < 0 || num4 + num > array.Length)
517  {
518  throw new ArgumentOutOfRangeException("byteCount");
519  }
520  if (index < 0 || num2 < 0 || index + num2 > buffer.Length)
521  {
522  throw new ArgumentOutOfRangeException("charsRemaining");
523  }
524  array2 = array;
525  }
526  fixed (byte* ptr = array2)
527  {
528  fixed (char* ptr2 = buffer)
529  {
530  num3 = m_decoder.GetChars((byte*)checked(unchecked((ulong)ptr) + unchecked((ulong)num4)), num, (char*)checked(unchecked((ulong)ptr2) + unchecked((ulong)(UIntPtr)(void*)checked(unchecked((long)index) * 2L))), num2, flush: false);
531  }
532  }
533  num2 -= num3;
534  index += num3;
535  }
536  return count - num2;
537  }
538 
539  private int InternalReadOneChar()
540  {
541  int num = 0;
542  int num2 = 0;
543  long num3 = num3 = 0L;
544  if (m_stream.CanSeek)
545  {
546  num3 = m_stream.Position;
547  }
548  if (m_charBytes == null)
549  {
550  m_charBytes = new byte[128];
551  }
552  if (m_singleChar == null)
553  {
554  m_singleChar = new char[1];
555  }
556  while (num == 0)
557  {
558  num2 = ((!m_2BytesPerChar) ? 1 : 2);
559  int num4 = m_stream.ReadByte();
560  m_charBytes[0] = (byte)num4;
561  if (num4 == -1)
562  {
563  num2 = 0;
564  }
565  if (num2 == 2)
566  {
567  num4 = m_stream.ReadByte();
568  m_charBytes[1] = (byte)num4;
569  if (num4 == -1)
570  {
571  num2 = 1;
572  }
573  }
574  if (num2 == 0)
575  {
576  return -1;
577  }
578  try
579  {
580  num = m_decoder.GetChars(m_charBytes, 0, num2, m_singleChar, 0);
581  }
582  catch
583  {
584  if (m_stream.CanSeek)
585  {
586  m_stream.Seek(num3 - m_stream.Position, SeekOrigin.Current);
587  }
588  throw;
589  }
590  }
591  if (num == 0)
592  {
593  return -1;
594  }
595  return m_singleChar[0];
596  }
597 
606  [SecuritySafeCritical]
607  [__DynamicallyInvokable]
608  public virtual char[] ReadChars(int count)
609  {
610  if (count < 0)
611  {
612  throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
613  }
614  if (m_stream == null)
615  {
616  __Error.FileNotOpen();
617  }
618  if (count == 0)
619  {
620  return EmptyArray<char>.Value;
621  }
622  char[] array = new char[count];
623  int num = InternalReadChars(array, 0, count);
624  if (num != count)
625  {
626  char[] array2 = new char[num];
627  Buffer.InternalBlockCopy(array, 0, array2, 0, 2 * num);
628  array = array2;
629  }
630  return array;
631  }
632 
645  [__DynamicallyInvokable]
646  public virtual int Read(byte[] buffer, int index, int count)
647  {
648  if (buffer == null)
649  {
650  throw new ArgumentNullException("buffer", Environment.GetResourceString("ArgumentNull_Buffer"));
651  }
652  if (index < 0)
653  {
654  throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
655  }
656  if (count < 0)
657  {
658  throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
659  }
660  if (buffer.Length - index < count)
661  {
662  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
663  }
664  if (m_stream == null)
665  {
666  __Error.FileNotOpen();
667  }
668  return m_stream.Read(buffer, index, count);
669  }
670 
679  [__DynamicallyInvokable]
680  public virtual byte[] ReadBytes(int count)
681  {
682  if (count < 0)
683  {
684  throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
685  }
686  if (m_stream == null)
687  {
688  __Error.FileNotOpen();
689  }
690  if (count == 0)
691  {
692  return EmptyArray<byte>.Value;
693  }
694  byte[] array = new byte[count];
695  int num = 0;
696  do
697  {
698  int num2 = m_stream.Read(array, num, count);
699  if (num2 == 0)
700  {
701  break;
702  }
703  num += num2;
704  count -= num2;
705  }
706  while (count > 0);
707  if (num != array.Length)
708  {
709  byte[] array2 = new byte[num];
710  Buffer.InternalBlockCopy(array, 0, array2, 0, num);
711  array = array2;
712  }
713  return array;
714  }
715 
721  [__DynamicallyInvokable]
722  protected virtual void FillBuffer(int numBytes)
723  {
724  if (m_buffer != null && (numBytes < 0 || numBytes > m_buffer.Length))
725  {
726  throw new ArgumentOutOfRangeException("numBytes", Environment.GetResourceString("ArgumentOutOfRange_BinaryReaderFillBuffer"));
727  }
728  int num = 0;
729  int num2 = 0;
730  if (m_stream == null)
731  {
732  __Error.FileNotOpen();
733  }
734  if (numBytes == 1)
735  {
736  num2 = m_stream.ReadByte();
737  if (num2 == -1)
738  {
739  __Error.EndOfFile();
740  }
741  m_buffer[0] = (byte)num2;
742  return;
743  }
744  do
745  {
746  num2 = m_stream.Read(m_buffer, num, numBytes - num);
747  if (num2 == 0)
748  {
749  __Error.EndOfFile();
750  }
751  num += num2;
752  }
753  while (num < numBytes);
754  }
755 
762  [__DynamicallyInvokable]
763  protected internal int Read7BitEncodedInt()
764  {
765  int num = 0;
766  int num2 = 0;
767  byte b;
768  do
769  {
770  if (num2 == 35)
771  {
772  throw new FormatException(Environment.GetResourceString("Format_Bad7BitInt32"));
773  }
774  b = ReadByte();
775  num |= (b & 0x7F) << num2;
776  num2 += 7;
777  }
778  while ((b & 0x80) != 0);
779  return num;
780  }
781  }
782 }
Represents a character encoding.To browse the .NET Framework source code for this type,...
Definition: Encoding.cs:15
virtual int Read(char[] buffer, int index, int count)
Reads the specified number of characters from the stream, starting from a specified point in the char...
BinaryReader(Stream input, Encoding encoding)
Initializes a new instance of the T:System.IO.BinaryReader class based on the specified stream and ch...
Definition: BinaryReader.cs:62
virtual void Close()
Closes the current reader and the underlying stream.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
abstract int Read([In] [Out] byte[] buffer, int offset, int count)
When overridden in a derived class, reads a sequence of bytes from the current stream and advances th...
virtual char [] ReadChars(int count)
Reads the specified number of characters from the current stream, returns the data in a character arr...
virtual int ReadInt32()
Reads a 4-byte signed integer from the current stream and advances the current position of the stream...
abstract bool CanSeek
When overridden in a derived class, gets a value indicating whether the current stream supports seeki...
Definition: Stream.cs:587
virtual ulong ReadUInt64()
Reads an 8-byte unsigned integer from the current stream and advances the position of the stream by e...
Provides a mechanism for releasing unmanaged resources.To browse the .NET Framework source code for t...
Definition: IDisposable.cs:8
abstract int GetMaxCharCount(int byteCount)
When overridden in a derived class, calculates the maximum number of characters produced by decoding ...
BinaryReader(Stream input)
Initializes a new instance of the T:System.IO.BinaryReader class based on the specified stream and us...
Definition: BinaryReader.cs:50
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
virtual int Read(byte[] buffer, int index, int count)
Reads the specified number of bytes from the stream, starting from a specified point in the byte arra...
BinaryReader(Stream input, Encoding encoding, bool leaveOpen)
Initializes a new instance of the T:System.IO.BinaryReader class based on the specified stream and ch...
Definition: BinaryReader.cs:76
virtual decimal ReadDecimal()
Reads a decimal value from the current stream and advances the current position of the stream by sixt...
virtual Decoder GetDecoder()
When overridden in a derived class, obtains a decoder that converts an encoded sequence of bytes into...
Definition: Encoding.cs:1953
virtual byte ReadByte()
Reads the next byte from the current stream and advances the current position of the stream by one by...
virtual ushort ReadUInt16()
Reads a 2-byte unsigned integer from the current stream using little-endian encoding and advances the...
SeekOrigin
Specifies the position in a stream to use for seeking.
Definition: SeekOrigin.cs:9
virtual short ReadInt16()
Reads a 2-byte signed integer from the current stream and advances the current position of the stream...
abstract bool CanRead
When overridden in a derived class, gets a value indicating whether the current stream supports readi...
Definition: Stream.cs:577
virtual uint ReadUInt32()
Reads a 4-byte unsigned integer from the current stream and advances the position of the stream by fo...
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
virtual void Close()
Closes the current stream and releases any resources (such as sockets and file handles) associated wi...
Definition: Stream.cs:855
virtual sbyte ReadSByte()
Reads a signed byte from this stream and advances the current position of the stream by one byte.
Creates a stream whose backing store is memory.To browse the .NET Framework source code for this type...
Definition: MemoryStream.cs:13
StringBuilder Append(char value, int repeatCount)
Appends a specified number of copies of the string representation of a Unicode character to this inst...
Represents a UTF-16 encoding of Unicode characters.
The exception that is thrown when the format of an argument is invalid, or when a composite format st...
virtual byte [] ReadBytes(int count)
Reads the specified number of bytes from the current stream into a byte array and advances the curren...
virtual bool ReadBoolean()
Reads a Boolean value from the current stream and advances the current position of the stream by one ...
Reads primitive data types as binary values in a specific encoding.
Definition: BinaryReader.cs:10
Converts a sequence of encoded bytes into a set of characters.
Definition: Decoder.cs:11
The exception that is thrown when an I/O error occurs.
Definition: IOException.cs:10
Represents a UTF-8 encoding of Unicode characters.
Definition: UTF8Encoding.cs:11
virtual int ReadByte()
Reads a byte from the stream and advances the position within the stream by one byte,...
Definition: Stream.cs:1297
virtual int Read()
Reads characters from the underlying stream and advances the current position of the stream in accord...
abstract long Seek(long offset, SeekOrigin origin)
When overridden in a derived class, sets the position within the current stream.
virtual void Dispose(bool disposing)
Releases the unmanaged resources used by the T:System.IO.BinaryReader class and optionally releases t...
Represents a mutable string of characters. This class cannot be inherited.To browse the ....
virtual Stream BaseStream
Exposes access to the underlying stream of the T:System.IO.BinaryReader.
Definition: BinaryReader.cs:38
The exception that is thrown when one of the arguments provided to a method is not valid.
virtual char ReadChar()
Reads the next character from the current stream and advances the current position of the stream in a...
virtual unsafe float ReadSingle()
Reads a 4-byte floating point value from the current stream and advances the current position of the ...
void Dispose()
Releases all resources used by the current instance of the T:System.IO.BinaryReader class.
abstract int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
When overridden in a derived class, decodes a sequence of bytes from the specified byte array and any...
abstract long Position
When overridden in a derived class, gets or sets the position within the current stream.
Definition: Stream.cs:633
virtual unsafe double ReadDouble()
Reads an 8-byte floating point value from the current stream and advances the current position of the...
Manipulates arrays of primitive types.
Definition: Buffer.cs:11
virtual void FillBuffer(int numBytes)
Fills the internal buffer with the specified number of bytes read from the stream.
virtual string ReadString()
Reads a string from the current stream. The string is prefixed with the length, encoded as an integer...
virtual long ReadInt64()
Reads an 8-byte signed integer from the current stream and advances the current position of the strea...
abstract int GetMaxByteCount(int charCount)
When overridden in a derived class, calculates the maximum number of bytes produced by encoding the s...
internal int Read7BitEncodedInt()
Reads in a 32-bit integer in compressed format.
virtual int PeekChar()
Returns the next available character and does not advance the byte or character position.
Provides a generic view of a sequence of bytes. This is an abstract class.To browse the ....
Definition: Stream.cs:16