BinaryReader.cs 9.2 KB

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