BinaryReader.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. //
  2. // System.IO.BinaryReader
  3. //
  4. // Author:
  5. // Matt Kimball ([email protected])
  6. // Dick Porter ([email protected])
  7. //
  8. using System;
  9. using System.Text;
  10. using System.Globalization;
  11. namespace System.IO {
  12. public class BinaryReader : IDisposable {
  13. Stream m_stream;
  14. Encoding m_encoding;
  15. int m_encoding_max_byte;
  16. byte[] m_buffer;
  17. public BinaryReader(Stream input) : this(input, Encoding.UTF8Unmarked) {
  18. }
  19. public BinaryReader(Stream input, Encoding encoding) {
  20. if (input == null || encoding == null)
  21. throw new ArgumentNullException(Locale.GetText ("Input or Encoding is a null reference."));
  22. if (!input.CanRead)
  23. throw new ArgumentException(Locale.GetText ("The stream doesn't support reading."));
  24. m_stream = input;
  25. m_encoding = encoding;
  26. m_encoding_max_byte = m_encoding.GetMaxByteCount(1);
  27. m_buffer = new byte [32];
  28. }
  29. public virtual Stream BaseStream {
  30. get {
  31. return m_stream;
  32. }
  33. }
  34. public virtual void Close() {
  35. Dispose (true);
  36. }
  37. protected virtual void Dispose (bool disposing)
  38. {
  39. if (disposing && m_stream != null)
  40. m_stream.Close ();
  41. m_buffer = null;
  42. m_encoding = null;
  43. m_stream = null;
  44. }
  45. void IDisposable.Dispose()
  46. {
  47. Dispose (true);
  48. }
  49. protected virtual void FillBuffer(int bytes) {
  50. if(m_stream==null) {
  51. throw new IOException("Stream is invalid");
  52. }
  53. CheckBuffer(bytes);
  54. /* Cope with partial reads */
  55. int pos=0;
  56. while(pos<bytes) {
  57. int n=m_stream.Read(m_buffer, pos, bytes-pos);
  58. if(n==0) {
  59. throw new EndOfStreamException();
  60. }
  61. pos+=n;
  62. }
  63. }
  64. public virtual int PeekChar() {
  65. if(m_stream==null) {
  66. throw new IOException("Stream is invalid");
  67. }
  68. if(!m_stream.CanSeek) {
  69. return(-1);
  70. }
  71. long pos=m_stream.Position;
  72. int ch=Read();
  73. m_stream.Position=pos;
  74. return(ch);
  75. }
  76. public virtual int Read() {
  77. char[] decode = new char[1];
  78. int count=Read(decode, 0, 1);
  79. if(count==0) {
  80. /* No chars available */
  81. return(-1);
  82. }
  83. return decode[0];
  84. }
  85. public virtual int Read(byte[] buffer, int index, int count) {
  86. if(m_stream==null) {
  87. throw new IOException("Stream is invalid");
  88. }
  89. if (buffer == null) {
  90. throw new ArgumentNullException("buffer is null");
  91. }
  92. if (buffer.Length - index < count) {
  93. throw new ArgumentException("buffer is too small");
  94. }
  95. if (index < 0) {
  96. throw new ArgumentOutOfRangeException("index is less than 0");
  97. }
  98. if (count < 0) {
  99. throw new ArgumentOutOfRangeException("count is less than 0");
  100. }
  101. int bytes_read=m_stream.Read(buffer, index, count);
  102. return(bytes_read);
  103. }
  104. public virtual int Read(char[] buffer, int index, int count) {
  105. if(m_stream==null) {
  106. throw new IOException("Stream is invalid");
  107. }
  108. if (buffer == null) {
  109. throw new ArgumentNullException("buffer is null");
  110. }
  111. if (buffer.Length - index < count) {
  112. throw new ArgumentException("buffer is too small");
  113. }
  114. if (index < 0) {
  115. throw new ArgumentOutOfRangeException("index is less than 0");
  116. }
  117. if (count < 0) {
  118. throw new ArgumentOutOfRangeException("count is less than 0");
  119. }
  120. int chars_read=0;
  121. int bytes_read=0;
  122. while(chars_read < count) {
  123. CheckBuffer(bytes_read);
  124. int read_byte=m_stream.ReadByte();
  125. if(read_byte==-1) {
  126. /* EOF */
  127. return(chars_read);
  128. }
  129. m_buffer[bytes_read]=(byte)read_byte;
  130. bytes_read++;
  131. chars_read=m_encoding.GetChars(m_buffer, 0,
  132. bytes_read,
  133. buffer, index);
  134. }
  135. return(chars_read);
  136. }
  137. protected int Read7BitEncodedInt() {
  138. int ret = 0;
  139. int shift = 0;
  140. byte b;
  141. do {
  142. b = ReadByte();
  143. ret = ret | ((b & 0x7f) << shift);
  144. shift += 7;
  145. } while ((b & 0x80) == 0x80);
  146. return ret;
  147. }
  148. public virtual bool ReadBoolean() {
  149. FillBuffer(1);
  150. // Return value:
  151. // true if the byte is non-zero; otherwise false.
  152. return(m_buffer[0] != 0);
  153. }
  154. public virtual byte ReadByte() {
  155. FillBuffer(1);
  156. return(m_buffer[0]);
  157. }
  158. public virtual byte[] ReadBytes(int count) {
  159. if(m_stream==null) {
  160. throw new IOException("Stream is invalid");
  161. }
  162. if (count < 0) {
  163. throw new ArgumentOutOfRangeException("count is less than 0");
  164. }
  165. /* Can't use FillBuffer() here, because it's OK to
  166. * return fewer bytes than were requested
  167. */
  168. byte[] buf = new byte[count];
  169. int pos=0;
  170. while(pos < count) {
  171. int n=m_stream.Read(buf, pos, count-pos);
  172. if(n==0) {
  173. /* EOF */
  174. break;
  175. }
  176. pos+=n;
  177. }
  178. if (pos!=count) {
  179. byte[] new_buffer=new byte[pos];
  180. Array.Copy(buf, new_buffer, pos);
  181. return(new_buffer);
  182. }
  183. return(buf);
  184. }
  185. public virtual char ReadChar() {
  186. int ch=Read();
  187. if(ch==-1) {
  188. throw new EndOfStreamException();
  189. }
  190. return((char)ch);
  191. }
  192. public virtual char[] ReadChars(int count) {
  193. if (count < 0) {
  194. throw new ArgumentOutOfRangeException("count is less than 0");
  195. }
  196. char[] full = new char[count];
  197. int chars = Read(full, 0, count);
  198. if (chars == 0) {
  199. throw new EndOfStreamException();
  200. } else if (chars != full.Length) {
  201. char[] ret = new char[chars];
  202. Array.Copy(full, 0, ret, 0, chars);
  203. return ret;
  204. } else {
  205. return full;
  206. }
  207. }
  208. unsafe public virtual decimal ReadDecimal() {
  209. FillBuffer(16);
  210. decimal ret;
  211. byte* ret_ptr = (byte *)&ret;
  212. for (int i = 0; i < 16; i++) {
  213. ret_ptr[i] = m_buffer[i];
  214. }
  215. return ret;
  216. }
  217. public virtual double ReadDouble() {
  218. FillBuffer(8);
  219. return(BitConverter.ToDouble(m_buffer, 0));
  220. }
  221. public virtual short ReadInt16() {
  222. FillBuffer(2);
  223. return((short) (m_buffer[0] | (m_buffer[1] << 8)));
  224. }
  225. public virtual int ReadInt32() {
  226. FillBuffer(4);
  227. return(m_buffer[0] | (m_buffer[1] << 8) |
  228. (m_buffer[2] << 16) | (m_buffer[3] << 24));
  229. }
  230. public virtual long ReadInt64() {
  231. FillBuffer(8);
  232. uint ret_low = (uint) (m_buffer[0] |
  233. (m_buffer[1] << 8) |
  234. (m_buffer[2] << 16) |
  235. (m_buffer[3] << 24)
  236. );
  237. uint ret_high = (uint) (m_buffer[4] |
  238. (m_buffer[5] << 8) |
  239. (m_buffer[6] << 16) |
  240. (m_buffer[7] << 24)
  241. );
  242. return (long) ((((ulong) ret_high) << 32) | ret_low);
  243. }
  244. [CLSCompliant(false)]
  245. public virtual sbyte ReadSByte() {
  246. FillBuffer(1);
  247. return((sbyte)m_buffer[0]);
  248. }
  249. public virtual string ReadString() {
  250. /* Inspection of BinaryWriter-written files
  251. * shows that the length is given in bytes,
  252. * not chars
  253. */
  254. int len = Read7BitEncodedInt();
  255. FillBuffer(len);
  256. char[] str = m_encoding.GetChars(m_buffer, 0, len);
  257. return(new String(str));
  258. }
  259. public virtual float ReadSingle() {
  260. FillBuffer(4);
  261. return(BitConverter.ToSingle(m_buffer, 0));
  262. }
  263. [CLSCompliant(false)]
  264. public virtual ushort ReadUInt16() {
  265. FillBuffer(2);
  266. return((ushort) (m_buffer[0] | (m_buffer[1] << 8)));
  267. }
  268. [CLSCompliant(false)]
  269. public virtual uint ReadUInt32() {
  270. FillBuffer(4);
  271. return((uint) (m_buffer[0] |
  272. (m_buffer[1] << 8) |
  273. (m_buffer[2] << 16) |
  274. (m_buffer[3] << 24)));
  275. }
  276. [CLSCompliant(false)]
  277. public virtual ulong ReadUInt64() {
  278. FillBuffer(8);
  279. uint ret_low = (uint) (m_buffer[0] |
  280. (m_buffer[1] << 8) |
  281. (m_buffer[2] << 16) |
  282. (m_buffer[3] << 24)
  283. );
  284. uint ret_high = (uint) (m_buffer[4] |
  285. (m_buffer[5] << 8) |
  286. (m_buffer[6] << 16) |
  287. (m_buffer[7] << 24)
  288. );
  289. return (((ulong) ret_high) << 32) | ret_low;
  290. }
  291. /* Ensures that m_buffer is at least length bytes
  292. * long, growing it if necessary
  293. */
  294. private void CheckBuffer(int length)
  295. {
  296. if(m_buffer.Length <= length) {
  297. byte[] new_buffer=new byte[length];
  298. Array.Copy(m_buffer, new_buffer,
  299. m_buffer.Length);
  300. m_buffer=new_buffer;
  301. }
  302. }
  303. }
  304. }