This is the Deflater class. The deflater class compresses input with the deflate algorithm described in RFC 1951. It has several compression levels and three different strategies described below. This class is not thread safe. This is inherent in the API, due to the split of deflate and setInput. author of the original java version : Jochen Hoenicke
Пример #1
21
        public byte[] Compress(byte[] bytData, params int[] ratio)
        {
            int compRatio = 9;
            try
            {
                if (ratio[0] > 0)

                {
                    compRatio = ratio[0];
                }
            }
            catch
            {
                throw;
            }

            try
            {
                var ms = new MemoryStream();
                var defl = new Deflater(compRatio, false);
                Stream s = new DeflaterOutputStream(ms, defl);
                s.Write(bytData, 0, bytData.Length);
                s.Close();
                byte[] compressedData = ms.ToArray();
                 return compressedData;
            }
            catch
            {
                throw;

            }
        }
Пример #2
1
        public byte[] Compress(byte[] input)
        {
            // Create the compressor with highest level of compression
            Deflater compressor = new Deflater();
            compressor.SetLevel(Deflater.BEST_COMPRESSION);

            // Give the compressor the data to compress
            compressor.SetInput(input);
            compressor.Finish();

            /*
             * Create an expandable byte array to hold the compressed data.
             * You cannot use an array that's the same size as the orginal because
             * there is no guarantee that the compressed data will be smaller than
             * the uncompressed data.
             */
            MemoryStream bos = new MemoryStream(input.Length);

            // Compress the data
            byte[] buf = new byte[1024];
            while (!compressor.IsFinished)
            {
                int count = compressor.Deflate(buf);
                bos.Write(buf, 0, count);
            }

            // Get the compressed data
            return bos.ToArray();
        }
Пример #3
0
        public void Compress()
        {
            var deflater = new Deflater();
            byte[] packet = PacketData;
            deflater.SetInput(packet, 0, packet.Length);
            deflater.Finish();

            var compBuffer = new byte[1024];
            var ret = new List<byte>();

            while (!deflater.IsFinished)
            {
                try
                {
                    deflater.Deflate(compBuffer);
                    ret.AddRange(compBuffer);
                    Array.Clear(compBuffer, 0, compBuffer.Length);
                }
                catch (Exception ex)
                {
                    return;
                }
            }
            deflater.Reset();

            Seek((byte)_headerType, SeekOrigin.Begin);
            // Write the compressed bytes over whatever is there.
            Write(ret.ToArray());
            // Set the stream length to the end of the actual packet data.
            // This makes sure we don't have any 'junk' packets at the end.
            OutStream.SetLength(BaseStream.Position);
        }
Пример #4
0
 public ChunkDataPacket()
 {
     if (zLibDeflater == null)
         zLibDeflater = new Deflater(CompressionLevel);
     if (LockObject == null)
         LockObject = new object();
 }
        /// <summary>Compresses the specified byte range using the
        /// specified compressionLevel (constants are defined in
        /// java.util.zip.Deflater). 
        /// </summary>
        public static byte[] Compress(byte[] value_Renamed, int offset, int length, int compressionLevel)
        {
            /* Create an expandable byte array to hold the compressed data.
            * You cannot use an array that's the same size as the orginal because
            * there is no guarantee that the compressed data will be smaller than
            * the uncompressed data. */
            System.IO.MemoryStream bos = new System.IO.MemoryStream(length);

            Deflater compressor = new Deflater();

            try
            {
                compressor.SetLevel(compressionLevel);
                compressor.SetInput(value_Renamed, offset, length);
                compressor.Finish();

                // Compress the data
                byte[] buf = new byte[1024];
                while (!compressor.IsFinished)
                {
                    int count = compressor.Deflate(buf);
                    bos.Write(buf, 0, count);
                }
            }
            finally
            {
            }

            return bos.ToArray();
        }
