PendingBuffer.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // PendingBuffer.cs
  2. //
  3. // Copyright (C) 2001 Mike Krueger
  4. // Copyright (C) 2004 John Reilly
  5. //
  6. // This file was translated from java, it was part of the GNU Classpath
  7. // Copyright (C) 2001 Free Software Foundation, Inc.
  8. //
  9. // This program is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU General Public License
  11. // as published by the Free Software Foundation; either version 2
  12. // of the License, or (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. // GNU General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License
  20. // along with this program; if not, write to the Free Software
  21. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. //
  23. // Linking this library statically or dynamically with other modules is
  24. // making a combined work based on this library. Thus, the terms and
  25. // conditions of the GNU General Public License cover the whole
  26. // combination.
  27. //
  28. // As a special exception, the copyright holders of this library give you
  29. // permission to link this library with independent modules to produce an
  30. // executable, regardless of the license terms of these independent
  31. // modules, and to copy and distribute the resulting executable under
  32. // terms of your choice, provided that you also meet, for each linked
  33. // independent module, the terms and conditions of the license of that
  34. // module. An independent module is a module which is not derived from
  35. // or based on this library. If you modify this library, you may extend
  36. // this exception to your version of the library, but you are not
  37. // obligated to do so. If you do not wish to do so, delete this
  38. // exception statement from your version.
  39. using System;
  40. namespace CommonMPQ.SharpZipLib.Zip.Compression
  41. {
  42. /// <summary>
  43. /// This class is general purpose class for writing data to a buffer.
  44. ///
  45. /// It allows you to write bits as well as bytes
  46. /// Based on DeflaterPending.java
  47. ///
  48. /// author of the original java version : Jochen Hoenicke
  49. /// </summary>
  50. public class PendingBuffer
  51. {
  52. #region Instance Fields
  53. /// <summary>
  54. /// Internal work buffer
  55. /// </summary>
  56. byte[] buffer_;
  57. int start;
  58. int end;
  59. uint bits;
  60. int bitCount;
  61. #endregion
  62. #region Constructors
  63. /// <summary>
  64. /// construct instance using default buffer size of 4096
  65. /// </summary>
  66. public PendingBuffer() : this( 4096 )
  67. {
  68. }
  69. /// <summary>
  70. /// construct instance using specified buffer size
  71. /// </summary>
  72. /// <param name="bufferSize">
  73. /// size to use for internal buffer
  74. /// </param>
  75. public PendingBuffer(int bufferSize)
  76. {
  77. buffer_ = new byte[bufferSize];
  78. }
  79. #endregion
  80. /// <summary>
  81. /// Clear internal state/buffers
  82. /// </summary>
  83. public void Reset()
  84. {
  85. start = end = bitCount = 0;
  86. }
  87. /// <summary>
  88. /// Write a byte to buffer
  89. /// </summary>
  90. /// <param name="value">
  91. /// The value to write
  92. /// </param>
  93. public void WriteByte(int value)
  94. {
  95. #if DebugDeflation
  96. if (DeflaterConstants.DEBUGGING && (start != 0) )
  97. {
  98. throw new SharpZipBaseException("Debug check: start != 0");
  99. }
  100. #endif
  101. buffer_[end++] = unchecked((byte) value);
  102. }
  103. /// <summary>
  104. /// Write a short value to buffer LSB first
  105. /// </summary>
  106. /// <param name="value">
  107. /// The value to write.
  108. /// </param>
  109. public void WriteShort(int value)
  110. {
  111. #if DebugDeflation
  112. if (DeflaterConstants.DEBUGGING && (start != 0) )
  113. {
  114. throw new SharpZipBaseException("Debug check: start != 0");
  115. }
  116. #endif
  117. buffer_[end++] = unchecked((byte) value);
  118. buffer_[end++] = unchecked((byte) (value >> 8));
  119. }
  120. /// <summary>
  121. /// write an integer LSB first
  122. /// </summary>
  123. /// <param name="value">The value to write.</param>
  124. public void WriteInt(int value)
  125. {
  126. #if DebugDeflation
  127. if (DeflaterConstants.DEBUGGING && (start != 0) )
  128. {
  129. throw new SharpZipBaseException("Debug check: start != 0");
  130. }
  131. #endif
  132. buffer_[end++] = unchecked((byte) value);
  133. buffer_[end++] = unchecked((byte) (value >> 8));
  134. buffer_[end++] = unchecked((byte) (value >> 16));
  135. buffer_[end++] = unchecked((byte) (value >> 24));
  136. }
  137. /// <summary>
  138. /// Write a block of data to buffer
  139. /// </summary>
  140. /// <param name="block">data to write</param>
  141. /// <param name="offset">offset of first byte to write</param>
  142. /// <param name="length">number of bytes to write</param>
  143. public void WriteBlock(byte[] block, int offset, int length)
  144. {
  145. #if DebugDeflation
  146. if (DeflaterConstants.DEBUGGING && (start != 0) )
  147. {
  148. throw new SharpZipBaseException("Debug check: start != 0");
  149. }
  150. #endif
  151. System.Array.Copy(block, offset, buffer_, end, length);
  152. end += length;
  153. }
  154. /// <summary>
  155. /// The number of bits written to the buffer
  156. /// </summary>
  157. public int BitCount {
  158. get {
  159. return bitCount;
  160. }
  161. }
  162. /// <summary>
  163. /// Align internal buffer on a byte boundary
  164. /// </summary>
  165. public void AlignToByte()
  166. {
  167. #if DebugDeflation
  168. if (DeflaterConstants.DEBUGGING && (start != 0) )
  169. {
  170. throw new SharpZipBaseException("Debug check: start != 0");
  171. }
  172. #endif
  173. if (bitCount > 0)
  174. {
  175. buffer_[end++] = unchecked((byte) bits);
  176. if (bitCount > 8) {
  177. buffer_[end++] = unchecked((byte) (bits >> 8));
  178. }
  179. }
  180. bits = 0;
  181. bitCount = 0;
  182. }
  183. /// <summary>
  184. /// Write bits to internal buffer
  185. /// </summary>
  186. /// <param name="b">source of bits</param>
  187. /// <param name="count">number of bits to write</param>
  188. public void WriteBits(int b, int count)
  189. {
  190. #if DebugDeflation
  191. if (DeflaterConstants.DEBUGGING && (start != 0) )
  192. {
  193. throw new SharpZipBaseException("Debug check: start != 0");
  194. }
  195. // if (DeflaterConstants.DEBUGGING) {
  196. // //Console.WriteLine("writeBits("+b+","+count+")");
  197. // }
  198. #endif
  199. bits |= (uint)(b << bitCount);
  200. bitCount += count;
  201. if (bitCount >= 16) {
  202. buffer_[end++] = unchecked((byte) bits);
  203. buffer_[end++] = unchecked((byte) (bits >> 8));
  204. bits >>= 16;
  205. bitCount -= 16;
  206. }
  207. }
  208. /// <summary>
  209. /// Write a short value to internal buffer most significant byte first
  210. /// </summary>
  211. /// <param name="s">value to write</param>
  212. public void WriteShortMSB(int s)
  213. {
  214. #if DebugDeflation
  215. if (DeflaterConstants.DEBUGGING && (start != 0) )
  216. {
  217. throw new SharpZipBaseException("Debug check: start != 0");
  218. }
  219. #endif
  220. buffer_[end++] = unchecked((byte) (s >> 8));
  221. buffer_[end++] = unchecked((byte) s);
  222. }
  223. /// <summary>
  224. /// Indicates if buffer has been flushed
  225. /// </summary>
  226. public bool IsFlushed {
  227. get {
  228. return end == 0;
  229. }
  230. }
  231. /// <summary>
  232. /// Flushes the pending buffer into the given output array. If the
  233. /// output array is to small, only a partial flush is done.
  234. /// </summary>
  235. /// <param name="output">The output array.</param>
  236. /// <param name="offset">The offset into output array.</param>
  237. /// <param name="length">The maximum number of bytes to store.</param>
  238. /// <returns>The number of bytes flushed.</returns>
  239. public int Flush(byte[] output, int offset, int length)
  240. {
  241. if (bitCount >= 8) {
  242. buffer_[end++] = unchecked((byte) bits);
  243. bits >>= 8;
  244. bitCount -= 8;
  245. }
  246. if (length > end - start) {
  247. length = end - start;
  248. System.Array.Copy(buffer_, start, output, offset, length);
  249. start = 0;
  250. end = 0;
  251. } else {
  252. System.Array.Copy(buffer_, start, output, offset, length);
  253. start += length;
  254. }
  255. return length;
  256. }
  257. /// <summary>
  258. /// Convert internal buffer to byte array.
  259. /// Buffer is empty on completion
  260. /// </summary>
  261. /// <returns>
  262. /// The internal buffer contents converted to a byte array.
  263. /// </returns>
  264. public byte[] ToByteArray()
  265. {
  266. byte[] result = new byte[end - start];
  267. System.Array.Copy(buffer_, start, result, 0, result.Length);
  268. start = 0;
  269. end = 0;
  270. return result;
  271. }
  272. }
  273. }