2 // © Copyright Henrik Ravn 2004
\r
4 // Use, modification and distribution are subject to the Boost Software License, Version 1.0.
\r
5 // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
\r
10 using System.Runtime.InteropServices;
\r
15 /// Implements a compressed <see cref="Stream"/>, in GZip (.gz) format.
\r
17 public class GZipStream : Stream, IDisposable
\r
20 [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl, CharSet=CharSet.Ansi)]
\r
21 private static extern IntPtr gzopen(string name, string mode);
\r
23 [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
\r
24 private static extern int gzclose(IntPtr gzFile);
\r
26 [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
\r
27 private static extern int gzwrite(IntPtr gzFile, int data, int length);
\r
29 [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
\r
30 private static extern int gzread(IntPtr gzFile, int data, int length);
\r
32 [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
\r
33 private static extern int gzgetc(IntPtr gzFile);
\r
35 [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
\r
36 private static extern int gzputc(IntPtr gzFile, int c);
\r
40 #region Private data
\r
41 private IntPtr _gzFile;
\r
42 private bool _isDisposed = false;
\r
43 private bool _isWriting;
\r
46 #region Constructors
\r
48 /// Creates a new file as a writeable GZipStream
\r
50 /// <param name="fileName">The name of the compressed file to create</param>
\r
51 /// <param name="level">The compression level to use when adding data</param>
\r
52 /// <exception cref="ZLibException">If an error occurred in the internal zlib function</exception>
\r
53 public GZipStream(string fileName, CompressLevel level)
\r
56 _gzFile = gzopen(fileName, String.Format("wb{0}", (int)level));
\r
57 if (_gzFile == IntPtr.Zero)
\r
58 throw new ZLibException(-1, "Could not open " + fileName);
\r
62 /// Opens an existing file as a readable GZipStream
\r
64 /// <param name="fileName">The name of the file to open</param>
\r
65 /// <exception cref="ZLibException">If an error occurred in the internal zlib function</exception>
\r
66 public GZipStream(string fileName)
\r
69 _gzFile = gzopen(fileName, "rb");
\r
70 if (_gzFile == IntPtr.Zero)
\r
71 throw new ZLibException(-1, "Could not open " + fileName);
\r
76 #region Access properties
\r
78 /// Returns true of this stream can be read from, false otherwise
\r
80 public override bool CanRead
\r
92 public override bool CanSeek
\r
101 /// Returns true if this tsream is writeable, false otherwise
\r
103 public override bool CanWrite
\r
112 #region Destructor & IDispose stuff
\r
115 /// Destroys this instance
\r
123 /// Closes the external file handle
\r
125 public void Dispose()
\r
130 // Does the actual closing of the file handle.
\r
131 private void cleanUp(bool isDisposing)
\r
136 _isDisposed = true;
\r
141 #region Basic reading and writing
\r
143 /// Attempts to read a number of bytes from the stream.
\r
145 /// <param name="buffer">The destination data buffer</param>
\r
146 /// <param name="offset">The index of the first destination byte in <c>buffer</c></param>
\r
147 /// <param name="count">The number of bytes requested</param>
\r
148 /// <returns>The number of bytes read</returns>
\r
149 /// <exception cref="ArgumentNullException">If <c>buffer</c> is null</exception>
\r
150 /// <exception cref="ArgumentOutOfRangeException">If <c>count</c> or <c>offset</c> are negative</exception>
\r
151 /// <exception cref="ArgumentException">If <c>offset</c> + <c>count</c> is > buffer.Length</exception>
\r
152 /// <exception cref="NotSupportedException">If this stream is not readable.</exception>
\r
153 /// <exception cref="ObjectDisposedException">If this stream has been disposed.</exception>
\r
154 public override int Read(byte[] buffer, int offset, int count)
\r
156 if (!CanRead) throw new NotSupportedException();
\r
157 if (buffer == null) throw new ArgumentNullException();
\r
158 if (offset < 0 || count < 0) throw new ArgumentOutOfRangeException();
\r
159 if ((offset+count) > buffer.Length) throw new ArgumentException();
\r
160 if (_isDisposed) throw new ObjectDisposedException("GZipStream");
\r
162 GCHandle h = GCHandle.Alloc(buffer, GCHandleType.Pinned);
\r
166 result = gzread(_gzFile, h.AddrOfPinnedObject().ToInt32() + offset, count);
\r
168 throw new IOException();
\r
178 /// Attempts to read a single byte from the stream.
\r
180 /// <returns>The byte that was read, or -1 in case of error or End-Of-File</returns>
\r
181 public override int ReadByte()
\r
183 if (!CanRead) throw new NotSupportedException();
\r
184 if (_isDisposed) throw new ObjectDisposedException("GZipStream");
\r
185 return gzgetc(_gzFile);
\r
189 /// Writes a number of bytes to the stream
\r
191 /// <param name="buffer"></param>
\r
192 /// <param name="offset"></param>
\r
193 /// <param name="count"></param>
\r
194 /// <exception cref="ArgumentNullException">If <c>buffer</c> is null</exception>
\r
195 /// <exception cref="ArgumentOutOfRangeException">If <c>count</c> or <c>offset</c> are negative</exception>
\r
196 /// <exception cref="ArgumentException">If <c>offset</c> + <c>count</c> is > buffer.Length</exception>
\r
197 /// <exception cref="NotSupportedException">If this stream is not writeable.</exception>
\r
198 /// <exception cref="ObjectDisposedException">If this stream has been disposed.</exception>
\r
199 public override void Write(byte[] buffer, int offset, int count)
\r
201 if (!CanWrite) throw new NotSupportedException();
\r
202 if (buffer == null) throw new ArgumentNullException();
\r
203 if (offset < 0 || count < 0) throw new ArgumentOutOfRangeException();
\r
204 if ((offset+count) > buffer.Length) throw new ArgumentException();
\r
205 if (_isDisposed) throw new ObjectDisposedException("GZipStream");
\r
207 GCHandle h = GCHandle.Alloc(buffer, GCHandleType.Pinned);
\r
210 int result = gzwrite(_gzFile, h.AddrOfPinnedObject().ToInt32() + offset, count);
\r
212 throw new IOException();
\r
221 /// Writes a single byte to the stream
\r
223 /// <param name="value">The byte to add to the stream.</param>
\r
224 /// <exception cref="NotSupportedException">If this stream is not writeable.</exception>
\r
225 /// <exception cref="ObjectDisposedException">If this stream has been disposed.</exception>
\r
226 public override void WriteByte(byte value)
\r
228 if (!CanWrite) throw new NotSupportedException();
\r
229 if (_isDisposed) throw new ObjectDisposedException("GZipStream");
\r
231 int result = gzputc(_gzFile, (int)value);
\r
233 throw new IOException();
\r
237 #region Position & length stuff
\r
241 /// <param name="value"></param>
\r
242 /// <exception cref="NotSupportedException">Always thrown</exception>
\r
243 public override void SetLength(long value)
\r
245 throw new NotSupportedException();
\r
249 /// Not suppported.
\r
251 /// <param name="offset"></param>
\r
252 /// <param name="origin"></param>
\r
253 /// <returns></returns>
\r
254 /// <exception cref="NotSupportedException">Always thrown</exception>
\r
255 public override long Seek(long offset, SeekOrigin origin)
\r
257 throw new NotSupportedException();
\r
261 /// Flushes the <c>GZipStream</c>.
\r
263 /// <remarks>In this implementation, this method does nothing. This is because excessive
\r
264 /// flushing may degrade the achievable compression rates.</remarks>
\r
265 public override void Flush()
\r
267 // left empty on purpose
\r
271 /// Gets/sets the current position in the <c>GZipStream</c>. Not suppported.
\r
273 /// <remarks>In this implementation this property is not supported</remarks>
\r
274 /// <exception cref="NotSupportedException">Always thrown</exception>
\r
275 public override long Position
\r
279 throw new NotSupportedException();
\r
283 throw new NotSupportedException();
\r
288 /// Gets the size of the stream. Not suppported.
\r
290 /// <remarks>In this implementation this property is not supported</remarks>
\r
291 /// <exception cref="NotSupportedException">Always thrown</exception>
\r
292 public override long Length
\r
296 throw new NotSupportedException();
\r