Пример #6
0
		public static void IOCompress(byte[] input, int len, byte[] output, out int deflatedLength)
		{
			Deflater item = new Deflater();
			item.SetInput(input, 0, len);
			item.Finish();
			deflatedLength = item.Deflate(output, 0, output.Length);
		}
Пример #7
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public byte[] Compress(Stream input)
        {
            String s = String.Format("Compressing output with level {0:d}", CompressionLevel);
            //Log.Debug(this, s);

            Deflater deflater = new Deflater(CompressionLevel, false);
            byte[] uncompressedData = new byte[(Int32)input.Length];
            input.Seek(0, SeekOrigin.Begin);
            input.Read(uncompressedData, 0, uncompressedData.Length);
            byte[] buffer = new byte[input.Length];

            try
            {
                deflater.SetInput(uncompressedData);
                deflater.Finish();
                int bytesDeflated = deflater.Deflate(buffer, 0, buffer.Length);

                byte[] compressedData = new byte[bytesDeflated];
                Array.Copy(buffer, compressedData, bytesDeflated);

                //Log.Debug(this, "Compression completed.");

                return compressedData;
            }
            catch (Exception e)
            {
                Log.Error(this, e);
                throw e;
            }
        }
Пример #8
0
 public static byte[] Compress(string str)
 {
     byte[] bytes = Encoding.Unicode.GetBytes(str);
     Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
     deflater.SetInput(bytes);
     deflater.Finish();
     MemoryStream stream = new MemoryStream(bytes.Length);
     try
     {
         byte[] output = new byte[0x400];
         while (!deflater.IsFinished)
         {
             int count = deflater.Deflate(output);
             stream.Write(output, 0, count);
         }
     }
     finally
     {
         stream.Close();
     }
     byte[] buffer3 = stream.ToArray();
     if ((buffer3.Length % 2) == 0)
     {
         return buffer3;
     }
     byte[] buffer4 = new byte[buffer3.Length + 1];
     for (int i = 0; i < buffer3.Length; i++)
     {
         buffer4[i] = buffer3[i];
     }
     buffer4[buffer3.Length] = 0;
     return buffer4;
 }
Пример #9
0
 ///	<summary>
 /// Construct an object writer for the specified repository.
 /// </summary>
 ///	<param name="repo"> </param>
 public ObjectWriter(Repository repo)
 {
     _r = repo;
     _buf = new byte[0x2000];
     _md = new MessageDigest();
     _def = new Deflater(_r.Config.getCore().getCompression());
 }
Пример #10
0
 public ObjectWriter(Repository repo)
 {
     this.r = repo;
     buf = new byte[8192];
     md = new MessageDigest(); // [henon] Sha1 hash digest generator
     def = new Deflater(r.Config.Core.Compression);
 }
Пример #11
0
        private const int CopyBufferSize = 32*1024;    // 32kb

        public void Compress(Stream source, Stream destination)
        {
            /*
            var deflater = new DeflaterOutputStream(destination, new Deflater(Deflater.DEFAULT_COMPRESSION, true));

            var dataBuffer = new byte[CopyBufferSize];
            StreamUtils.Copy(source, deflater, dataBuffer);
             */

            var def = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
            
            var inputData = new byte[source.Length - source.Position];
            source.Read(inputData, 0, inputData.Length);

            var buffer = new byte[CopyBufferSize];

            def.SetInput( inputData, 0, inputData.Length );
            def.Finish();

            while(!def.IsFinished)
            {
                int outputLen = def.Deflate(buffer, 0, buffer.Length);
                destination.Write( buffer, 0, outputLen );
            }

            def.Reset();
        }
