The Labs \ Source Viewer \ SSCLI \ System.IO.Compression \ GZipStream

  1. //------------------------------------------------------------------------------
  2. // <copyright file="GZipStream.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace System.IO.Compression
  16. {
  17.     using System.IO;
  18.     using System.Diagnostics;
  19.     using System.Security.Permissions;
  20.    
  21.     public class GZipStream : Stream
  22.     {
  23.         private DeflateStream deflateStream;
  24.        
  25.         public GZipStream(Stream stream, CompressionMode mode) : this(stream, mode, false)
  26.         {
  27.         }
  28.        
  29.         public GZipStream(Stream stream, CompressionMode mode, bool leaveOpen)
  30.         {
  31.             deflateStream = new DeflateStream(stream, mode, leaveOpen, true);
  32.         }
  33.        
  34.         public override bool CanRead {
  35.             get {
  36.                 if (deflateStream == null) {
  37.                     return false;
  38.                 }
  39.                
  40.                 return deflateStream.CanRead;
  41.             }
  42.         }
  43.        
  44.         public override bool CanWrite {
  45.             get {
  46.                 if (deflateStream == null) {
  47.                     return false;
  48.                 }
  49.                
  50.                 return deflateStream.CanWrite;
  51.             }
  52.         }
  53.        
  54.         public override bool CanSeek {
  55.             get {
  56.                 if (deflateStream == null) {
  57.                     return false;
  58.                 }
  59.                
  60.                 return deflateStream.CanSeek;
  61.             }
  62.         }
  63.        
  64.         public override long Length {
  65.             get {
  66.                 throw new NotSupportedException(SR.GetString(SR.NotSupported));
  67.             }
  68.         }
  69.        
  70.         public override long Position {
  71.             get {
  72.                 throw new NotSupportedException(SR.GetString(SR.NotSupported));
  73.             }
  74.            
  75.             set {
  76.                 throw new NotSupportedException(SR.GetString(SR.NotSupported));
  77.             }
  78.         }
  79.        
  80.         public override void Flush()
  81.         {
  82.             if (deflateStream == null) {
  83.                 throw new ObjectDisposedException(null, SR.GetString(SR.ObjectDisposed_StreamClosed));
  84.             }
  85.             deflateStream.Flush();
  86.             return;
  87.         }
  88.        
  89.         public override long Seek(long offset, SeekOrigin origin)
  90.         {
  91.             throw new NotSupportedException(SR.GetString(SR.NotSupported));
  92.         }
  93.        
  94.         public override void SetLength(long value)
  95.         {
  96.             throw new NotSupportedException(SR.GetString(SR.NotSupported));
  97.         }
  98.        
  99.         [HostProtection(ExternalThreading = true)]
  100.         public override IAsyncResult BeginRead(byte[] array, int offset, int count, AsyncCallback asyncCallback, object asyncState)
  101.         {
  102.             if (deflateStream == null) {
  103.                 throw new InvalidOperationException(SR.GetString(SR.ObjectDisposed_StreamClosed));
  104.             }
  105.             return deflateStream.BeginRead(array, offset, count, asyncCallback, asyncState);
  106.         }
  107.        
  108.         public override int EndRead(IAsyncResult asyncResult)
  109.         {
  110.             if (deflateStream == null) {
  111.                 throw new InvalidOperationException(SR.GetString(SR.ObjectDisposed_StreamClosed));
  112.             }
  113.             return deflateStream.EndRead(asyncResult);
  114.         }
  115.        
  116.         [HostProtection(ExternalThreading = true)]
  117.         public override IAsyncResult BeginWrite(byte[] array, int offset, int count, AsyncCallback asyncCallback, object asyncState)
  118.         {
  119.             if (deflateStream == null) {
  120.                 throw new InvalidOperationException(SR.GetString(SR.ObjectDisposed_StreamClosed));
  121.             }
  122.             return deflateStream.BeginWrite(array, offset, count, asyncCallback, asyncState);
  123.         }
  124.        
  125.         public override void EndWrite(IAsyncResult asyncResult)
  126.         {
  127.             if (deflateStream == null) {
  128.                 throw new InvalidOperationException(SR.GetString(SR.ObjectDisposed_StreamClosed));
  129.             }
  130.             deflateStream.EndWrite(asyncResult);
  131.         }
  132.         public override int Read(byte[] array, int offset, int count)
  133.         {
  134.             if (deflateStream == null) {
  135.                 throw new ObjectDisposedException(null, SR.GetString(SR.ObjectDisposed_StreamClosed));
  136.             }
  137.            
  138.             return deflateStream.Read(array, offset, count);
  139.         }
  140.        
  141.         public override void Write(byte[] array, int offset, int count)
  142.         {
  143.             if (deflateStream == null) {
  144.                 throw new ObjectDisposedException(null, SR.GetString(SR.ObjectDisposed_StreamClosed));
  145.             }
  146.            
  147.             deflateStream.Write(array, offset, count);
  148.         }
  149.        
  150.         protected override void Dispose(bool disposing)
  151.         {
  152.             try {
  153.                 if (disposing && deflateStream != null) {
  154.                     deflateStream.Close();
  155.                 }
  156.                 deflateStream = null;
  157.             }
  158.             finally {
  159.                 base.Dispose(disposing);
  160.             }
  161.         }
  162.        
  163.         public Stream BaseStream {
  164.             get {
  165.                 if (deflateStream != null) {
  166.                     return deflateStream.BaseStream;
  167.                 }
  168.                 else {
  169.                     return null;
  170.                 }
  171.             }
  172.         }
  173.     }
  174.    
  175. }

Developer Fusion