00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 using System;
00041
00042 namespace ICSharpCode.SharpZipLib.Zip.Compression
00043 {
00044
00053 public class PendingBuffer
00054 {
00057 protected byte[] buf;
00058
00059 int start;
00060 int end;
00061
00062 uint bits;
00063 int bitCount;
00064
00068 public PendingBuffer() : this( 4096 )
00069 {
00070
00071 }
00072
00079 public PendingBuffer(int bufsize)
00080 {
00081 buf = new byte[bufsize];
00082 }
00083
00087 public void Reset()
00088 {
00089 start = end = bitCount = 0;
00090 }
00091
00098 public void WriteByte(int b)
00099 {
00100 if (DeflaterConstants.DEBUGGING && start != 0) {
00101 throw new SharpZipBaseException();
00102 }
00103 buf[end++] = (byte) b;
00104 }
00105
00112 public void WriteShort(int s)
00113 {
00114 if (DeflaterConstants.DEBUGGING && start != 0) {
00115 throw new SharpZipBaseException();
00116 }
00117 buf[end++] = (byte) s;
00118 buf[end++] = (byte) (s >> 8);
00119 }
00120
00125 public void WriteInt(int s)
00126 {
00127 if (DeflaterConstants.DEBUGGING && start != 0) {
00128 throw new SharpZipBaseException();
00129 }
00130 buf[end++] = (byte) s;
00131 buf[end++] = (byte) (s >> 8);
00132 buf[end++] = (byte) (s >> 16);
00133 buf[end++] = (byte) (s >> 24);
00134 }
00135
00142 public void WriteBlock(byte[] block, int offset, int len)
00143 {
00144 if (DeflaterConstants.DEBUGGING && start != 0) {
00145 throw new SharpZipBaseException();
00146 }
00147 System.Array.Copy(block, offset, buf, end, len);
00148 end += len;
00149 }
00150
00154 public int BitCount {
00155 get {
00156 return bitCount;
00157 }
00158 }
00159
00163 public void AlignToByte()
00164 {
00165 if (DeflaterConstants.DEBUGGING && start != 0) {
00166 throw new SharpZipBaseException();
00167 }
00168 if (bitCount > 0) {
00169 buf[end++] = (byte) bits;
00170 if (bitCount > 8) {
00171 buf[end++] = (byte) (bits >> 8);
00172 }
00173 }
00174 bits = 0;
00175 bitCount = 0;
00176 }
00177
00183 public void WriteBits(int b, int count)
00184 {
00185 if (DeflaterConstants.DEBUGGING && start != 0) {
00186 throw new SharpZipBaseException();
00187 }
00188
00189
00190
00191 bits |= (uint)(b << bitCount);
00192 bitCount += count;
00193 if (bitCount >= 16) {
00194 buf[end++] = (byte) bits;
00195 buf[end++] = (byte) (bits >> 8);
00196 bits >>= 16;
00197 bitCount -= 16;
00198 }
00199 }
00200
00205 public void WriteShortMSB(int s)
00206 {
00207 if (DeflaterConstants.DEBUGGING && start != 0) {
00208 throw new SharpZipBaseException();
00209 }
00210 buf[end++] = (byte) (s >> 8);
00211 buf[end++] = (byte) s;
00212 }
00213
00217 public bool IsFlushed {
00218 get {
00219 return end == 0;
00220 }
00221 }
00222
00239 public int Flush(byte[] output, int offset, int length)
00240 {
00241 if (bitCount >= 8) {
00242 buf[end++] = (byte) bits;
00243 bits >>= 8;
00244 bitCount -= 8;
00245 }
00246 if (length > end - start) {
00247 length = end - start;
00248 System.Array.Copy(buf, start, output, offset, length);
00249 start = 0;
00250 end = 0;
00251 } else {
00252 System.Array.Copy(buf, start, output, offset, length);
00253 start += length;
00254 }
00255 return length;
00256 }
00257
00265 public byte[] ToByteArray()
00266 {
00267 byte[] ret = new byte[end - start];
00268 System.Array.Copy(buf, start, ret, 0, ret.Length);
00269 start = 0;
00270 end = 0;
00271 return ret;
00272 }
00273 }
00274 }