annotate src/zlib-1.2.8/contrib/dotzlib/DotZLib/CodecBase.cs @ 56:af97cad61ff0

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