Пример #12
0
        public void TestInflateDeflate()
        {
            MemoryStream ms = new MemoryStream();
            Deflater deflater = new Deflater(6);
            DeflaterOutputStream outStream = new DeflaterOutputStream(ms, deflater);

            byte[] buf = new byte[1000000];
            System.Random rnd = new Random();
            rnd.NextBytes(buf);

            outStream.Write(buf, 0, buf.Length);
            outStream.Flush();
            outStream.Finish();

            ms.Seek(0, SeekOrigin.Begin);

            InflaterInputStream inStream = new InflaterInputStream(ms);
            byte[] buf2 = new byte[buf.Length];
            int    pos  = 0;
            while (true) {
                int numRead = inStream.Read(buf2, pos, 4096);
                if (numRead <= 0) {
                    break;
                }
                pos += numRead;
            }

            for (int i = 0; i < buf.Length; ++i) {
                Assertion.AssertEquals(buf2[i], buf[i]);
            }
        }
Пример #13
0
 public ZlibStream(Stream inner, Inflater inflater, int buffSize)
 {
     _innerStream = inner;
     _in = inflater;
     _inBuff = new byte[buffSize];
     _outBuff = _inBuff;
     _out = new Deflater();
 }
Пример #14
0
		/// <summary>
		/// Performs deflate compression on the given data.
		/// </summary>
		/// <param name="input">the data to compress</param>
		/// <param name="output">the compressed data</param>
		public static void CompressZLib(byte[] input, byte[] output, int compressionLevel, out int deflatedLength)
		{
			Deflater item = new Deflater(compressionLevel);

			item.SetInput(input, 0, input.Length);
			item.Finish();
			deflatedLength = item.Deflate(output, 0, output.Length);
		}
Пример #15
0
        /// <summary>
        /// Compress an array of bytes.
        /// </summary>
        /// <param name="_pBytes">An array of bytes to be compressed.</param>
        /// <returns>Compressed bytes.</returns>
        /// <example>
        /// Following example demonstrates the way of compressing an ASCII string text.
        /// <code>
        /// public void Compress()
        /// {
        ///     string source = "Hello, world!";
        ///     byte[] source_bytes = System.Text.Encoding.ASCII.GetBytes(source);
        ///     byte[] compressed = DataCompression.Compress(source_bytes);
        ///     
        ///     // Process the compressed bytes here.
        /// }
        /// </code>
        /// </example>
        /// <remarks>It is the best practice that use the overrided <b>DataCompression.Compress</b> method with <see cref="System.String"/> parameter to compress a string.</remarks>
        public static byte[] Compress(byte[] _pBytes)
        {
            MemoryStream ms = new MemoryStream();

            Deflater mDeflater = new Deflater(Deflater.BEST_COMPRESSION);
            DeflaterOutputStream outputStream = new DeflaterOutputStream(ms, mDeflater, 131072);

            outputStream.Write(_pBytes, 0, _pBytes.Length);
            outputStream.Close();

            return ms.ToArray();
        }
Пример #16
0
 private ICSharpCode.SharpZipLib.Zip.Compression.Deflater Deflater()
 {
     if (deflater == null)
     {
         deflater = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(config.GetCompressionLevel
                                                                             ());
     }
     else
     {
         deflater.Reset();
     }
     return(deflater);
 }
		MemoryStream Deflate(byte[] data, int level, bool zlib)
		{
			MemoryStream memoryStream = new MemoryStream();
			
			Deflater deflater = new Deflater(level, !zlib);
			using ( DeflaterOutputStream outStream = new DeflaterOutputStream(memoryStream, deflater) )
			{
				outStream.IsStreamOwner = false;
				outStream.Write(data, 0, data.Length);
				outStream.Flush();
				outStream.Finish();
			}
			return memoryStream;
		}
