123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- using System;
- namespace CommonMPQ.SharpZipLib.Zip.Compression
- {
-
-
-
-
-
-
-
-
-
- public class PendingBuffer
- {
- #region Instance Fields
-
-
-
- byte[] buffer_;
-
- int start;
- int end;
-
- uint bits;
- int bitCount;
- #endregion
- #region Constructors
-
-
-
- public PendingBuffer() : this( 4096 )
- {
- }
-
-
-
-
-
-
-
- public PendingBuffer(int bufferSize)
- {
- buffer_ = new byte[bufferSize];
- }
- #endregion
-
-
-
- public void Reset()
- {
- start = end = bitCount = 0;
- }
-
-
-
-
-
-
- public void WriteByte(int value)
- {
- #if DebugDeflation
- if (DeflaterConstants.DEBUGGING && (start != 0) )
- {
- throw new SharpZipBaseException("Debug check: start != 0");
- }
- #endif
- buffer_[end++] = unchecked((byte) value);
- }
-
-
-
-
-
-
- public void WriteShort(int value)
- {
- #if DebugDeflation
- if (DeflaterConstants.DEBUGGING && (start != 0) )
- {
- throw new SharpZipBaseException("Debug check: start != 0");
- }
- #endif
- buffer_[end++] = unchecked((byte) value);
- buffer_[end++] = unchecked((byte) (value >> 8));
- }
-
-
-
-
- public void WriteInt(int value)
- {
- #if DebugDeflation
- if (DeflaterConstants.DEBUGGING && (start != 0) )
- {
- throw new SharpZipBaseException("Debug check: start != 0");
- }
- #endif
- buffer_[end++] = unchecked((byte) value);
- buffer_[end++] = unchecked((byte) (value >> 8));
- buffer_[end++] = unchecked((byte) (value >> 16));
- buffer_[end++] = unchecked((byte) (value >> 24));
- }
-
-
-
-
-
-
-
- public void WriteBlock(byte[] block, int offset, int length)
- {
- #if DebugDeflation
- if (DeflaterConstants.DEBUGGING && (start != 0) )
- {
- throw new SharpZipBaseException("Debug check: start != 0");
- }
- #endif
- System.Array.Copy(block, offset, buffer_, end, length);
- end += length;
- }
-
-
-
- public int BitCount {
- get {
- return bitCount;
- }
- }
-
-
-
-
- public void AlignToByte()
- {
- #if DebugDeflation
- if (DeflaterConstants.DEBUGGING && (start != 0) )
- {
- throw new SharpZipBaseException("Debug check: start != 0");
- }
- #endif
- if (bitCount > 0)
- {
- buffer_[end++] = unchecked((byte) bits);
- if (bitCount > 8) {
- buffer_[end++] = unchecked((byte) (bits >> 8));
- }
- }
- bits = 0;
- bitCount = 0;
- }
-
-
-
-
-
- public void WriteBits(int b, int count)
- {
- #if DebugDeflation
- if (DeflaterConstants.DEBUGGING && (start != 0) )
- {
- throw new SharpZipBaseException("Debug check: start != 0");
- }
-
-
-
- #endif
- bits |= (uint)(b << bitCount);
- bitCount += count;
- if (bitCount >= 16) {
- buffer_[end++] = unchecked((byte) bits);
- buffer_[end++] = unchecked((byte) (bits >> 8));
- bits >>= 16;
- bitCount -= 16;
- }
- }
-
-
-
-
- public void WriteShortMSB(int s)
- {
- #if DebugDeflation
- if (DeflaterConstants.DEBUGGING && (start != 0) )
- {
- throw new SharpZipBaseException("Debug check: start != 0");
- }
- #endif
- buffer_[end++] = unchecked((byte) (s >> 8));
- buffer_[end++] = unchecked((byte) s);
- }
-
-
-
-
- public bool IsFlushed {
- get {
- return end == 0;
- }
- }
-
-
-
-
-
-
-
-
-
- public int Flush(byte[] output, int offset, int length)
- {
- if (bitCount >= 8) {
- buffer_[end++] = unchecked((byte) bits);
- bits >>= 8;
- bitCount -= 8;
- }
- if (length > end - start) {
- length = end - start;
- System.Array.Copy(buffer_, start, output, offset, length);
- start = 0;
- end = 0;
- } else {
- System.Array.Copy(buffer_, start, output, offset, length);
- start += length;
- }
- return length;
- }
-
-
-
-
-
-
-
- public byte[] ToByteArray()
- {
- byte[] result = new byte[end - start];
- System.Array.Copy(buffer_, start, result, 0, result.Length);
- start = 0;
- end = 0;
- return result;
- }
- }
- }
|