2009-10-21 Miguel de Icaza <miguel@novell.com>
[mono.git] / mcs / class / corlib / System.IO / BinaryReader.cs
index f6c481e9bbc780cdf817f7bc341ea41abc6b3207..d6dd9c96afdfd21725721e24589e7f6cec459b45 100644 (file)
@@ -33,12 +33,13 @@ using System;
 using System.Text;
 using System.Globalization;
 using Mono.Security;
+using System.Runtime.InteropServices;
 
 namespace System.IO {
+       [ComVisible (true)]
        public class BinaryReader : IDisposable {
                Stream m_stream;
                Encoding m_encoding;
-               int m_encoding_max_byte;
 
                byte[] m_buffer;
 
@@ -53,7 +54,7 @@ namespace System.IO {
                
                private bool m_disposed = false;
 
-               public BinaryReader(Stream input) : this(input, Encoding.UTF8Unmarked) {
+               public BinaryReader(Stream input) : this(input, Encoding.UTF8UnmarkedUnsafe) {
                }
 
                public BinaryReader(Stream input, Encoding encoding) {
@@ -91,25 +92,29 @@ namespace System.IO {
                        charBuffer = null;
                }
 
+#if NET_4_0
+               public void Dispose ()
+#else
                void IDisposable.Dispose() 
+#endif
                {
                        Dispose (true);
                }
 
-               protected virtual void FillBuffer (int bytes)
+               protected virtual void FillBuffer (int numBytes)
                {
                        if (m_disposed)
                                throw new ObjectDisposedException ("BinaryReader", "Cannot read from a closed BinaryReader.");
                        if (m_stream==null)
                                throw new IOException("Stream is invalid");
                        
-                       CheckBuffer(bytes);
+                       CheckBuffer(numBytes);
 
                        /* Cope with partial reads */
                        int pos=0;
 
-                       while(pos<bytes) {
-                               int n=m_stream.Read(m_buffer, pos, bytes-pos);
+                       while(pos<numBytes) {
+                               int n=m_stream.Read(m_buffer, pos, numBytes-pos);
                                if(n==0) {
                                        throw new EndOfStreamException();
                                }
@@ -133,10 +138,9 @@ namespace System.IO {
                        }
 
                        char[] result = new char[1];
-                       byte[] bytes;
                        int bcount;
 
-                       int ccount = ReadCharBytes (result, 0, 1, out bytes, out bcount);
+                       int ccount = ReadCharBytes (result, 0, 1, out bcount);
 
                        // Reposition the stream
                        m_stream.Position -= bcount;
@@ -215,54 +219,57 @@ namespace System.IO {
                        }
 
                        int bytes_read;
-                       byte[] bytes;
-                       return ReadCharBytes (buffer, index, count, out bytes, out bytes_read);
+                       return ReadCharBytes (buffer, index, count, out bytes_read);
                }
 
-               private int ReadCharBytes(char[] buffer, int index, int count, out byte[] bytes, out int bytes_read) \r
+               private int ReadCharBytes (char[] buffer, int index, int count, out int bytes_read) 
                {
-                       int chars_read=0;
-                       bytes_read=0;
+                       int chars_read = 0;
+                       bytes_read = 0;
                        
-                       while(chars_read < count) 
-                       {
-                               CheckBuffer(bytes_read + 1);
+                       while (chars_read < count) {
+                               int pos = 0;
+                               while (true) {
+                                       CheckBuffer (pos + 1);
 
-                               int read_byte = m_stream.ReadByte();
+                                       int read_byte = m_stream.ReadByte ();
 
-                               if(read_byte==-1) 
-                               {
-                                       /* EOF */
-                                       bytes = m_buffer;
-                                       return(chars_read);
-                               }
+                                       if (read_byte == -1)
+                                               /* EOF */
+                                               return chars_read;
 
-                               m_buffer[bytes_read]=(byte)read_byte;
-                               bytes_read++;
+                                       m_buffer [pos ++] = (byte)read_byte;
+                                       bytes_read ++;
 
-                               chars_read=m_encoding.GetChars(m_buffer, 0,
-                                                                                bytes_read,
-                                                                                buffer, index);
-                               
+                                       int n = m_encoding.GetChars (m_buffer, 0, pos, buffer, index + chars_read);
+                                       if (n > 0)
+                                               break;
+                               }
+                               chars_read ++;
                        }
 
-                       bytes = m_buffer;
-                       return(chars_read);
+                       return chars_read;
                }
 
                protected int Read7BitEncodedInt() {
                        int ret = 0;
                        int shift = 0;
+                       int len;
                        byte b;
 
-                       do {
+                       for (len = 0; len < 5; ++len) {
                                b = ReadByte();
                                
                                ret = ret | ((b & 0x7f) << shift);
                                shift += 7;
-                       } while ((b & 0x80) == 0x80);
+                               if ((b & 0x80) == 0)
+                                       break;
+                       }
 
-                       return ret;
+                       if (len < 5)
+                               return ret;
+                       else
+                               throw new FormatException ("Too many bytes in what should have been a 7 bit encoded Int32.");
                }
 
                public virtual bool ReadBoolean() {
@@ -306,7 +313,7 @@ namespace System.IO {
                        byte[] buf = new byte[count];
                        int pos=0;
                        
-                       while(pos < count) \r
+                       while(pos < count) 
                        {
                                int n=m_stream.Read(buf, pos, count-pos);
                                if(n==0) {