Пример #18
0
        public static void Serialize(Stream stream, PiaFile piaFile)
        {
            if (stream == null)
                throw new ArgumentNullException("Stream");

            if (piaFile == null)
                throw new ArgumentNullException("PiaFile");

            try
            {
                // Header
                var headerString = piaFile.Header.ToString();
                var headerBytes = Encoding.Default.GetBytes(headerString);
                stream.Write(headerBytes, 0, headerBytes.Length);

                // Nodes
                var nodeString = _serializeNode(piaFile);
                var nodeBytes = Encoding.Default.GetBytes(nodeString);

                // Deflation
                byte[] deflatedBytes;
                var deflater = new Deflater(Deflater.DEFAULT_COMPRESSION);
                using (var ms = new MemoryStream())
                {
                    var deflateStream = new DeflaterOutputStream(ms, deflater);
                    deflateStream.Write(nodeBytes, 0, nodeBytes.Length);
                    deflateStream.Finish();

                    deflatedBytes = ms.ToArray();
                }

                // Checksum
                var checkSum = new byte[12];
                BitConverter.GetBytes(deflater.Adler).CopyTo(checkSum, 0); // Adler
                BitConverter.GetBytes(nodeBytes.Length).CopyTo(checkSum, 4); // InflatedSize
                BitConverter.GetBytes(deflatedBytes.Length).CopyTo(checkSum, 8); // DeflatedSize
                stream.Write(checkSum, 0, checkSum.Length);

                // Final write
                stream.Write(deflatedBytes, 0, deflatedBytes.Length);
            }
            catch (Exception)
            {
                throw;
            }
        }
Пример #19
0
        public byte[] Compress(byte[] bytData)
        {
            try
            {
                var ms = new MemoryStream();
                var defl = new Deflater(9, false);
                Stream s = new DeflaterOutputStream(ms, defl);
                s.Write(bytData, 0, bytData.Length);
                s.Close();
                byte[] compressedData = ms.ToArray();
                return compressedData;
            }
            catch
            {

                throw;
            }
        }
Пример #20
0
		/// <summary>
		/// Sends packet (might be compressed)
		/// </summary>
		/// <returns></returns>
		public void SendTo(IRealmClient client)
		{
			if (TotalLength <= WCellDef.MAX_UNCOMPRESSED_UPDATE_PACKET)
			{
				client.Send(GetFinalizedPacket());
			}
			else
			{
				var segment = ((SegmentStream)BaseStream).Segment;
				//var input = ((MemoryStream)BaseStream).ToArray();
				var inputOffset = HeaderSize;
				//Compression.CompressZLib(packetBuffer, outputBuffer, RealmServer.Instance.Configuration.CompressionLevel, out deflatedLength);

				var length = ContentLength;
				if (length > 0x7FFF)
				{
					log.Warn("Sent UpdatePacket with Length {0} to {1} in {2}", length, client,
						client.ActiveCharacter.Zone as IWorldSpace ?? client.ActiveCharacter.Region);
				}

				var maxOutputLength = length + FullUpdatePacketHeaderSize;

				var outSegment = BufferManager.GetSegment(maxOutputLength);

				var deflater = new Deflater(RealmServerConfiguration.CompressionLevel);
				deflater.SetInput(segment.Buffer.Array, segment.Offset + inputOffset, length);
				//deflater.SetInput(input, 0 + inputOffset, length);
				deflater.Finish();
				int deflatedLength = deflater.Deflate(outSegment.Buffer.Array,
					outSegment.Offset + FullUpdatePacketHeaderSize, length);

				var totalLength = deflatedLength + FullUpdatePacketHeaderSize;

				if (totalLength > MaxPacketSize)
				{
					//TODO: Split up packet if packet size exceeds max length
					throw new Exception("Compressed Update packet exceeded max length: " + totalLength);
				}
				SendPacket(client, outSegment, totalLength, length);

				outSegment.DecrementUsage();
			}
		}
Пример #21
0
        public static byte[] Compress(byte[] input, int level)
        {
            byte[] bytesOut;
            byte[] temp = new byte[input.Length];
            Deflater deflater = new Deflater(level, true);
            try
            {
                deflater.SetInput(input, 0, input.Length);
                deflater.Finish();
                bytesOut = new byte[deflater.Deflate(temp)];
            }
            catch (Exception e)
            {
                throw e;
            }

            Array.Copy(temp, 0, bytesOut, 0, bytesOut.Length);

            return bytesOut;
        }
