using System.Collections.Generic; using Renci.SshNet.Security; using System.IO; using System; namespace Renci.SshNet.Compression { /// /// Represents base class for compression algorithm implementation /// public abstract class Compressor : Algorithm, IDisposable { private readonly ZlibStream _compressor; private readonly ZlibStream _decompressor; private MemoryStream _compressorStream; private MemoryStream _decompressorStream; /// /// Gets or sets a value indicating whether compression is active. /// /// /// true if compression is active; otherwise, false. /// protected bool IsActive { get; set; } /// /// Gets the session. /// protected Session Session { get; private set; } /// /// Initializes a new instance of the class. /// public Compressor() { this._compressorStream = new MemoryStream(); this._decompressorStream = new MemoryStream(); this._compressor = new ZlibStream(this._compressorStream, CompressionMode.Compress); this._decompressor = new ZlibStream(this._decompressorStream, CompressionMode.Decompress); } /// /// Initializes the algorithm /// /// The session. public virtual void Init(Session session) { this.Session = session; } /// /// Compresses the specified data. /// /// Data to compress. /// Compressed data public virtual byte[] Compress(byte[] data) { if (!this.IsActive) { return data; } this._compressorStream.SetLength(0); this._compressor.Write(data, 0, data.Length); return this._compressorStream.ToArray(); } /// /// Decompresses the specified data. /// /// Compressed data. /// Decompressed data. public virtual byte[] Decompress(byte[] data) { if (!this.IsActive) { return data; } this._decompressorStream.SetLength(0); this._decompressor.Write(data, 0, data.Length); return this._decompressorStream.ToArray(); } #region IDisposable Members private bool _isDisposed = false; /// /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged ResourceMessages. /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } /// /// Releases unmanaged and - optionally - managed resources /// /// true to release both managed and unmanaged resources; false to release only unmanaged ResourceMessages. protected virtual void Dispose(bool disposing) { // Check to see if Dispose has already been called. if (!this._isDisposed) { // If disposing equals true, dispose all managed // and unmanaged ResourceMessages. if (disposing) { // Dispose managed ResourceMessages. if (this._compressorStream != null) { this._compressorStream.Dispose(); this._compressorStream = null; } if (this._decompressorStream != null) { this._decompressorStream.Dispose(); this._decompressorStream = null; } } // Note disposing has been done. this._isDisposed = true; } } /// /// Releases unmanaged resources and performs other cleanup operations before the /// is reclaimed by garbage collection. /// ~Compressor() { // Do not re-create Dispose clean-up code here. // Calling Dispose(false) is optimal in terms of // readability and maintainability. Dispose(false); } #endregion } }