2 // System.IO.BinaryReader
5 // Matt Kimball (matt@kimball.net)
6 // Dick Porter (dick@ximian.com)
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Globalization;
38 public class BinaryReader : IDisposable {
41 int m_encoding_max_byte;
45 private bool m_disposed = false;
47 public BinaryReader(Stream input) : this(input, Encoding.UTF8Unmarked) {
50 public BinaryReader(Stream input, Encoding encoding) {
51 if (input == null || encoding == null)
52 throw new ArgumentNullException(Locale.GetText ("Input or Encoding is a null reference."));
54 throw new ArgumentException(Locale.GetText ("The stream doesn't support reading."));
57 m_encoding = encoding;
58 m_encoding_max_byte = m_encoding.GetMaxByteCount(1);
59 m_buffer = new byte [32];
62 public virtual Stream BaseStream {
68 public virtual void Close() {
73 protected virtual void Dispose (bool disposing)
75 if (disposing && m_stream != null)
84 void IDisposable.Dispose()
89 protected virtual void FillBuffer (int bytes)
92 throw new ObjectDisposedException ("BinaryReader", "Cannot read from a closed BinaryReader.");
94 throw new IOException("Stream is invalid");
98 /* Cope with partial reads */
102 int n=m_stream.Read(m_buffer, pos, bytes-pos);
104 throw new EndOfStreamException();
111 public virtual int PeekChar() {
115 throw new ObjectDisposedException ("BinaryReader", "Cannot read from a closed BinaryReader.");
117 throw new IOException("Stream is invalid");
120 if ( !m_stream.CanSeek )
125 char[] result = new char[1];
129 int ccount = ReadCharBytes (result, 0, 1, out bytes, out bcount);
131 // Reposition the stream
132 m_stream.Position -= bcount;
134 // If we read 0 characters then return -1
140 // Return the single character we read
144 public virtual int Read() {
145 char[] decode = new char[1];
147 int count=Read(decode, 0, 1);
149 /* No chars available */
156 public virtual int Read(byte[] buffer, int index, int count) {
160 throw new ObjectDisposedException ("BinaryReader", "Cannot read from a closed BinaryReader.");
162 throw new IOException("Stream is invalid");
165 if (buffer == null) {
166 throw new ArgumentNullException("buffer is null");
169 throw new ArgumentOutOfRangeException("index is less than 0");
172 throw new ArgumentOutOfRangeException("count is less than 0");
174 if (buffer.Length - index < count) {
175 throw new ArgumentException("buffer is too small");
178 int bytes_read=m_stream.Read(buffer, index, count);
183 public virtual int Read(char[] buffer, int index, int count) {
188 throw new ObjectDisposedException ("BinaryReader", "Cannot read from a closed BinaryReader.");
190 throw new IOException("Stream is invalid");
193 if (buffer == null) {
194 throw new ArgumentNullException("buffer is null");
197 throw new ArgumentOutOfRangeException("index is less than 0");
200 throw new ArgumentOutOfRangeException("count is less than 0");
202 if (buffer.Length - index < count) {
203 throw new ArgumentException("buffer is too small");
208 return ReadCharBytes (buffer, index, count, out bytes, out bytes_read);
211 private int ReadCharBytes(char[] buffer, int index, int count, out byte[] bytes, out int bytes_read)
\r
216 while(chars_read < count)
218 CheckBuffer(bytes_read + 1);
220 int read_byte = m_stream.ReadByte();
229 m_buffer[bytes_read]=(byte)read_byte;
232 chars_read=m_encoding.GetChars(m_buffer, 0,
242 protected int Read7BitEncodedInt() {
250 ret = ret | ((b & 0x7f) << shift);
252 } while ((b & 0x80) == 0x80);
257 public virtual bool ReadBoolean() {
259 // true if the byte is non-zero; otherwise false.
260 return ReadByte() != 0;
263 public virtual byte ReadByte() {
264 if (m_stream == null) {
266 throw new ObjectDisposedException ("BinaryReader", "Cannot read from a closed BinaryReader.");
268 throw new IOException ("Stream is invalid");
271 int val = m_stream.ReadByte ();
275 throw new EndOfStreamException ();
278 public virtual byte[] ReadBytes(int count) {
282 throw new ObjectDisposedException ("BinaryReader", "Cannot read from a closed BinaryReader.");
284 throw new IOException("Stream is invalid");
288 throw new ArgumentOutOfRangeException("count is less than 0");
291 /* Can't use FillBuffer() here, because it's OK to
292 * return fewer bytes than were requested
295 byte[] buf = new byte[count];
298 while(pos < count)
\r
300 int n=m_stream.Read(buf, pos, count-pos);
310 byte[] new_buffer=new byte[pos];
311 Array.Copy(buf, new_buffer, pos);
318 public virtual char ReadChar() {
322 throw new EndOfStreamException();
328 public virtual char[] ReadChars(int count) {
330 throw new ArgumentOutOfRangeException("count is less than 0");
333 char[] full = new char[count];
334 int chars = Read(full, 0, count);
337 throw new EndOfStreamException();
338 } else if (chars != full.Length) {
339 char[] ret = new char[chars];
340 Array.Copy(full, 0, ret, 0, chars);
347 unsafe public virtual decimal ReadDecimal() {
351 byte* ret_ptr = (byte *)&ret;
354 * internal representation of decimal is
355 * ss32, hi32, lo32, mi32,
356 * but in stream it is
357 * lo32, mi32, hi32, ss32
358 * So we have to rerange this int32 values
361 if (BitConverter.IsLittleEndian) {
362 for (int i = 0; i < 16; i++) {
365 ret_ptr [i + 8] = m_buffer [i];
368 ret_ptr [i + 8] = m_buffer [i];
371 ret_ptr [i - 4] = m_buffer [i];
374 ret_ptr [i - 12] = m_buffer [i];
378 for (int i = 0; i < 16; i++) {
381 ret_ptr [11 - i] = m_buffer [i];
384 ret_ptr [19 - i] = m_buffer [i];
387 ret_ptr [15 - i] = m_buffer [i];
390 ret_ptr [15 - i] = m_buffer [i];
398 public virtual double ReadDouble() {
401 return(BitConverterLE.ToDouble(m_buffer, 0));
404 public virtual short ReadInt16() {
407 return((short) (m_buffer[0] | (m_buffer[1] << 8)));
410 public virtual int ReadInt32() {
413 return(m_buffer[0] | (m_buffer[1] << 8) |
414 (m_buffer[2] << 16) | (m_buffer[3] << 24));
417 public virtual long ReadInt64() {
420 uint ret_low = (uint) (m_buffer[0] |
422 (m_buffer[2] << 16) |
425 uint ret_high = (uint) (m_buffer[4] |
427 (m_buffer[6] << 16) |
430 return (long) ((((ulong) ret_high) << 32) | ret_low);
433 [CLSCompliant(false)]
434 public virtual sbyte ReadSByte() {
435 return (sbyte) ReadByte ();
438 public virtual string ReadString() {
439 /* Inspection of BinaryWriter-written files
440 * shows that the length is given in bytes,
443 int len = Read7BitEncodedInt();
447 char[] str = m_encoding.GetChars(m_buffer, 0, len);
449 return(new String(str));
452 public virtual float ReadSingle() {
455 return(BitConverterLE.ToSingle(m_buffer, 0));
458 [CLSCompliant(false)]
459 public virtual ushort ReadUInt16() {
462 return((ushort) (m_buffer[0] | (m_buffer[1] << 8)));
465 [CLSCompliant(false)]
466 public virtual uint ReadUInt32() {
470 return((uint) (m_buffer[0] |
472 (m_buffer[2] << 16) |
473 (m_buffer[3] << 24)));
476 [CLSCompliant(false)]
477 public virtual ulong ReadUInt64() {
480 uint ret_low = (uint) (m_buffer[0] |
482 (m_buffer[2] << 16) |
485 uint ret_high = (uint) (m_buffer[4] |
487 (m_buffer[6] << 16) |
490 return (((ulong) ret_high) << 32) | ret_low;
493 /* Ensures that m_buffer is at least length bytes
494 * long, growing it if necessary
496 private void CheckBuffer(int length)
498 if(m_buffer.Length <= length) {
499 byte[] new_buffer=new byte[length];
500 Array.Copy(m_buffer, new_buffer,