2 // System.IO.BinaryReader
5 // Matt Kimball (matt@kimball.net)
6 // Dick Porter (dick@ximian.com)
11 using System.Globalization;
14 public class BinaryReader : IDisposable {
17 int m_encoding_max_byte;
21 public BinaryReader(Stream input) : this(input, Encoding.UTF8) {
24 public BinaryReader(Stream input, Encoding encoding) {
25 if (input == null || encoding == null)
26 throw new ArgumentNullException(Locale.GetText ("Input or Encoding is a null reference."));
28 throw new ArgumentException(Locale.GetText ("The stream doesn't support reading."));
31 m_encoding = encoding;
32 m_encoding_max_byte = m_encoding.GetMaxByteCount(1);
33 m_buffer = new byte [32];
36 public virtual Stream BaseStream {
42 public virtual void Close() {
46 protected virtual void Dispose (bool disposing)
48 if (disposing && m_stream != null)
56 void IDisposable.Dispose()
61 protected virtual void FillBuffer(int bytes) {
63 throw new IOException("Stream is invalid");
68 /* Cope with partial reads */
71 int n=m_stream.Read(m_buffer, pos, bytes-pos);
73 throw new EndOfStreamException();
80 public virtual int PeekChar() {
82 throw new IOException("Stream is invalid");
85 if(!m_stream.CanSeek) {
89 long pos=m_stream.Position;
91 m_stream.Position=pos;
96 public virtual int Read() {
97 char[] decode = new char[1];
99 int count=Read(decode, 0, 1);
101 /* No chars available */
108 public virtual int Read(byte[] buffer, int index, int count) {
110 throw new IOException("Stream is invalid");
113 if (buffer == null) {
114 throw new ArgumentNullException("buffer is null");
116 if (buffer.Length - index < count) {
117 throw new ArgumentException("buffer is too small");
120 throw new ArgumentOutOfRangeException("index is less than 0");
123 throw new ArgumentOutOfRangeException("count is less than 0");
126 int bytes_read=m_stream.Read(buffer, index, count);
131 public virtual int Read(char[] buffer, int index, int count) {
133 throw new IOException("Stream is invalid");
136 if (buffer == null) {
137 throw new ArgumentNullException("buffer is null");
139 if (buffer.Length - index < count) {
140 throw new ArgumentException("buffer is too small");
143 throw new ArgumentOutOfRangeException("index is less than 0");
146 throw new ArgumentOutOfRangeException("count is less than 0");
152 while(chars_read < count) {
153 CheckBuffer(bytes_read);
155 int read_byte=m_stream.ReadByte();
161 m_buffer[bytes_read]=(byte)read_byte;
164 chars_read=m_encoding.GetChars(m_buffer, 0,
173 protected int Read7BitEncodedInt() {
181 ret = ret | ((b & 0x7f) << shift);
183 } while ((b & 0x80) == 0x80);
188 public virtual bool ReadBoolean() {
192 // true if the byte is non-zero; otherwise false.
193 return(m_buffer[0] != 0);
196 public virtual byte ReadByte() {
202 public virtual byte[] ReadBytes(int count) {
204 throw new IOException("Stream is invalid");
208 throw new ArgumentOutOfRangeException("count is less than 0");
211 /* Can't use FillBuffer() here, because it's OK to
212 * return fewer bytes than were requested
215 byte[] buf = new byte[count];
219 int n=m_stream.Read(buf, pos, count-pos);
229 byte[] new_buffer=new byte[pos];
230 Array.Copy(buf, new_buffer, pos);
237 public virtual char ReadChar() {
241 throw new EndOfStreamException();
247 public virtual char[] ReadChars(int count) {
249 throw new ArgumentOutOfRangeException("count is less than 0");
252 char[] full = new char[count];
253 int chars = Read(full, 0, count);
256 throw new EndOfStreamException();
257 } else if (chars != full.Length) {
258 char[] ret = new char[chars];
259 Array.Copy(full, 0, ret, 0, chars);
266 unsafe public virtual decimal ReadDecimal() {
270 byte* ret_ptr = (byte *)&ret;
271 for (int i = 0; i < 16; i++) {
272 ret_ptr[i] = m_buffer[i];
278 public virtual double ReadDouble() {
281 return(BitConverter.ToDouble(m_buffer, 0));
284 public virtual short ReadInt16() {
287 return((short) (m_buffer[0] | (m_buffer[1] << 8)));
290 public virtual int ReadInt32() {
293 return(m_buffer[0] | (m_buffer[1] << 8) |
294 (m_buffer[2] << 16) | (m_buffer[3] << 24));
297 public virtual long ReadInt64() {
300 uint ret_low = (uint) (m_buffer[0] |
302 (m_buffer[2] << 16) |
305 uint ret_high = (uint) (m_buffer[4] |
307 (m_buffer[6] << 16) |
310 return (long) ((((ulong) ret_high) << 32) | ret_low);
313 [CLSCompliant(false)]
314 public virtual sbyte ReadSByte() {
317 return((sbyte)m_buffer[0]);
320 public virtual string ReadString() {
321 /* Inspection of BinaryWriter-written files
322 * shows that the length is given in bytes,
325 int len = Read7BitEncodedInt();
329 char[] str = m_encoding.GetChars(m_buffer, 0, len);
331 return(new String(str));
334 public virtual float ReadSingle() {
337 return(BitConverter.ToSingle(m_buffer, 0));
340 [CLSCompliant(false)]
341 public virtual ushort ReadUInt16() {
344 return((ushort) (m_buffer[0] | (m_buffer[1] << 8)));
347 [CLSCompliant(false)]
348 public virtual uint ReadUInt32() {
352 return((uint) (m_buffer[0] |
354 (m_buffer[2] << 16) |
355 (m_buffer[3] << 24)));
358 [CLSCompliant(false)]
359 public virtual ulong ReadUInt64() {
362 uint ret_low = (uint) (m_buffer[0] |
364 (m_buffer[2] << 16) |
367 uint ret_high = (uint) (m_buffer[4] |
369 (m_buffer[6] << 16) |
372 return (((ulong) ret_high) << 32) | ret_low;
375 /* Ensures that m_buffer is at least length bytes
376 * long, growing it if necessary
378 private void CheckBuffer(int length)
380 if(m_buffer.Length <= length) {
381 byte[] new_buffer=new byte[length];
382 Array.Copy(m_buffer, new_buffer,