// // System.IO.BinaryReader // // Author: // Matt Kimball (matt@kimball.net) // Dick Porter (dick@ximian.com) // // // Copyright (C) 2004 Novell, Inc (http://www.novell.com) // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to // permit persons to whom the Software is furnished to do so, subject to // the following conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // using System; using System.Text; using System.Globalization; using Mono.Security; namespace System.IO { 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, Encoding encoding) { if (input == null || encoding == null) throw new ArgumentNullException(Locale.GetText ("Input or Encoding is a null reference.")); if (!input.CanRead) throw new ArgumentException(Locale.GetText ("The stream doesn't support reading.")); m_stream = input; m_encoding = encoding; m_encoding_max_byte = m_encoding.GetMaxByteCount(1); m_buffer = new byte [32]; } public virtual Stream BaseStream { get { return m_stream; } } public virtual void Close() { Dispose (true); m_disposed = true; } protected virtual void Dispose (bool disposing) { if (disposing && m_stream != null) m_stream.Close (); m_disposed = true; m_buffer = null; m_encoding = null; m_stream = null; } void IDisposable.Dispose() { Dispose (true); } protected virtual void FillBuffer (int bytes) { 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); /* Cope with partial reads */ int pos=0; while(pos