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;
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) {
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();
}
}
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;
}
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() {
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) {