annotate src/zlib-1.2.7/contrib/dotzlib/DotZLib/CodecBase.cs @ 23:619f715526df sv_v2.1

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