OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [zlib/] [contrib/] [dotzlib/] [DotZLib/] [CodecBase.cs] - Blame information for rev 745

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.