123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- using System;
- using System.Runtime.InteropServices;
- namespace DotZLib
- {
-
-
-
- public abstract class CodecBase : Codec, IDisposable
- {
- #region Data members
-
-
-
-
- internal ZStream _ztream = new ZStream();
-
-
-
- protected bool _isDisposed = false;
-
-
-
- protected const int kBufferSize = 16384;
- private byte[] _outBuffer = new byte[kBufferSize];
- private byte[] _inBuffer = new byte[kBufferSize];
- private GCHandle _hInput;
- private GCHandle _hOutput;
- private uint _checksum = 0;
- #endregion
-
-
-
- public CodecBase()
- {
- try
- {
- _hInput = GCHandle.Alloc(_inBuffer, GCHandleType.Pinned);
- _hOutput = GCHandle.Alloc(_outBuffer, GCHandleType.Pinned);
- }
- catch (Exception)
- {
- CleanUp(false);
- throw;
- }
- }
- #region Codec Members
-
-
-
- public event DataAvailableHandler DataAvailable;
-
-
-
- protected void OnDataAvailable()
- {
- if (_ztream.total_out > 0)
- {
- if (DataAvailable != null)
- DataAvailable( _outBuffer, 0, (int)_ztream.total_out);
- resetOutput();
- }
- }
-
-
-
-
-
- public void Add(byte[] data)
- {
- Add(data,0,data.Length);
- }
-
-
-
-
-
-
-
-
- public abstract void Add(byte[] data, int offset, int count);
-
-
-
-
- public abstract void Finish();
-
-
-
- public uint Checksum { get { return _checksum; } }
- #endregion
- #region Destructor & IDisposable stuff
-
-
-
- ~CodecBase()
- {
- CleanUp(false);
- }
-
-
-
- public void Dispose()
- {
- CleanUp(true);
- }
-
-
-
-
- protected abstract void CleanUp();
-
- private void CleanUp(bool isDisposing)
- {
- if (!_isDisposed)
- {
- CleanUp();
- if (_hInput.IsAllocated)
- _hInput.Free();
- if (_hOutput.IsAllocated)
- _hOutput.Free();
- _isDisposed = true;
- }
- }
- #endregion
- #region Helper methods
-
-
-
-
-
-
- protected void copyInput(byte[] data, int startIndex, int count)
- {
- Array.Copy(data, startIndex, _inBuffer,0, count);
- _ztream.next_in = _hInput.AddrOfPinnedObject();
- _ztream.total_in = 0;
- _ztream.avail_in = (uint)count;
- }
-
-
-
- protected void resetOutput()
- {
- _ztream.total_out = 0;
- _ztream.avail_out = kBufferSize;
- _ztream.next_out = _hOutput.AddrOfPinnedObject();
- }
-
-
-
-
- protected void setChecksum(uint newSum)
- {
- _checksum = newSum;
- }
- #endregion
- }
- }
|