Summary

Class:ICSharpCode.SharpZipLib.GZip.GZipOutputStream
Assembly:ICSharpCode.SharpZipLib
File(s):C:\Users\Neil\Documents\Visual Studio 2015\Projects\icsharpcode\SZL_master\ICSharpCode.SharpZipLib\GZip\GzipOutputStream.cs
Covered lines:63
Uncovered lines:5
Coverable lines:68
Total lines:227
Line coverage:92.6%
Branch coverage:81.2%

Metrics

MethodCyclomatic ComplexitySequence CoverageBranch Coverage
.ctor(...)1100100
.ctor(...)1100100
Finalize()1100100
SetLevel(...)200
GetLevel()100
Write(...)3100100
Close()3100100
Finish()3100100
WriteHeader()210066.67

File(s)

C:\Users\Neil\Documents\Visual Studio 2015\Projects\icsharpcode\SZL_master\ICSharpCode.SharpZipLib\GZip\GzipOutputStream.cs

#LineLine coverage
 1using System;
 2using System.IO;
 3using ICSharpCode.SharpZipLib.Checksum;
 4using ICSharpCode.SharpZipLib.Zip.Compression;
 5using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
 6
 7namespace ICSharpCode.SharpZipLib.GZip
 8{
 9  /// <summary>
 10  /// This filter stream is used to compress a stream into a "GZIP" stream.
 11  /// The "GZIP" format is described in RFC 1952.
 12  ///
 13  /// author of the original java version : John Leuner
 14  /// </summary>
 15  /// <example> This sample shows how to gzip a file
 16  /// <code>
 17  /// using System;
 18  /// using System.IO;
 19  ///
 20  /// using ICSharpCode.SharpZipLib.GZip;
 21  /// using ICSharpCode.SharpZipLib.Core;
 22  ///
 23  /// class MainClass
 24  /// {
 25  ///   public static void Main(string[] args)
 26  ///   {
 27  ///       using (Stream s = new GZipOutputStream(File.Create(args[0] + ".gz")))
 28  ///       using (FileStream fs = File.OpenRead(args[0])) {
 29  ///         byte[] writeData = new byte[4096];
 30  ///         Streamutils.Copy(s, fs, writeData);
 31  ///       }
 32  ///     }
 33  ///   }
 34  /// }
 35  /// </code>
 36  /// </example>
 37  public class GZipOutputStream : DeflaterOutputStream
 38  {
 39    enum OutputState
 40    {
 41      Header,
 42      Footer,
 43      Finished,
 44      Closed,
 45    };
 46
 47    #region Instance Fields
 48    /// <summary>
 49    /// CRC-32 value for uncompressed data
 50    /// </summary>
 951    protected Crc32 crc = new Crc32();
 52    OutputState state_ = OutputState.Header;
 53    #endregion
 54
 55    #region Constructors
 56    /// <summary>
 57    /// Creates a GzipOutputStream with the default buffer size
 58    /// </summary>
 59    /// <param name="baseOutputStream">
 60    /// The stream to read data (to be compressed) from
 61    /// </param>
 62    public GZipOutputStream(Stream baseOutputStream)
 963      : this(baseOutputStream, 4096)
 64    {
 965    }
 66
 67    /// <summary>
 68    /// Creates a GZipOutputStream with the specified buffer size
 69    /// </summary>
 70    /// <param name="baseOutputStream">
 71    /// The stream to read data (to be compressed) from
 72    /// </param>
 73    /// <param name="size">
 74    /// Size of the buffer to use
 75    /// </param>
 976    public GZipOutputStream(Stream baseOutputStream, int size) : base(baseOutputStream, new Deflater(Deflater.DEFAULT_CO
 77    {
 978    }
 79    #endregion
 80
 81    #region Destructor
 82    /// <summary>
 83    /// Ensures that resources are freed and other cleanup operations
 84    /// are performed when the garbage collector reclaims the GZipOutputStream.
 85    /// </summary>
 86    ~GZipOutputStream()
 87    {
 988      Dispose(false);
 1889    }
 90    #endregion
 91
 92    #region Public API
 93    /// <summary>
 94    /// Sets the active compression level (1-9).  The new level will be activated
 95    /// immediately.
 96    /// </summary>
 97    /// <param name="level">The compression level to set.</param>
 98    /// <exception cref="ArgumentOutOfRangeException">
 99    /// Level specified is not supported.
 100    /// </exception>
 101    /// <see cref="Deflater"/>
 102    public void SetLevel(int level)
 103    {
 0104       if (level < Deflater.BEST_SPEED) {
 0105        throw new ArgumentOutOfRangeException(nameof(level));
 106      }
 0107      deflater_.SetLevel(level);
 0108    }
 109
 110    /// <summary>
 111    /// Get the current compression level.
 112    /// </summary>
 113    /// <returns>The current compression level.</returns>
 114    public int GetLevel()
 115    {
 0116      return deflater_.GetLevel();
 117    }
 118    #endregion
 119
 120    #region Stream overrides
 121    /// <summary>
 122    /// Write given buffer to output updating crc
 123    /// </summary>
 124    /// <param name="buffer">Buffer to write</param>
 125    /// <param name="offset">Offset of first byte in buf to write</param>
 126    /// <param name="count">Number of bytes to write</param>
 127    public override void Write(byte[] buffer, int offset, int count)
 128    {
 3129       if (state_ == OutputState.Header) {
 1130        WriteHeader();
 131      }
 132
 3133       if (state_ != OutputState.Footer) {
 2134        throw new InvalidOperationException("Write not permitted in current state");
 135      }
 136
 1137      crc.Update(buffer, offset, count);
 1138      base.Write(buffer, offset, count);
 1139    }
 140
 141    /// <summary>
 142    /// Writes remaining compressed output data to the output stream
 143    /// and closes it.
 144    /// </summary>
 145    public override void Close()
 146    {
 147      try {
 10148        Finish();
 10149      } finally {
 10150         if (state_ != OutputState.Closed) {
 8151          state_ = OutputState.Closed;
 8152           if (IsStreamOwner) {
 7153            baseOutputStream_.Close();
 154          }
 155        }
 10156      }
 10157    }
 158    #endregion
 159
 160    #region DeflaterOutputStream overrides
 161    /// <summary>
 162    /// Finish compression and write any footer information required to stream
 163    /// </summary>
 164    public override void Finish()
 165    {
 166      // If no data has been written a header should be added.
 13167       if (state_ == OutputState.Header) {
 8168        WriteHeader();
 169      }
 170
 13171       if (state_ == OutputState.Footer) {
 9172        state_ = OutputState.Finished;
 9173        base.Finish();
 174
 9175        var totalin = (uint)(deflater_.TotalIn & 0xffffffff);
 9176        var crcval = (uint)(crc.Value & 0xffffffff);
 177
 178        byte[] gzipFooter;
 179
 180        unchecked {
 9181          gzipFooter = new byte[] {
 9182          (byte) crcval, (byte) (crcval >> 8),
 9183          (byte) (crcval >> 16), (byte) (crcval >> 24),
 9184
 9185          (byte) totalin, (byte) (totalin >> 8),
 9186          (byte) (totalin >> 16), (byte) (totalin >> 24)
 9187        };
 188        }
 189
 9190        baseOutputStream_.Write(gzipFooter, 0, gzipFooter.Length);
 191      }
 13192    }
 193    #endregion
 194
 195    #region Support Routines
 196    void WriteHeader()
 197    {
 9198       if (state_ == OutputState.Header) {
 9199        state_ = OutputState.Footer;
 200
 9201        var mod_time = (int)((DateTime.Now.Ticks - new DateTime(1970, 1, 1).Ticks) / 10000000L);  // Ticks give back 100
 9202        byte[] gzipHeader = {
 9203          // The two magic bytes
 9204          (byte) (GZipConstants.GZIP_MAGIC >> 8), (byte) (GZipConstants.GZIP_MAGIC & 0xff),
 9205
 9206          // The compression type
 9207          (byte) Deflater.DEFLATED,
 9208
 9209          // The flags (not set)
 9210          0,
 9211
 9212          // The modification time
 9213          (byte) mod_time, (byte) (mod_time >> 8),
 9214          (byte) (mod_time >> 16), (byte) (mod_time >> 24),
 9215
 9216          // The extra flags
 9217          0,
 9218
 9219          // The OS type (unknown)
 9220          (byte) 255
 9221        };
 9222        baseOutputStream_.Write(gzipHeader, 0, gzipHeader.Length);
 223      }
 9224    }
 225    #endregion
 226  }
 227}