Home | History | Annotate | Line # | Download | only in DotZLib
      1      1.1  christos //
      2      1.1  christos //  Copyright Henrik Ravn 2004
      3      1.1  christos //
      4  1.1.1.2  christos // Use, modification and distribution are subject to the Boost Software License, Version 1.0.
      5      1.1  christos // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
      6      1.1  christos //
      7      1.1  christos 
      8      1.1  christos using System;
      9      1.1  christos using System.Runtime.InteropServices;
     10      1.1  christos 
     11      1.1  christos namespace DotZLib
     12      1.1  christos {
     13      1.1  christos 	/// <summary>
     14      1.1  christos 	/// Implements the common functionality needed for all <see cref="Codec"/>s
     15      1.1  christos 	/// </summary>
     16      1.1  christos 	public abstract class CodecBase : Codec, IDisposable
     17      1.1  christos 	{
     18      1.1  christos 
     19      1.1  christos         #region Data members
     20      1.1  christos 
     21      1.1  christos         /// <summary>
     22  1.1.1.2  christos         /// Instance of the internal zlib buffer structure that is
     23      1.1  christos         /// passed to all functions in the zlib dll
     24      1.1  christos         /// </summary>
     25      1.1  christos         internal ZStream _ztream = new ZStream();
     26      1.1  christos 
     27      1.1  christos         /// <summary>
     28      1.1  christos         /// True if the object instance has been disposed, false otherwise
     29      1.1  christos         /// </summary>
     30      1.1  christos         protected bool _isDisposed = false;
     31      1.1  christos 
     32      1.1  christos         /// <summary>
     33      1.1  christos         /// The size of the internal buffers
     34      1.1  christos         /// </summary>
     35      1.1  christos         protected const int kBufferSize = 16384;
     36      1.1  christos 
     37      1.1  christos         private byte[] _outBuffer = new byte[kBufferSize];
     38      1.1  christos         private byte[] _inBuffer = new byte[kBufferSize];
     39      1.1  christos 
     40      1.1  christos         private GCHandle _hInput;
     41      1.1  christos         private GCHandle _hOutput;
     42      1.1  christos 
     43      1.1  christos         private uint _checksum = 0;
     44      1.1  christos 
     45      1.1  christos         #endregion
     46      1.1  christos 
     47      1.1  christos         /// <summary>
     48  1.1.1.2  christos         /// Initializes a new instance of the <c>CodeBase</c> class.
     49      1.1  christos         /// </summary>
     50      1.1  christos 		public CodecBase()
     51      1.1  christos 		{
     52      1.1  christos             try
     53      1.1  christos             {
     54      1.1  christos                 _hInput = GCHandle.Alloc(_inBuffer, GCHandleType.Pinned);
     55      1.1  christos                 _hOutput = GCHandle.Alloc(_outBuffer, GCHandleType.Pinned);
     56      1.1  christos             }
     57      1.1  christos             catch (Exception)
     58      1.1  christos             {
     59      1.1  christos                 CleanUp(false);
     60      1.1  christos                 throw;
     61      1.1  christos             }
     62      1.1  christos         }
     63      1.1  christos 
     64      1.1  christos 
     65      1.1  christos         #region Codec Members
     66      1.1  christos 
     67      1.1  christos         /// <summary>
     68      1.1  christos         /// Occurs when more processed data are available.
     69      1.1  christos         /// </summary>
     70      1.1  christos         public event DataAvailableHandler DataAvailable;
     71      1.1  christos 
     72      1.1  christos         /// <summary>
     73      1.1  christos         /// Fires the <see cref="DataAvailable"/> event
     74      1.1  christos         /// </summary>
     75      1.1  christos         protected void OnDataAvailable()
     76      1.1  christos         {
     77      1.1  christos             if (_ztream.total_out > 0)
     78      1.1  christos             {
     79      1.1  christos                 if (DataAvailable != null)
     80  1.1.1.2  christos                     DataAvailable( _outBuffer, 0, (int)_ztream.total_out);
     81      1.1  christos                 resetOutput();
     82      1.1  christos             }
     83      1.1  christos         }
     84      1.1  christos 
     85      1.1  christos         /// <summary>
     86      1.1  christos         /// Adds more data to the codec to be processed.
     87      1.1  christos         /// </summary>
     88      1.1  christos         /// <param name="data">Byte array containing the data to be added to the codec</param>
     89      1.1  christos         /// <remarks>Adding data may, or may not, raise the <c>DataAvailable</c> event</remarks>
     90      1.1  christos         public void Add(byte[] data)
     91      1.1  christos         {
     92      1.1  christos             Add(data,0,data.Length);
     93      1.1  christos         }
     94      1.1  christos 
     95      1.1  christos         /// <summary>
     96      1.1  christos         /// Adds more data to the codec to be processed.
     97      1.1  christos         /// </summary>
     98      1.1  christos         /// <param name="data">Byte array containing the data to be added to the codec</param>
     99      1.1  christos         /// <param name="offset">The index of the first byte to add from <c>data</c></param>
    100      1.1  christos         /// <param name="count">The number of bytes to add</param>
    101      1.1  christos         /// <remarks>Adding data may, or may not, raise the <c>DataAvailable</c> event</remarks>
    102      1.1  christos         /// <remarks>This must be implemented by a derived class</remarks>
    103      1.1  christos         public abstract void Add(byte[] data, int offset, int count);
    104      1.1  christos 
    105      1.1  christos         /// <summary>
    106      1.1  christos         /// Finishes up any pending data that needs to be processed and handled.
    107      1.1  christos         /// </summary>
    108      1.1  christos         /// <remarks>This must be implemented by a derived class</remarks>
    109      1.1  christos         public abstract void Finish();
    110      1.1  christos 
    111      1.1  christos         /// <summary>
    112      1.1  christos         /// Gets the checksum of the data that has been added so far
    113      1.1  christos         /// </summary>
    114      1.1  christos         public uint Checksum { get { return _checksum; } }
    115      1.1  christos 
    116      1.1  christos         #endregion
    117      1.1  christos 
    118      1.1  christos         #region Destructor & IDisposable stuff
    119      1.1  christos 
    120      1.1  christos         /// <summary>
    121      1.1  christos         /// Destroys this instance
    122      1.1  christos         /// </summary>
    123      1.1  christos         ~CodecBase()
    124      1.1  christos         {
    125      1.1  christos             CleanUp(false);
    126      1.1  christos         }
    127      1.1  christos 
    128      1.1  christos         /// <summary>
    129      1.1  christos         /// Releases any unmanaged resources and calls the <see cref="CleanUp()"/> method of the derived class
    130      1.1  christos         /// </summary>
    131      1.1  christos         public void Dispose()
    132      1.1  christos         {
    133      1.1  christos             CleanUp(true);
    134      1.1  christos         }
    135      1.1  christos 
    136      1.1  christos         /// <summary>
    137      1.1  christos         /// Performs any codec specific cleanup
    138      1.1  christos         /// </summary>
    139      1.1  christos         /// <remarks>This must be implemented by a derived class</remarks>
    140      1.1  christos         protected abstract void CleanUp();
    141      1.1  christos 
    142  1.1.1.3  christos         // performs the release of the handles and calls the derived CleanUp()
    143      1.1  christos         private void CleanUp(bool isDisposing)
    144      1.1  christos         {
    145      1.1  christos             if (!_isDisposed)
    146      1.1  christos             {
    147      1.1  christos                 CleanUp();
    148      1.1  christos                 if (_hInput.IsAllocated)
    149      1.1  christos                     _hInput.Free();
    150      1.1  christos                 if (_hOutput.IsAllocated)
    151      1.1  christos                     _hOutput.Free();
    152      1.1  christos 
    153      1.1  christos                 _isDisposed = true;
    154      1.1  christos             }
    155      1.1  christos         }
    156      1.1  christos 
    157      1.1  christos 
    158      1.1  christos         #endregion
    159      1.1  christos 
    160      1.1  christos         #region Helper methods
    161      1.1  christos 
    162      1.1  christos         /// <summary>
    163  1.1.1.3  christos         /// Copies a number of bytes to the internal codec buffer - ready for processing
    164      1.1  christos         /// </summary>
    165      1.1  christos         /// <param name="data">The byte array that contains the data to copy</param>
    166      1.1  christos         /// <param name="startIndex">The index of the first byte to copy</param>
    167      1.1  christos         /// <param name="count">The number of bytes to copy from <c>data</c></param>
    168      1.1  christos         protected void copyInput(byte[] data, int startIndex, int count)
    169      1.1  christos         {
    170      1.1  christos             Array.Copy(data, startIndex, _inBuffer,0, count);
    171      1.1  christos             _ztream.next_in = _hInput.AddrOfPinnedObject();
    172      1.1  christos             _ztream.total_in = 0;
    173      1.1  christos             _ztream.avail_in = (uint)count;
    174      1.1  christos 
    175      1.1  christos         }
    176      1.1  christos 
    177      1.1  christos         /// <summary>
    178      1.1  christos         /// Resets the internal output buffers to a known state - ready for processing
    179      1.1  christos         /// </summary>
    180      1.1  christos         protected void resetOutput()
    181      1.1  christos         {
    182      1.1  christos             _ztream.total_out = 0;
    183      1.1  christos             _ztream.avail_out = kBufferSize;
    184      1.1  christos             _ztream.next_out = _hOutput.AddrOfPinnedObject();
    185      1.1  christos         }
    186      1.1  christos 
    187      1.1  christos         /// <summary>
    188      1.1  christos         /// Updates the running checksum property
    189      1.1  christos         /// </summary>
    190      1.1  christos         /// <param name="newSum">The new checksum value</param>
    191      1.1  christos         protected void setChecksum(uint newSum)
    192      1.1  christos         {
    193      1.1  christos             _checksum = newSum;
    194      1.1  christos         }
    195      1.1  christos         #endregion
    196      1.1  christos 
    197      1.1  christos     }
    198      1.1  christos }
    199