DataInput.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace WMFConverter.IO
  7. {
  8. /// <summary>
  9. /// Read input stream of wmf file.
  10. /// </summary>
  11. public class DataInput
  12. {
  13. #region Local Variables
  14. private System.IO.Stream _inputStream;
  15. private bool _isLittleEndian = BitConverter.IsLittleEndian;
  16. private byte[] _buf = new byte[4];
  17. private int _count = 0;
  18. #endregion
  19. #region Properties
  20. /// <summary>
  21. /// Represents the count of bytes read
  22. /// </summary>
  23. public int Count
  24. {
  25. get
  26. {
  27. return _count;
  28. }
  29. set
  30. {
  31. _count = value;
  32. }
  33. }
  34. #endregion
  35. #region Constructors
  36. /// <summary>
  37. /// Default constructor.
  38. /// Create a DataInput instance using native order.
  39. /// </summary>
  40. /// <param name="inputStream"></param>
  41. public DataInput(System.IO.Stream inputStream)
  42. {
  43. _isLittleEndian = BitConverter.IsLittleEndian;
  44. _inputStream = inputStream;
  45. }
  46. /// <summary>
  47. /// Default contructor.
  48. /// Create a DataInput instance receving the order.
  49. /// </summary>
  50. /// <param name="inputStream"></param>
  51. /// <param name="endian"></param>
  52. public DataInput(System.IO.Stream inputStream, bool endian)
  53. {
  54. _inputStream = inputStream;
  55. _isLittleEndian = endian;
  56. }
  57. #endregion
  58. #region Public Methods
  59. /// <summary>
  60. /// Reads the next one byte of this input stream as a signed 8-bit integer.
  61. /// </summary>
  62. /// <returns></returns>
  63. public int ReadByte()
  64. {
  65. if (_inputStream.Read(_buf, 0, 1) == 1)
  66. {
  67. _count += 1;
  68. return (0xff & _buf[0]);
  69. }
  70. throw new System.IO.EndOfStreamException();
  71. }
  72. /// <summary>
  73. /// Reads the next two bytes of this input stream as a signed 16-bit integer.
  74. /// </summary>
  75. /// <returns></returns>
  76. public int ReadInt16()
  77. {
  78. if (_inputStream.Read(_buf, 0, 2) == 2)
  79. {
  80. short value = 0;
  81. if (_isLittleEndian == false)
  82. {
  83. value |= (short)(0xff & _buf[1]);
  84. value |= (short)((0xff & _buf[0]) << 8);
  85. }
  86. else
  87. {
  88. value |= (short)(0xff & _buf[0]);
  89. value |= (short)((0xff & _buf[1]) << 8);
  90. }
  91. _count += 2;
  92. return value;
  93. }
  94. throw new System.IO.EndOfStreamException();
  95. }
  96. /// <summary>
  97. /// Reads the next four bytes of this input stream as a signed 32-bit integer.
  98. /// </summary>
  99. /// <returns></returns>
  100. public int ReadInt32()
  101. {
  102. if (_inputStream.Read(_buf, 0, 4) == 4)
  103. {
  104. int value = 0;
  105. if (_isLittleEndian == false)
  106. {
  107. value |= (0xff & _buf[3]);
  108. value |= (0xff & _buf[2]) << 8;
  109. value |= (0xff & _buf[1]) << 16;
  110. value |= (0xff & _buf[0]) << 24;
  111. }
  112. else
  113. {
  114. value |= (0xff & _buf[0]);
  115. value |= (0xff & _buf[1]) << 8;
  116. value |= (0xff & _buf[2]) << 16;
  117. value |= (0xff & _buf[3]) << 24;
  118. }
  119. _count += 4;
  120. return value;
  121. }
  122. throw new System.IO.EndOfStreamException();
  123. }
  124. /// <summary>
  125. /// Reads the next two bytes of this input stream as a unsigned 16-bit integer.
  126. /// </summary>
  127. /// <returns></returns>
  128. public int ReadUint16()
  129. {
  130. if (_inputStream.Read(_buf, 0, 2) == 2)
  131. {
  132. int value = 0;
  133. if (_isLittleEndian == false)
  134. {
  135. value |= (0xff & _buf[1]);
  136. value |= (0xff & _buf[0]) << 8;
  137. }
  138. else
  139. {
  140. value |= (0xff & _buf[0]);
  141. value |= (0xff & _buf[1]) << 8;
  142. }
  143. _count += 2;
  144. return value;
  145. }
  146. throw new System.IO.EndOfStreamException();
  147. }
  148. /// <summary>
  149. /// Reads the next four bytes of this input stream as a unsigned 32-bit integer.
  150. /// </summary>
  151. /// <returns></returns>
  152. public long ReadUint32()
  153. {
  154. if (_inputStream.Read(_buf, 0, 4) == 4)
  155. {
  156. long value = 0;
  157. if (_isLittleEndian == false)
  158. {
  159. value |= (0xff & _buf[3]);
  160. value |= (0xff & _buf[2]) << 8;
  161. value |= (0xff & _buf[1]) << 16;
  162. value |= (0xff & _buf[0]) << 24;
  163. }
  164. else
  165. {
  166. value |= (0xff & _buf[0]);
  167. value |= (0xff & _buf[1]) << 8;
  168. value |= (0xff & _buf[2]) << 16;
  169. value |= (0xff & _buf[3]) << 24;
  170. }
  171. _count += 4;
  172. return value;
  173. }
  174. throw new System.IO.EndOfStreamException();
  175. }
  176. /// <summary>
  177. /// Reads the next n bytes.
  178. /// </summary>
  179. /// <param name="n"></param>
  180. /// <returns></returns>
  181. public byte[] ReadBytes(int n)
  182. {
  183. byte[] array = new byte[n];
  184. if (_inputStream.Read(array, 0, array.Length) == n)
  185. {
  186. _count += n;
  187. return array;
  188. }
  189. throw new System.IO.EndOfStreamException();
  190. }
  191. /// <summary>
  192. /// Close the input stream.
  193. /// </summary>
  194. public void Close()
  195. {
  196. try
  197. {
  198. _inputStream.Close();
  199. }
  200. catch (System.IO.IOException ex)
  201. {
  202. Console.Write(ex.Message);
  203. }
  204. }
  205. #endregion
  206. }
  207. }