Пример #22
0
		public static byte[] Compress(byte[] content)
		{
			//return content;
			Deflater compressor = new Deflater();
			compressor.SetLevel(Deflater.BEST_COMPRESSION);

			compressor.SetInput(content);
			compressor.Finish();

			using (MemoryStream bos = new MemoryStream(content.Length))
			{
				var buf = new byte[1024];
				while (!compressor.IsFinished)
				{
					int n = compressor.Deflate(buf);
					bos.Write(buf, 0, n);
				}
				return bos.ToArray();
			}
		}
Пример #23
0
 /// <summary>
 /// Write out bytes to the underlying stream after compressing them using deflate
 /// </summary>
 /// <param name="buffer">The array of bytes to write</param>
 /// <param name="offset">The offset into the supplied buffer to start</param>
 /// <param name="count">The number of bytes to write</param>
 public override void Write(byte[] buffer, int offset, int count) {
   
   if (m_stream == null) {
     Deflater deflater;
   
     switch(CompressionLevel) {
       case CompressionLevels.High:
         deflater = new Deflater(Deflater.BEST_COMPRESSION, true);
         break;
       case CompressionLevels.Low:
         deflater = new Deflater(Deflater.BEST_SPEED, true);
         break;
       case CompressionLevels.Normal:
       default:
         deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
         break;
     }
     m_stream = new DeflaterOutputStream(BaseStream, deflater);
   }
   m_stream.Write(buffer, offset, count);
 }
        public static int Deflate(byte[] inBuffer, ref byte[] outBuffer)
        {
            int newLen = 0, flagOffset = 1;

            outBuffer[0] = inBuffer[0];
            if (inBuffer[0] == 0)
            {
                flagOffset = 2;
                outBuffer[1] = inBuffer[1];
            }

            if (inBuffer.Length > 30)
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, false);
                    using (DeflaterOutputStream outStream = new DeflaterOutputStream(ms, deflater))
                    {
                        outStream.IsStreamOwner = false;
                        outStream.Write(inBuffer, flagOffset, inBuffer.Length - flagOffset);
                        outStream.Flush();
                        outStream.Finish();
                    }

                    ms.Position = 0;
                    ms.Read(outBuffer, flagOffset + 1, (int)ms.Length);
                    newLen = (int)ms.Length + flagOffset + 1;
                    outBuffer[flagOffset] = 0x5a;
                }
            }
            else
            {
                Buffer.BlockCopy(inBuffer, flagOffset, outBuffer, flagOffset + 1, inBuffer.Length - flagOffset);
                outBuffer[flagOffset] = 0xa5;
                newLen = inBuffer.Length + 1;
            }

            return newLen;
        }
		public DeflaterOutputStream(Stream baseOutputStream, Deflater deflater, int bufferSize)
		{
			if ( baseOutputStream == null ) {
				throw new ArgumentNullException("baseOutputStream");
			}

			if (baseOutputStream.CanWrite == false) {
				throw new ArgumentException("Must support writing", "baseOutputStream");
			}

			if (deflater == null) {
				throw new ArgumentNullException("deflater");
			}
			
			if (bufferSize <= 0) {
				throw new ArgumentOutOfRangeException("bufferSize");
			}
			
			baseOutputStream_ = baseOutputStream;
			buffer_ = new byte[bufferSize];
			deflater_ = deflater;
		}
