PendingBuffer.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // PendingBuffer.cs
  2. // Copyright (C) 2001 Mike Krueger
  3. //
  4. // This file was translated from java, it was part of the GNU Classpath
  5. // Copyright (C) 2001 Free Software Foundation, Inc.
  6. //
  7. // This program is free software; you can redistribute it and/or
  8. // modify it under the terms of the GNU General Public License
  9. // as published by the Free Software Foundation; either version 2
  10. // of the License, or (at your option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with this program; if not, write to the Free Software
  19. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. //
  21. // Linking this library statically or dynamically with other modules is
  22. // making a combined work based on this library. Thus, the terms and
  23. // conditions of the GNU General Public License cover the whole
  24. // combination.
  25. //
  26. // As a special exception, the copyright holders of this library give you
  27. // permission to link this library with independent modules to produce an
  28. // executable, regardless of the license terms of these independent
  29. // modules, and to copy and distribute the resulting executable under
  30. // terms of your choice, provided that you also meet, for each linked
  31. // independent module, the terms and conditions of the license of that
  32. // module. An independent module is a module which is not derived from
  33. // or based on this library. If you modify this library, you may extend
  34. // this exception to your version of the library, but you are not
  35. // obligated to do so. If you do not wish to do so, delete this
  36. // exception statement from your version.
  37. using System;
  38. namespace ICSharpCode.SharpZipLib.Zip.Compression
  39. {
  40. /// <summary>
  41. /// This class is general purpose class for writing data to a buffer.
  42. ///
  43. /// It allows you to write bits as well as bytes
  44. /// Based on DeflaterPending.java
  45. ///
  46. /// author of the original java version : Jochen Hoenicke
  47. /// </summary>
  48. public class PendingBuffer
  49. {
  50. protected byte[] buf;
  51. int start;
  52. int end;
  53. uint bits;
  54. int bitCount;
  55. public PendingBuffer() : this( 4096 )
  56. {
  57. }
  58. public PendingBuffer(int bufsize)
  59. {
  60. buf = new byte[bufsize];
  61. }
  62. public void Reset()
  63. {
  64. start = end = bitCount = 0;
  65. }
  66. public void WriteByte(int b)
  67. {
  68. if (DeflaterConstants.DEBUGGING && start != 0) {
  69. throw new Exception();
  70. }
  71. buf[end++] = (byte) b;
  72. }
  73. public void WriteShort(int s)
  74. {
  75. if (DeflaterConstants.DEBUGGING && start != 0) {
  76. throw new Exception();
  77. }
  78. buf[end++] = (byte) s;
  79. buf[end++] = (byte) (s >> 8);
  80. }
  81. public void WriteInt(int s)
  82. {
  83. if (DeflaterConstants.DEBUGGING && start != 0) {
  84. throw new Exception();
  85. }
  86. buf[end++] = (byte) s;
  87. buf[end++] = (byte) (s >> 8);
  88. buf[end++] = (byte) (s >> 16);
  89. buf[end++] = (byte) (s >> 24);
  90. }
  91. public void WriteBlock(byte[] block, int offset, int len)
  92. {
  93. if (DeflaterConstants.DEBUGGING && start != 0) {
  94. throw new Exception();
  95. }
  96. System.Array.Copy(block, offset, buf, end, len);
  97. end += len;
  98. }
  99. public int BitCount {
  100. get {
  101. return bitCount;
  102. }
  103. }
  104. public void AlignToByte()
  105. {
  106. if (DeflaterConstants.DEBUGGING && start != 0) {
  107. throw new Exception();
  108. }
  109. if (bitCount > 0) {
  110. buf[end++] = (byte) bits;
  111. if (bitCount > 8) {
  112. buf[end++] = (byte) (bits >> 8);
  113. }
  114. }
  115. bits = 0;
  116. bitCount = 0;
  117. }
  118. public void WriteBits(int b, int count)
  119. {
  120. if (DeflaterConstants.DEBUGGING && start != 0) {
  121. throw new Exception();
  122. }
  123. // if (DeflaterConstants.DEBUGGING) {
  124. // //Console.WriteLine("writeBits("+b+","+count+")");
  125. // }
  126. bits |= (uint)(b << bitCount);
  127. bitCount += count;
  128. if (bitCount >= 16) {
  129. buf[end++] = (byte) bits;
  130. buf[end++] = (byte) (bits >> 8);
  131. bits >>= 16;
  132. bitCount -= 16;
  133. }
  134. }
  135. public void WriteShortMSB(int s)
  136. {
  137. if (DeflaterConstants.DEBUGGING && start != 0) {
  138. throw new Exception();
  139. }
  140. buf[end++] = (byte) (s >> 8);
  141. buf[end++] = (byte) s;
  142. }
  143. public bool IsFlushed {
  144. get {
  145. return end == 0;
  146. }
  147. }
  148. /// <summary>
  149. /// Flushes the pending buffer into the given output array. If the
  150. /// output array is to small, only a partial flush is done.
  151. /// </summary>
  152. /// <param name="output">
  153. /// the output array;
  154. /// </param>
  155. /// <param name="offset">
  156. /// the offset into output array;
  157. /// </param>
  158. /// <param name="length">
  159. /// length the maximum number of bytes to store;
  160. /// </param>
  161. /// <exception name="ArgumentOutOfRangeException">
  162. /// IndexOutOfBoundsException if offset or length are invalid.
  163. /// </exception>
  164. public int Flush(byte[] output, int offset, int length)
  165. {
  166. if (bitCount >= 8) {
  167. buf[end++] = (byte) bits;
  168. bits >>= 8;
  169. bitCount -= 8;
  170. }
  171. if (length > end - start) {
  172. length = end - start;
  173. System.Array.Copy(buf, start, output, offset, length);
  174. start = 0;
  175. end = 0;
  176. } else {
  177. System.Array.Copy(buf, start, output, offset, length);
  178. start += length;
  179. }
  180. return length;
  181. }
  182. public byte[] ToByteArray()
  183. {
  184. byte[] ret = new byte[end - start];
  185. System.Array.Copy(buf, start, ret, 0, ret.Length);
  186. start = 0;
  187. end = 0;
  188. return ret;
  189. }
  190. }
  191. }