You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
7.9 KiB

  1. using System;
  2. /* Contains conversion support elements such as classes, interfaces and static methods. */
  3. namespace zlib
  4. {
  5. class SupportClass
  6. {
  7. /// <summary>
  8. /// This method returns the literal value received
  9. /// </summary>
  10. /// <param name="literal">The literal to return</param>
  11. /// <returns>The received value</returns>
  12. public static long Identity(long literal)
  13. {
  14. return literal;
  15. }
  16. /// <summary>
  17. /// This method returns the literal value received
  18. /// </summary>
  19. /// <param name="literal">The literal to return</param>
  20. /// <returns>The received value</returns>
  21. public static ulong Identity(ulong literal)
  22. {
  23. return literal;
  24. }
  25. /// <summary>
  26. /// This method returns the literal value received
  27. /// </summary>
  28. /// <param name="literal">The literal to return</param>
  29. /// <returns>The received value</returns>
  30. public static float Identity(float literal)
  31. {
  32. return literal;
  33. }
  34. /// <summary>
  35. /// This method returns the literal value received
  36. /// </summary>
  37. /// <param name="literal">The literal to return</param>
  38. /// <returns>The received value</returns>
  39. public static double Identity(double literal)
  40. {
  41. return literal;
  42. }
  43. /*******************************/
  44. /// <summary>
  45. /// Performs an unsigned bitwise right shift with the specified number
  46. /// </summary>
  47. /// <param name="number">Number to operate on</param>
  48. /// <param name="bits">Ammount of bits to shift</param>
  49. /// <returns>The resulting number from the shift operation</returns>
  50. public static int URShift(int number, int bits)
  51. {
  52. if ( number >= 0)
  53. return number >> bits;
  54. else
  55. return (number >> bits) + (2 << ~bits);
  56. }
  57. /// <summary>
  58. /// Performs an unsigned bitwise right shift with the specified number
  59. /// </summary>
  60. /// <param name="number">Number to operate on</param>
  61. /// <param name="bits">Ammount of bits to shift</param>
  62. /// <returns>The resulting number from the shift operation</returns>
  63. public static int URShift(int number, long bits)
  64. {
  65. return URShift(number, (int)bits);
  66. }
  67. /// <summary>
  68. /// Performs an unsigned bitwise right shift with the specified number
  69. /// </summary>
  70. /// <param name="number">Number to operate on</param>
  71. /// <param name="bits">Ammount of bits to shift</param>
  72. /// <returns>The resulting number from the shift operation</returns>
  73. public static long URShift(long number, int bits)
  74. {
  75. if ( number >= 0)
  76. return number >> bits;
  77. else
  78. return (number >> bits) + (2L << ~bits);
  79. }
  80. /// <summary>
  81. /// Performs an unsigned bitwise right shift with the specified number
  82. /// </summary>
  83. /// <param name="number">Number to operate on</param>
  84. /// <param name="bits">Ammount of bits to shift</param>
  85. /// <returns>The resulting number from the shift operation</returns>
  86. public static long URShift(long number, long bits)
  87. {
  88. return URShift(number, (int)bits);
  89. }
  90. /*******************************/
  91. /// <summary>Reads a number of characters from the current source Stream and writes the data to the target array at the specified index.</summary>
  92. /// <param name="sourceStream">The source Stream to read from.</param>
  93. /// <param name="target">Contains the array of characteres read from the source Stream.</param>
  94. /// <param name="start">The starting index of the target array.</param>
  95. /// <param name="count">The maximum number of characters to read from the source Stream.</param>
  96. /// <returns>The number of characters read. The number will be less than or equal to count depending on the data available in the source Stream. Returns -1 if the end of the stream is reached.</returns>
  97. public static System.Int32 ReadInput(System.IO.Stream sourceStream, byte[] target, int start, int count)
  98. {
  99. // Returns 0 bytes if not enough space in target
  100. if (target.Length == 0)
  101. return 0;
  102. byte[] receiver = new byte[target.Length];
  103. int bytesRead = sourceStream.Read(receiver, start, count);
  104. // Returns -1 if EOF
  105. if (bytesRead == 0)
  106. return -1;
  107. for(int i = start; i < start + bytesRead; i++)
  108. target[i] = (byte)receiver[i];
  109. return bytesRead;
  110. }
  111. /// <summary>Reads a number of characters from the current source TextReader and writes the data to the target array at the specified index.</summary>
  112. /// <param name="sourceTextReader">The source TextReader to read from</param>
  113. /// <param name="target">Contains the array of characteres read from the source TextReader.</param>
  114. /// <param name="start">The starting index of the target array.</param>
  115. /// <param name="count">The maximum number of characters to read from the source TextReader.</param>
  116. /// <returns>The number of characters read. The number will be less than or equal to count depending on the data available in the source TextReader. Returns -1 if the end of the stream is reached.</returns>
  117. public static System.Int32 ReadInput(System.IO.TextReader sourceTextReader, byte[] target, int start, int count)
  118. {
  119. // Returns 0 bytes if not enough space in target
  120. if (target.Length == 0) return 0;
  121. char[] charArray = new char[target.Length];
  122. int bytesRead = sourceTextReader.Read(charArray, start, count);
  123. // Returns -1 if EOF
  124. if (bytesRead == 0) return -1;
  125. for(int index=start; index<start+bytesRead; index++)
  126. target[index] = (byte)charArray[index];
  127. return bytesRead;
  128. }
  129. /// <summary>
  130. /// Converts a string to an array of bytes
  131. /// </summary>
  132. /// <param name="sourceString">The string to be converted</param>
  133. /// <returns>The new array of bytes</returns>
  134. public static byte[] ToByteArray(System.String sourceString)
  135. {
  136. return System.Text.UTF8Encoding.UTF8.GetBytes(sourceString);
  137. }
  138. /// <summary>
  139. /// Converts an array of bytes to an array of chars
  140. /// </summary>
  141. /// <param name="byteArray">The array of bytes to convert</param>
  142. /// <returns>The new array of chars</returns>
  143. public static char[] ToCharArray(byte[] byteArray)
  144. {
  145. return System.Text.UTF8Encoding.UTF8.GetChars(byteArray);
  146. }
  147. /*******************************/
  148. /// <summary>
  149. /// Writes an object to the specified Stream
  150. /// </summary>
  151. /// <param name="stream">The target Stream</param>
  152. /// <param name="objectToSend">The object to be sent</param>
  153. public static void Serialize(System.IO.Stream stream, System.Object objectToSend)
  154. {
  155. System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  156. formatter.Serialize(stream, objectToSend);
  157. }
  158. /// <summary>
  159. /// Writes an object to the specified BinaryWriter
  160. /// </summary>
  161. /// <param name="binaryWriter">The target BinaryWriter</param>
  162. /// <param name="objectToSend">The object to be sent</param>
  163. public static void Serialize(System.IO.BinaryWriter binaryWriter, System.Object objectToSend)
  164. {
  165. System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  166. formatter.Serialize(binaryWriter.BaseStream, objectToSend);
  167. }
  168. /*******************************/
  169. /// <summary>
  170. /// Deserializes an object, or an entire graph of connected objects, and returns the object intance
  171. /// </summary>
  172. /// <param name="binaryReader">Reader instance used to read the object</param>
  173. /// <returns>The object instance</returns>
  174. public static System.Object Deserialize(System.IO.BinaryReader binaryReader)
  175. {
  176. System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  177. return formatter.Deserialize(binaryReader.BaseStream);
  178. }
  179. /*******************************/
  180. /// <summary>
  181. /// Writes the exception stack trace to the received stream
  182. /// </summary>
  183. /// <param name="throwable">Exception to obtain information from</param>
  184. /// <param name="stream">Output sream used to write to</param>
  185. public static void WriteStackTrace(System.Exception throwable, System.IO.TextWriter stream)
  186. {
  187. stream.Write(throwable.StackTrace);
  188. stream.Flush();
  189. }
  190. }
  191. }