// // 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; Decoder decoder; char [] charBuffer; // // 128 chars should cover most strings in one grab. // const int MaxBufferSize = 128; 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; decoder = encoding.GetDecoder (); 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; charBuffer = 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 MaxBufferSize) ? MaxBufferSize : len; FillBuffer (readLen); int cch = decoder.GetChars (m_buffer, 0, readLen, charBuffer, 0); if (sb == null && readLen == len) // ok, we got out the easy way, dont bother with the sb return new String (charBuffer, 0, cch); if (sb == null) // Len is a fairly good estimate of the number of chars in a string // Most of the time 1 byte == 1 char sb = new StringBuilder (len); sb.Append (charBuffer, 0, cch); len -= readLen; } while (len > 0); return sb.ToString(); } public virtual float ReadSingle() { FillBuffer(4); return(BitConverterLE.ToSingle(m_buffer, 0)); } [CLSCompliant(false)] public virtual ushort ReadUInt16() { FillBuffer(2); return((ushort) (m_buffer[0] | (m_buffer[1] << 8))); } [CLSCompliant(false)] public virtual uint ReadUInt32() { FillBuffer(4); return((uint) (m_buffer[0] | (m_buffer[1] << 8) | (m_buffer[2] << 16) | (m_buffer[3] << 24))); } [CLSCompliant(false)] public virtual ulong ReadUInt64() { FillBuffer(8); uint ret_low = (uint) (m_buffer[0] | (m_buffer[1] << 8) | (m_buffer[2] << 16) | (m_buffer[3] << 24) ); uint ret_high = (uint) (m_buffer[4] | (m_buffer[5] << 8) | (m_buffer[6] << 16) | (m_buffer[7] << 24) ); return (((ulong) ret_high) << 32) | ret_low; } /* Ensures that m_buffer is at least length bytes * long, growing it if necessary */ private void CheckBuffer(int length) { if(m_buffer.Length <= length) { byte[] new_buffer=new byte[length]; Buffer.BlockCopyInternal (m_buffer, 0, new_buffer, 0, m_buffer.Length); m_buffer=new_buffer; } } } }