SetInput() public method

Sets the data which should be compressed next. This should be only called when needsInput indicates that more input is needed. If you call setInput when needsInput() returns false, the previous input that is still pending will be thrown away. The given byte array should not be changed, before needsInput() returns true again. This call is equivalent to setInput(input, 0, input.length).
/// if the buffer was finished() or ended(). ///
public SetInput ( byte input ) : void
input byte /// the buffer containing the input data. ///
return void
Ejemplo n.º 1
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();
        }
Ejemplo n.º 2
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();
        }
Ejemplo n.º 3
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;
 }
Ejemplo n.º 4
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;
            }
        }
Ejemplo n.º 5
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);
		}
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
        /// <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();
        }
Ejemplo n.º 8
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);
		}
Ejemplo n.º 9
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();
			}
		}
Ejemplo n.º 10
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;
        }
Ejemplo n.º 11
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();
			}
		}
Ejemplo n.º 12
0
        private void WriteWhole(Deflater def, int typeCode, byte[] data)
        {
            int sz = data.Length;
            int hdrlen = 0;
            _buffer[hdrlen++] = (byte)((typeCode << 4) | sz & 15);
            sz = (int)(((uint)sz) >> 4);

            while (sz > 0)
            {
                _buffer[hdrlen - 1] |= 0x80;
                _buffer[hdrlen++] = (byte)(sz & 0x7f);
                sz = (int)(((uint)sz) >> 7);
            }

            _packDigest.Update(_buffer, 0, hdrlen);
            _crc.Update(_buffer, 0, hdrlen);
            _packOut.Write(_buffer, 0, hdrlen);
            def.Reset();
            def.SetInput(data);
            def.Finish();

            while (!def.IsFinished)
            {
                int datlen = def.Deflate(_buffer);
                _packDigest.Update(_buffer, 0, datlen);
                _crc.Update(_buffer, 0, datlen);
                _packOut.Write(_buffer, 0, datlen);
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// End compression.
        /// </summary>
        private void EndCompress()
        {
            // If no buffer exists, return.
            if (_buffer == null)
                return;

            // Input buffer.
            byte[] input = _buffer.ToArray();

            // Dump buffer
            _buffer.Clear();
            _buffer = null;

            // Create a new memory stream.
            using (MemoryStream ms = new MemoryStream())
            {
                Deflater compress = new Deflater();
                compress.SetInput(input);
                compress.Finish();

                // the result buffer
                byte[] result = new byte[input.Length];

                // Loop it just in case (shouldn't happen BUT, zlib's been known to cause a size increase)
                while (!compress.IsFinished)
                {
                    int length = compress.Deflate(result);
                    ms.Write(result, 0, length);
                }

                // Write the size of the compressed data, and the data.
                WriteInt((int)ms.Length);
                WriteBytes(ms.ToArray());
            }
        }
Ejemplo n.º 14
0
		/// <summary>
		/// Writes the MPK to a specific filename
		/// </summary>
		/// <param name="fname"></param>
		public void Write(string fname)
		{
			Deflater def;
			byte[] buf, dir, name;
			var files = new MPKFile[_files.Count];

			using (var dirmem = new MemoryStream(_files.Count*MPKFileHeader.MaxSize))
			{
				int index = 0;
				uint offset = 0;
				uint diroffset = 0;

				foreach (MPKFile file in _files.Values)
				{
					file.Header.DirectoryOffset = diroffset;
					file.Header.Offset = offset;
					files[index] = file;

					using (var wrtr = new BinaryWriter(dirmem, Encoding.UTF8))
					{
						file.Header.Write(wrtr);
					}

					offset += file.Header.UncompressedSize;
					diroffset += file.Header.CompressedSize;
					index++;
				}

				def = new Deflater();
				def.SetInput(dirmem.GetBuffer(), 0, (int) dirmem.Position);
				def.Finish();

				dir = new byte[dirmem.Position];
				def.Deflate(dir);
				_sizeDir = (int) def.TotalOut;
			}

			def = new Deflater();

			_crc.Reset();
			_crc.Update(dir, 0, _sizeDir);

			def.SetInput(Encoding.UTF8.GetBytes(_name));
			def.Finish();

			name = new byte[_name.Length];
			def.Deflate(name);

			_numFiles = _files.Count;
			_sizeName = (int) def.TotalOut;

			using (var filemem = new MemoryStream())
			{
				using (var wrtr = new BinaryWriter(filemem, Encoding.UTF8))
				{
					wrtr.Write((int) _crc.Value);
					wrtr.Write(_sizeDir);
					wrtr.Write(_sizeName);
					wrtr.Write(_numFiles);

					buf = new byte[16];
					Buffer.BlockCopy(filemem.GetBuffer(), 0, buf, 0, 16);

					for (byte i = 0; i < 16; i++)
					{
						buf[i] ^= i;
					}
				}
			}

			using (FileStream fileStream = File.Open(fname, FileMode.Create, FileAccess.Write))
			{
				using (var wrtr = new BinaryWriter(fileStream, Encoding.UTF8))
				{
					wrtr.Write(Magic);
					wrtr.Write((byte) 2);
					wrtr.Write(buf, 0, 16);
					wrtr.Write(name, 0, _sizeName);
					wrtr.Write(dir, 0, _sizeDir);

					foreach (MPKFile file in files)
					{
						wrtr.Write(file.CompressedData);
					}
				}
			}
		}
Ejemplo n.º 15
0
 //cualquiera de las dos funciones de comprimir causa un leak de memoria enorme
 public static int ComprimirBuffer(byte[] in_buffer,ref byte[] out_buffer)
 {
     out_buffer=new byte[in_buffer.Length+300];
     Deflater compresor=new Deflater();
     compresor.SetInput(in_buffer);
     compresor.Flush();
     compresor.Finish();
     int compressedsize=compresor.Deflate(out_buffer,0,(int)(in_buffer.Length)+300);
     compresor=null;
     return compressedsize;
 }
Ejemplo n.º 16
0
        // make string smaller (or try it at least ;)
        public string Deflate(string input)
        {
            byte[] inputData = Convert.FromBase64String(input);
            Deflater deflater = new Deflater(Deflater.BEST_SPEED, false);
            deflater.SetInput(inputData);
            deflater.Finish();

            using (var ms = new MemoryStream())
            {
                var outputBuffer = new byte[65536 * 4];
                while (deflater.IsNeedingInput == false)
                {
                    var read = deflater.Deflate(outputBuffer);
                    ms.Write(outputBuffer, 0, read);

                    if (deflater.IsFinished == true)
                        break;
                }

                deflater.Reset();

                return Convert.ToBase64String( ms.ToArray());
            }
        }
Ejemplo n.º 17
0
            public static byte[] Deflate(byte [] input, int offset, int length)
            {
                System.IO.MemoryStream output = new System.IO.MemoryStream ();
                Deflater deflater = new Deflater ();
                deflater.SetInput (input, offset, length);

                byte [] buf = new byte [1024];
                int deflate_length;
                while ((deflate_length = deflater.Deflate (buf)) > 0)
                    output.Write (buf, 0, deflate_length);

                output.Close ();
                return output.ToArray ();
            }
Ejemplo n.º 18
0
		/// <summary>
		/// Loads a MPK file
		/// </summary>
		/// <param name="fname">The filename to load</param>
		public void Load(string fname)
		{
			var fi = new FileInfo(fname);

			if (!fi.Exists)
			{
				throw new FileNotFoundException("File does not exist", fname);
			}

			using (FileStream file = fi.OpenRead())
			{
				_buf = new byte[fi.Length];
				file.Read(_buf, 0, _buf.Length);
			}

			_hdr = new MPKFileHeader { Name = fname, UncompressedSize = (uint)fi.Length, TimeStamp = (uint)DateTime.Now.ToFileTime() };

			var def = new Deflater();

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

			// create temporary buffer
			var tempbuffer = new byte[_buf.Length + _buf.Length / 5];
			_hdr.CompressedSize = (uint)def.Deflate(tempbuffer, 0, tempbuffer.Length);

			_compBuf = new byte[_hdr.CompressedSize];
			Buffer.BlockCopy(tempbuffer, 0, _compBuf, 0, (int)_hdr.CompressedSize);


			var crc = new Crc32();
			crc.Update(_compBuf, 0, (int)_hdr.CompressedSize);

			_hdr.CRC = crc;
		}
Ejemplo n.º 19
0
        /// <summary>
        /// Build a binary data frame form the frame object.
        /// </summary>
        /// <param name="frame">ID3 Frame</param>
        /// <returns>binary frame representation</returns>
        public byte[] Make(FrameBase frame, out ushort flags)
        {
            flags = GetFlags(frame);
            var buffer = frame.Make();

            var memoryStream = new MemoryStream();
            var writer = new BinaryWriter(memoryStream);

            if (frame.Group.HasValue == true)
                writer.Write((byte)frame.Group);

            if (frame.Compression == true)
            {
                switch (Version)
                {
                    case 3:
                        {
                            writer.Write(Swap.Int32(buffer.Length));
                            break;
                        }
                    case 4:
                        {
                            writer.Write(Sync.UnsafeBigEndian(Swap.UInt32((uint)buffer.Length)));
                            break;
                        }
                    default:
                        {
                            throw new NotImplementedException("ID3v2 Version " + Version + " is not supported.");
                        }
                }
                var buf = new byte[2048];
                var deflater = new Deflater(Deflater.BEST_COMPRESSION);
                deflater.SetInput(buffer, 0, buffer.Length);
                deflater.Finish();
                while (!deflater.IsNeedingInput)
                {
                    int len = deflater.Deflate(buf, 0, buf.Length);
                    if (len <= 0)
                    {
                        break;
                    }
                    memoryStream.Write(buf, 0, len);
                }

                if (!deflater.IsNeedingInput)
                {
                    //TODO: Skip and remove invalid frames.
                    throw new InvalidFrameException("Can't decompress frame '" + frame.FrameId + "' missing data");
                }
            }
            else
            {
                memoryStream.Write(buffer, 0, buffer.Length);
            }

            if (frame.Encryption == true)
            {
                //TODO: Encryption
                throw new NotImplementedException("Encryption is not implemented, consequently it is not supported.");
            }

            if (frame.Unsynchronisation == true)
            {
                MemoryStream synchStream = new MemoryStream();
                Sync.Unsafe(memoryStream, synchStream, (uint)memoryStream.Position);
                memoryStream = synchStream;
            }
            return memoryStream.ToArray();
        }
Ejemplo n.º 20
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);
 }
Ejemplo n.º 21
0
		/// <exception cref="System.IO.IOException"></exception>
		private void WriteWhole(Deflater def, int typeCode, byte[] data)
		{
			int sz = data.Length;
			int hdrlen = 0;
			buf[hdrlen++] = unchecked((byte)((typeCode << 4) | sz & 15));
			sz = (int)(((uint)sz) >> 4);
			while (sz > 0)
			{
				buf[hdrlen - 1] |= unchecked((int)(0x80));
				buf[hdrlen++] = unchecked((byte)(sz & unchecked((int)(0x7f))));
				sz = (int)(((uint)sz) >> 7);
			}
			packDigest.Update(buf, 0, hdrlen);
			crc.Update(buf, 0, hdrlen);
			packOut.Write(buf, 0, hdrlen);
			def.Reset();
			def.SetInput(data);
			def.Finish();
			while (!def.IsFinished)
			{
				int datlen = def.Deflate(buf);
				packDigest.Update(buf, 0, datlen);
				crc.Update(buf, 0, datlen);
				packOut.Write(buf, 0, datlen);
			}
		}