Пример #26
0
 /// <summary>
 /// ѹ�����ݣ����ѹ����ֵΪ-1��ʾ��������������ѹ������
 /// </summary>
 /// <param name="val">ԭʼ������</param>
 /// <returns>ѹ����������</returns>
 public byte[] Compress(byte[] val)
 {
     byte[] buf;
     if (overSize != -1 && val.Length > overSize) {
         using (MemoryStream destStream = new MemoryStream()) {
             Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
             using (DeflaterOutputStream compressStream = new DeflaterOutputStream(destStream, deflater)) {
                 compressStream.Write(val, 0, val.Length);
                 compressStream.Finish();
                 buf = new byte[destStream.Length + 1];
                 destStream.ToArray().CopyTo(buf, 1);
             }
             buf[0] = 1; // ��ѹ����־
             return buf;
         }
     }
     else {
         buf = new byte[val.Length + 1];
         val.CopyTo(buf, 1);
         buf[0] = 0; // δѹ����־
         return buf;
     }
 }
Пример #27
0
 public DeflaterOutputStream(Stream baseOutputStream, Deflater deflater, int bufferSize)
 {
     this.isStreamOwner_ = true;
     if (baseOutputStream == null)
     {
         throw new ArgumentNullException("baseOutputStream");
     }
     if (!baseOutputStream.CanWrite)
     {
         throw new ArgumentException("Must support writing", "baseOutputStream");
     }
     if (deflater == null)
     {
         throw new ArgumentNullException("deflater");
     }
     if (bufferSize < 0x200)
     {
         throw new ArgumentOutOfRangeException("bufferSize");
     }
     this.baseOutputStream_ = baseOutputStream;
     this.buffer_ = new byte[bufferSize];
     this.deflater_ = deflater;
 }
Пример #28
0
        public void TestInflateDeflate()
        {
            MemoryStream         ms        = new MemoryStream();
            Deflater             deflater  = new Deflater(6);
            DeflaterOutputStream outStream = new DeflaterOutputStream(ms, deflater);

            byte[]        buf = new byte[1000000];
            System.Random rnd = new Random();
            rnd.NextBytes(buf);

            outStream.Write(buf, 0, buf.Length);
            outStream.Flush();
            outStream.Finish();

            ms.Seek(0, SeekOrigin.Begin);

            InflaterInputStream inStream = new InflaterInputStream(ms);

            byte[] buf2 = new byte[buf.Length];
            int    pos  = 0;

            while (true)
            {
                int numRead = inStream.Read(buf2, pos, 4096);
                if (numRead <= 0)
                {
                    break;
                }
                pos += numRead;
            }

            for (int i = 0; i < buf.Length; ++i)
            {
                Assertion.AssertEquals(buf2[i], buf[i]);
            }
        }
Пример #29
0
 /// <exception cref="System.IO.IOException"></exception>
 private void Deflate(TemporaryBuffer.Heap tinyPack, byte[] content)
 {
     Deflater deflater = new Deflater();
     byte[] buf = new byte[128];
     deflater.SetInput(content, 0, content.Length);
     deflater.Finish();
     do
     {
         int n = deflater.Deflate(buf, 0, buf.Length);
         if (n > 0)
         {
             tinyPack.Write(buf, 0, n);
         }
     }
     while (!deflater.IsFinished);
 }
Пример #30
0
 internal ZipStream(Deflater deflater, byte[] zbuf)
 {
     this.deflater = deflater;
     this.zbuf = zbuf;
 }
Пример #31
0
		/// <summary>
		/// Creates a new DeflaterOutputStream with the given Deflater and
		/// default buffer size.
		/// </summary>
		/// <param name="baseOutputStream">
		/// the output stream where deflated output should be written.
		/// </param>
		/// <param name="defl">
		/// the underlying deflater.
		/// </param>
		public DeflaterOutputStream(Stream baseOutputStream, Deflater defl) : this(baseOutputStream, defl, 512)
		{
		}
Пример #32
0
 private ICSharpCode.SharpZipLib.Zip.Compression.Deflater Deflater()
 {
     if (deflater == null)
     {
         deflater = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(config.GetCompressionLevel
             ());
     }
     else
     {
         deflater.Reset();
     }
     return deflater;
 }