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;
24 private bool m_disposed = false;
26 public BinaryReader(Stream input) : this(input, Encoding.UTF8Unmarked) {
29 public BinaryReader(Stream input, Encoding encoding) {
30 if (input == null || encoding == null)
31 throw new ArgumentNullException(Locale.GetText ("Input or Encoding is a null reference."));
33 throw new ArgumentException(Locale.GetText ("The stream doesn't support reading."));
36 m_encoding = encoding;
37 m_encoding_max_byte = m_encoding.GetMaxByteCount(1);
38 m_buffer = new byte [32];
41 public virtual Stream BaseStream {
47 public virtual void Close() {
52 protected virtual void Dispose (bool disposing)
54 if (disposing && m_stream != null)
63 void IDisposable.Dispose()
68 protected virtual void FillBuffer(int bytes) {
70 throw new IOException("Stream is invalid");
75 /* Cope with partial reads */
78 while (m_peekIndex < m_peekLimit)
79 m_buffer[pos++] = m_peekBuffer[m_peekIndex++];
82 int n=m_stream.Read(m_buffer, pos, bytes-pos);
84 throw new EndOfStreamException();
91 public virtual int PeekChar() {
95 throw new ObjectDisposedException ("BinaryReader", "Cannot read from a closed BinaryReader.");
97 throw new IOException("Stream is invalid");
100 char[] result = new char[1];
104 int ccount = ReadCharBytes (result, 0, 1, out bytes, out bcount);
106 if (m_peekBuffer == null || m_peekBuffer.Length < bcount)
107 m_peekBuffer = new byte[bcount];
109 Array.Copy (bytes, m_peekBuffer, bcount);
111 m_peekLimit = bcount;
113 if (ccount == 0) return -1;
114 else return result[0];
117 public virtual int Read() {
118 char[] decode = new char[1];
120 int count=Read(decode, 0, 1);
122 /* No chars available */
129 public virtual int Read(byte[] buffer, int index, int count) {
133 throw new ObjectDisposedException ("BinaryReader", "Cannot read from a closed BinaryReader.");
135 throw new IOException("Stream is invalid");
138 if (buffer == null) {
139 throw new ArgumentNullException("buffer is null");
141 if (buffer.Length - index < count) {
142 throw new ArgumentException("buffer is too small");
145 throw new ArgumentOutOfRangeException("index is less than 0");
148 throw new ArgumentOutOfRangeException("count is less than 0");
152 while (m_peekIndex < m_peekLimit) {
153 buffer[index++] = m_peekBuffer[m_peekIndex++];
158 int bytes_read=m_stream.Read(buffer, index, count);
160 return(bytes_read + fromPeek);
163 public virtual int Read(char[] buffer, int index, int count) {
168 throw new ObjectDisposedException ("BinaryReader", "Cannot read from a closed BinaryReader.");
170 throw new IOException("Stream is invalid");
173 if (buffer == null) {
174 throw new ArgumentNullException("buffer is null");
176 if (buffer.Length - index < count) {
177 throw new ArgumentException("buffer is too small");
180 throw new ArgumentOutOfRangeException("index is less than 0");
183 throw new ArgumentOutOfRangeException("count is less than 0");
188 return ReadCharBytes (buffer, index, count, out bytes, out bytes_read);
191 private int ReadCharBytes(char[] buffer, int index, int count, out byte[] bytes, out int bytes_read)
\r
196 while(chars_read < count) {
197 CheckBuffer(bytes_read + 1);
199 int read_byte = (m_peekIndex < m_peekLimit) ? m_peekBuffer[m_peekIndex++] : m_stream.ReadByte();
206 m_buffer[bytes_read]=(byte)read_byte;
209 chars_read=m_encoding.GetChars(m_buffer, 0,
219 protected int Read7BitEncodedInt() {
227 ret = ret | ((b & 0x7f) << shift);
229 } while ((b & 0x80) == 0x80);
234 public virtual bool ReadBoolean() {
238 // true if the byte is non-zero; otherwise false.
239 return(m_buffer[0] != 0);
242 public virtual byte ReadByte() {
248 public virtual byte[] ReadBytes(int count) {
252 throw new ObjectDisposedException ("BinaryReader", "Cannot read from a closed BinaryReader.");
254 throw new IOException("Stream is invalid");
258 throw new ArgumentOutOfRangeException("count is less than 0");
261 /* Can't use FillBuffer() here, because it's OK to
262 * return fewer bytes than were requested
265 byte[] buf = new byte[count];
268 while (m_peekIndex < m_peekLimit)
269 buf[pos++] = m_peekBuffer[m_peekIndex++];
271 while(pos < count)
\r
273 int n=m_stream.Read(buf, pos, count-pos);
283 byte[] new_buffer=new byte[pos];
284 Array.Copy(buf, new_buffer, pos);
291 public virtual char ReadChar() {
295 throw new EndOfStreamException();
301 public virtual char[] ReadChars(int count) {
303 throw new ArgumentOutOfRangeException("count is less than 0");
306 char[] full = new char[count];
307 int chars = Read(full, 0, count);
310 throw new EndOfStreamException();
311 } else if (chars != full.Length) {
312 char[] ret = new char[chars];
313 Array.Copy(full, 0, ret, 0, chars);
320 unsafe public virtual decimal ReadDecimal() {
324 byte* ret_ptr = (byte *)&ret;
325 for (int i = 0; i < 16; i++) {
328 * internal representation of decimal is
329 * ss32, hi32, lo32, mi32,
330 * but in stream it is
331 * lo32, mi32, hi32, ss32
332 * So we have to rerange this int32 values
337 ret_ptr [i + 8] = m_buffer [i];
340 ret_ptr [i + 8] = m_buffer [i];
343 ret_ptr [i - 4] = m_buffer [i];
346 ret_ptr [i - 12] = m_buffer [i];
353 public virtual double ReadDouble() {
356 return(BitConverter.ToDouble(m_buffer, 0));
359 public virtual short ReadInt16() {
362 return((short) (m_buffer[0] | (m_buffer[1] << 8)));
365 public virtual int ReadInt32() {
368 return(m_buffer[0] | (m_buffer[1] << 8) |
369 (m_buffer[2] << 16) | (m_buffer[3] << 24));
372 public virtual long ReadInt64() {
375 uint ret_low = (uint) (m_buffer[0] |
377 (m_buffer[2] << 16) |
380 uint ret_high = (uint) (m_buffer[4] |
382 (m_buffer[6] << 16) |
385 return (long) ((((ulong) ret_high) << 32) | ret_low);
388 [CLSCompliant(false)]
389 public virtual sbyte ReadSByte() {
392 return((sbyte)m_buffer[0]);
395 public virtual string ReadString() {
396 /* Inspection of BinaryWriter-written files
397 * shows that the length is given in bytes,
400 int len = Read7BitEncodedInt();
404 char[] str = m_encoding.GetChars(m_buffer, 0, len);
406 return(new String(str));
409 public virtual float ReadSingle() {
412 return(BitConverter.ToSingle(m_buffer, 0));
415 [CLSCompliant(false)]
416 public virtual ushort ReadUInt16() {
419 return((ushort) (m_buffer[0] | (m_buffer[1] << 8)));
422 [CLSCompliant(false)]
423 public virtual uint ReadUInt32() {
427 return((uint) (m_buffer[0] |
429 (m_buffer[2] << 16) |
430 (m_buffer[3] << 24)));
433 [CLSCompliant(false)]
434 public virtual ulong ReadUInt64() {
437 uint ret_low = (uint) (m_buffer[0] |
439 (m_buffer[2] << 16) |
442 uint ret_high = (uint) (m_buffer[4] |
444 (m_buffer[6] << 16) |
447 return (((ulong) ret_high) << 32) | ret_low;
450 /* Ensures that m_buffer is at least length bytes
451 * long, growing it if necessary
453 private void CheckBuffer(int length)
455 if(m_buffer.Length <= length) {
456 byte[] new_buffer=new byte[length];
457 Array.Copy(m_buffer, new_buffer,