PendingBuffer.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System;
  2. namespace ICSharpCode.SharpZipLib.Zip.Compression
  3. {
  4. /// <summary>
  5. /// This class is general purpose class for writing data to a buffer.
  6. ///
  7. /// It allows you to write bits as well as bytes
  8. /// Based on DeflaterPending.java
  9. ///
  10. /// author of the original java version : Jochen Hoenicke
  11. /// </summary>
  12. public class PendingBuffer
  13. {
  14. #region Instance Fields
  15. /// <summary>
  16. /// Internal work buffer
  17. /// </summary>
  18. readonly byte[] buffer;
  19. int start;
  20. int end;
  21. uint bits;
  22. int bitCount;
  23. #endregion
  24. #region Constructors
  25. /// <summary>
  26. /// construct instance using default buffer size of 4096
  27. /// </summary>
  28. public PendingBuffer() : this(4096)
  29. {
  30. }
  31. /// <summary>
  32. /// construct instance using specified buffer size
  33. /// </summary>
  34. /// <param name="bufferSize">
  35. /// size to use for internal buffer
  36. /// </param>
  37. public PendingBuffer(int bufferSize)
  38. {
  39. buffer = new byte[bufferSize];
  40. }
  41. #endregion
  42. /// <summary>
  43. /// Clear internal state/buffers
  44. /// </summary>
  45. public void Reset()
  46. {
  47. start = end = bitCount = 0;
  48. }
  49. /// <summary>
  50. /// Write a byte to buffer
  51. /// </summary>
  52. /// <param name="value">
  53. /// The value to write
  54. /// </param>
  55. public void WriteByte(int value)
  56. {
  57. #if DebugDeflation
  58. if (DeflaterConstants.DEBUGGING && (start != 0) )
  59. {
  60. throw new SharpZipBaseException("Debug check: start != 0");
  61. }
  62. #endif
  63. buffer[end++] = unchecked((byte)value);
  64. }
  65. /// <summary>
  66. /// Write a short value to buffer LSB first
  67. /// </summary>
  68. /// <param name="value">
  69. /// The value to write.
  70. /// </param>
  71. public void WriteShort(int value)
  72. {
  73. #if DebugDeflation
  74. if (DeflaterConstants.DEBUGGING && (start != 0) )
  75. {
  76. throw new SharpZipBaseException("Debug check: start != 0");
  77. }
  78. #endif
  79. buffer[end++] = unchecked((byte)value);
  80. buffer[end++] = unchecked((byte)(value >> 8));
  81. }
  82. /// <summary>
  83. /// write an integer LSB first
  84. /// </summary>
  85. /// <param name="value">The value to write.</param>
  86. public void WriteInt(int value)
  87. {
  88. #if DebugDeflation
  89. if (DeflaterConstants.DEBUGGING && (start != 0) )
  90. {
  91. throw new SharpZipBaseException("Debug check: start != 0");
  92. }
  93. #endif
  94. buffer[end++] = unchecked((byte)value);
  95. buffer[end++] = unchecked((byte)(value >> 8));
  96. buffer[end++] = unchecked((byte)(value >> 16));
  97. buffer[end++] = unchecked((byte)(value >> 24));
  98. }
  99. /// <summary>
  100. /// Write a block of data to buffer
  101. /// </summary>
  102. /// <param name="block">data to write</param>
  103. /// <param name="offset">offset of first byte to write</param>
  104. /// <param name="length">number of bytes to write</param>
  105. public void WriteBlock(byte[] block, int offset, int length)
  106. {
  107. #if DebugDeflation
  108. if (DeflaterConstants.DEBUGGING && (start != 0) )
  109. {
  110. throw new SharpZipBaseException("Debug check: start != 0");
  111. }
  112. #endif
  113. System.Array.Copy(block, offset, buffer, end, length);
  114. end += length;
  115. }
  116. /// <summary>
  117. /// The number of bits written to the buffer
  118. /// </summary>
  119. public int BitCount {
  120. get {
  121. return bitCount;
  122. }
  123. }
  124. /// <summary>
  125. /// Align internal buffer on a byte boundary
  126. /// </summary>
  127. public void AlignToByte()
  128. {
  129. #if DebugDeflation
  130. if (DeflaterConstants.DEBUGGING && (start != 0) )
  131. {
  132. throw new SharpZipBaseException("Debug check: start != 0");
  133. }
  134. #endif
  135. if (bitCount > 0) {
  136. buffer[end++] = unchecked((byte)bits);
  137. if (bitCount > 8) {
  138. buffer[end++] = unchecked((byte)(bits >> 8));
  139. }
  140. }
  141. bits = 0;
  142. bitCount = 0;
  143. }
  144. /// <summary>
  145. /// Write bits to internal buffer
  146. /// </summary>
  147. /// <param name="b">source of bits</param>
  148. /// <param name="count">number of bits to write</param>
  149. public void WriteBits(int b, int count)
  150. {
  151. #if DebugDeflation
  152. if (DeflaterConstants.DEBUGGING && (start != 0) )
  153. {
  154. throw new SharpZipBaseException("Debug check: start != 0");
  155. }
  156. // if (DeflaterConstants.DEBUGGING) {
  157. // //Console.WriteLine("writeBits("+b+","+count+")");
  158. // }
  159. #endif
  160. bits |= (uint)(b << bitCount);
  161. bitCount += count;
  162. if (bitCount >= 16) {
  163. buffer[end++] = unchecked((byte)bits);
  164. buffer[end++] = unchecked((byte)(bits >> 8));
  165. bits >>= 16;
  166. bitCount -= 16;
  167. }
  168. }
  169. /// <summary>
  170. /// Write a short value to internal buffer most significant byte first
  171. /// </summary>
  172. /// <param name="s">value to write</param>
  173. public void WriteShortMSB(int s)
  174. {
  175. #if DebugDeflation
  176. if (DeflaterConstants.DEBUGGING && (start != 0) )
  177. {
  178. throw new SharpZipBaseException("Debug check: start != 0");
  179. }
  180. #endif
  181. buffer[end++] = unchecked((byte)(s >> 8));
  182. buffer[end++] = unchecked((byte)s);
  183. }
  184. /// <summary>
  185. /// Indicates if buffer has been flushed
  186. /// </summary>
  187. public bool IsFlushed {
  188. get {
  189. return end == 0;
  190. }
  191. }
  192. /// <summary>
  193. /// Flushes the pending buffer into the given output array. If the
  194. /// output array is to small, only a partial flush is done.
  195. /// </summary>
  196. /// <param name="output">The output array.</param>
  197. /// <param name="offset">The offset into output array.</param>
  198. /// <param name="length">The maximum number of bytes to store.</param>
  199. /// <returns>The number of bytes flushed.</returns>
  200. public int Flush(byte[] output, int offset, int length)
  201. {
  202. if (bitCount >= 8) {
  203. buffer[end++] = unchecked((byte)bits);
  204. bits >>= 8;
  205. bitCount -= 8;
  206. }
  207. if (length > end - start) {
  208. length = end - start;
  209. System.Array.Copy(buffer, start, output, offset, length);
  210. start = 0;
  211. end = 0;
  212. } else {
  213. System.Array.Copy(buffer, start, output, offset, length);
  214. start += length;
  215. }
  216. return length;
  217. }
  218. /// <summary>
  219. /// Convert internal buffer to byte array.
  220. /// Buffer is empty on completion
  221. /// </summary>
  222. /// <returns>
  223. /// The internal buffer contents converted to a byte array.
  224. /// </returns>
  225. public byte[] ToByteArray()
  226. {
  227. AlignToByte();
  228. byte[] result = new byte[end - start];
  229. System.Array.Copy(buffer, start, result, 0, result.Length);
  230. start = 0;
  231. end = 0;
  232. return result;
  233. }
  234. }
  235. }