介绍开源的.net通信框架NetworkComms框架 源码分析(八)SharpZipLibGzipCompressor

时间:2022-08-17 03:46:34

原文网址: http://www.cnblogs.com/csdev

Networkcomms 是一款C# 语言编写的TCP/UDP通信框架  作者是英国人  以前是收费的 目前作者已经开源  许可是:Apache License v2

开源地址是:https://github.com/MarcFletcher/NetworkComms.Net

SharpZipLibGzipCompressor  是7Zip数据加密器

数据加密会加大Cpu负载,很多时候并不启用。

namespace SharpZipLibCompressor
{
    /// <summary>
    /// Compresor using Gzip compression from <see href="http://www.icsharpcode.net/opensource/sharpziplib/">SharpZipLib</see>
    /// </summary>
    [DataSerializerProcessor()]
    public class SharpZipLibGzipCompressor : DataProcessor
    {
        static DataProcessor instance;

        /// <summary>
        /// Instance singleton used to access <see cref="NetworkCommsDotNet.DPSBase.DataProcessor"/> instance.  Obsolete, use instead <see cref="NetworkCommsDotNet.DPSBase.DPSManager.GetDataProcessor{T}"/>
        /// </summary>
        [Obsolete("Instance access via class obsolete, use DPSManager.GetDataProcessor<T>")]
        public static DataProcessor Instance
        {
            get
            {
                if (instance == null)
                    instance = GetInstance<SharpZipLibGzipCompressor>();

                return instance;
            }
        }

#if ANDROID || iOS
        [Preserve]
#endif
        private SharpZipLibGzipCompressor() { }

        /// <inheritdoc />
        public override void ForwardProcessDataStream(Stream inStream, Stream outStream, Dictionary<string, string> options, out long writtenBytes)
        {
            using (GZipOutputStream gzStream = new GZipOutputStream(outStream))
            {
                gzStream.IsStreamOwner = false;
                ];

                while (true)
                {
                    , buffer.Length);

                    )
                        break;

                    gzStream.Write(buffer, , readCount);
                }
            }

            writtenBytes = outStream.Position;
        }

        /// <inheritdoc />
        public override void ReverseProcessDataStream(Stream inStream, Stream outStream, Dictionary<string, string> options, out long writtenBytes)
        {
            using (GZipInputStream zip = new GZipInputStream(inStream))
            {
                zip.IsStreamOwner = false;
                ];

                while (true)
                {
                    , buffer.Length);

                    )
                        break;

                    outStream.Write(buffer, , readCount);
                }
            }

            writtenBytes = outStream.Position;
        }
    }
}