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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [zlib/] [contrib/] [dotzlib/] [DotZLib/] [GZipStream.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.IO;
10
using System.Runtime.InteropServices;
11
 
12
namespace DotZLib
13
{
14
        /// 
15
        /// Implements a compressed , in GZip (.gz) format.
16
        /// 
17
        public class GZipStream : Stream, IDisposable
18
        {
19
        #region Dll Imports
20
        [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl, CharSet=CharSet.Ansi)]
21
        private static extern IntPtr gzopen(string name, string mode);
22
 
23
        [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
24
        private static extern int gzclose(IntPtr gzFile);
25
 
26
        [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
27
        private static extern int gzwrite(IntPtr gzFile, int data, int length);
28
 
29
        [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
30
        private static extern int gzread(IntPtr gzFile, int data, int length);
31
 
32
        [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
33
        private static extern int gzgetc(IntPtr gzFile);
34
 
35
        [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
36
        private static extern int gzputc(IntPtr gzFile, int c);
37
 
38
        #endregion
39
 
40
        #region Private data
41
        private IntPtr _gzFile;
42
        private bool _isDisposed = false;
43
        private bool _isWriting;
44
        #endregion
45
 
46
        #region Constructors
47
        /// 
48
        /// Creates a new file as a writeable GZipStream
49
        /// 
50
        /// The name of the compressed file to create
51
        /// The compression level to use when adding data
52
        /// If an error occurred in the internal zlib function
53
                public GZipStream(string fileName, CompressLevel level)
54
                {
55
            _isWriting = true;
56
            _gzFile = gzopen(fileName, String.Format("wb{0}", (int)level));
57
            if (_gzFile == IntPtr.Zero)
58
                throw new ZLibException(-1, "Could not open " + fileName);
59
                }
60
 
61
        /// 
62
        /// Opens an existing file as a readable GZipStream
63
        /// 
64
        /// The name of the file to open
65
        /// If an error occurred in the internal zlib function
66
        public GZipStream(string fileName)
67
        {
68
            _isWriting = false;
69
            _gzFile = gzopen(fileName, "rb");
70
            if (_gzFile == IntPtr.Zero)
71
                throw new ZLibException(-1, "Could not open " + fileName);
72
 
73
        }
74
        #endregion
75
 
76
        #region Access properties
77
        /// 
78
        /// Returns true of this stream can be read from, false otherwise
79
        /// 
80
        public override bool CanRead
81
        {
82
            get
83
            {
84
                return !_isWriting;
85
            }
86
        }
87
 
88
 
89
        /// 
90
        /// Returns false.
91
        /// 
92
        public override bool CanSeek
93
        {
94
            get
95
            {
96
                return false;
97
            }
98
        }
99
 
100
        /// 
101
        /// Returns true if this tsream is writeable, false otherwise
102
        /// 
103
        public override bool CanWrite
104
        {
105
            get
106
            {
107
                return _isWriting;
108
            }
109
        }
110
        #endregion
111
 
112
        #region Destructor & IDispose stuff
113
 
114
        /// 
115
        /// Destroys this instance
116
        /// 
117
        ~GZipStream()
118
        {
119
            cleanUp(false);
120
        }
121
 
122
        /// 
123
        /// Closes the external file handle
124
        /// 
125
        public void Dispose()
126
        {
127
            cleanUp(true);
128
        }
129
 
130
        // Does the actual closing of the file handle.
131
        private void cleanUp(bool isDisposing)
132
        {
133
            if (!_isDisposed)
134
            {
135
                gzclose(_gzFile);
136
                _isDisposed = true;
137
            }
138
        }
139
        #endregion
140
 
141
        #region Basic reading and writing
142
        /// 
143
        /// Attempts to read a number of bytes from the stream.
144
        /// 
145
        /// The destination data buffer
146
        /// The index of the first destination byte in buffer
147
        /// The number of bytes requested
148
        /// The number of bytes read
149
        /// If buffer is null
150
        /// If count or offset are negative
151
        /// If offset  + count is > buffer.Length
152
        /// If this stream is not readable.
153
        /// If this stream has been disposed.
154
        public override int Read(byte[] buffer, int offset, int count)
155
        {
156
            if (!CanRead) throw new NotSupportedException();
157
            if (buffer == null) throw new ArgumentNullException();
158
            if (offset < 0 || count < 0) throw new ArgumentOutOfRangeException();
159
            if ((offset+count) > buffer.Length) throw new ArgumentException();
160
            if (_isDisposed) throw new ObjectDisposedException("GZipStream");
161
 
162
            GCHandle h = GCHandle.Alloc(buffer, GCHandleType.Pinned);
163
            int result;
164
            try
165
            {
166
                result = gzread(_gzFile, h.AddrOfPinnedObject().ToInt32() + offset, count);
167
                if (result < 0)
168
                    throw new IOException();
169
            }
170
            finally
171
            {
172
                h.Free();
173
            }
174
            return result;
175
        }
176
 
177
        /// 
178
        /// Attempts to read a single byte from the stream.
179
        /// 
180
        /// The byte that was read, or -1 in case of error or End-Of-File
181
        public override int ReadByte()
182
        {
183
            if (!CanRead) throw new NotSupportedException();
184
            if (_isDisposed) throw new ObjectDisposedException("GZipStream");
185
            return gzgetc(_gzFile);
186
        }
187
 
188
        /// 
189
        /// Writes a number of bytes to the stream
190
        /// 
191
        /// 
192
        /// 
193
        /// 
194
        /// If buffer is null
195
        /// If count or offset are negative
196
        /// If offset  + count is > buffer.Length
197
        /// If this stream is not writeable.
198
        /// If this stream has been disposed.
199
        public override void Write(byte[] buffer, int offset, int count)
200
        {
201
            if (!CanWrite) throw new NotSupportedException();
202
            if (buffer == null) throw new ArgumentNullException();
203
            if (offset < 0 || count < 0) throw new ArgumentOutOfRangeException();
204
            if ((offset+count) > buffer.Length) throw new ArgumentException();
205
            if (_isDisposed) throw new ObjectDisposedException("GZipStream");
206
 
207
            GCHandle h = GCHandle.Alloc(buffer, GCHandleType.Pinned);
208
            try
209
            {
210
                int result = gzwrite(_gzFile, h.AddrOfPinnedObject().ToInt32() + offset, count);
211
                if (result < 0)
212
                    throw new IOException();
213
            }
214
            finally
215
            {
216
                h.Free();
217
            }
218
        }
219
 
220
        /// 
221
        /// Writes a single byte to the stream
222
        /// 
223
        /// The byte to add to the stream.
224
        /// If this stream is not writeable.
225
        /// If this stream has been disposed.
226
        public override void WriteByte(byte value)
227
        {
228
            if (!CanWrite) throw new NotSupportedException();
229
            if (_isDisposed) throw new ObjectDisposedException("GZipStream");
230
 
231
            int result = gzputc(_gzFile, (int)value);
232
            if (result < 0)
233
                throw new IOException();
234
        }
235
        #endregion
236
 
237
        #region Position & length stuff
238
        /// 
239
        /// Not supported.
240
        /// 
241
        /// 
242
        /// Always thrown
243
        public override void SetLength(long value)
244
        {
245
            throw new NotSupportedException();
246
        }
247
 
248
        /// 
249
        ///  Not suppported.
250
        /// 
251
        /// 
252
        /// 
253
        /// 
254
        /// Always thrown
255
        public override long Seek(long offset, SeekOrigin origin)
256
        {
257
            throw new NotSupportedException();
258
        }
259
 
260
        /// 
261
        /// Flushes the GZipStream.
262
        /// 
263
        /// In this implementation, this method does nothing. This is because excessive
264
        /// flushing may degrade the achievable compression rates.
265
        public override void Flush()
266
        {
267
            // left empty on purpose
268
        }
269
 
270
        /// 
271
        /// Gets/sets the current position in the GZipStream. Not suppported.
272
        /// 
273
        /// In this implementation this property is not supported
274
        /// Always thrown
275
        public override long Position
276
        {
277
            get
278
            {
279
                throw new NotSupportedException();
280
            }
281
            set
282
            {
283
                throw new NotSupportedException();
284
            }
285
        }
286
 
287
        /// 
288
        /// Gets the size of the stream. Not suppported.
289
        /// 
290
        /// In this implementation this property is not supported
291
        /// Always thrown
292
        public override long Length
293
        {
294
            get
295
            {
296
                throw new NotSupportedException();
297
            }
298
        }
299
        #endregion
300
    }
301
}

powered by: WebSVN 2.1.0

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