Flush() public method

Flushes the stream by calling Flush on the deflater and then on the underlying stream. This ensures that all bytes are flushed.
public Flush ( ) : void
return void
Ejemplo n.º 1
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]);
            }
        }
		// netz.compress.ICompress implementation

		public long Compress(string file, string zipFile)
		{
			long length = -1;
			FileStream ifs = null;
			FileStream ofs = null;
			try
			{
				ifs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read);
				ofs = File.Open(zipFile, FileMode.Create, FileAccess.Write, FileShare.None);
				DeflaterOutputStream dos = new DeflaterOutputStream(ofs, new Deflater(Deflater.BEST_COMPRESSION));
				byte[] buff = new byte[ifs.Length];
				while(true)
				{
					int r = ifs.Read(buff, 0, buff.Length);
					if(r <= 0) break;
					dos.Write(buff, 0, r);
				}
				dos.Flush();
				dos.Finish();
				length = dos.Length;
				dos.Close();
			}
			finally
			{
				if(ifs != null) ifs.Close();
				if(ofs != null) ofs.Close();
			}
			return length;
		}
Ejemplo n.º 3
0
        public override byte[] Encode(byte[] data)
        {
            var o = new MemoryStream();

            var compressed = new DeflaterOutputStream(o, new Deflater(1));
            compressed.Write(data, 0, data.Length);
            compressed.Flush();
            compressed.Close();

            return o.ToArray();
        }
Ejemplo n.º 4
0
		internal static Byte[] Deflate(Byte[] b)
		{
			System.IO.MemoryStream ms = new System.IO.MemoryStream();
			DeflaterOutputStream outStream =new DeflaterOutputStream( ms);
			
			outStream.Write(b, 0, b.Length);
			outStream.Flush();
			outStream.Finish();
			
			Byte[] result=ms.ToArray();
			outStream.Close();
			ms.Close();
			return result;
		}
Ejemplo n.º 5
0
        /*
         * Name function: Compress
         * Purpose: compress a part of the byte array into a Zlib Block
         * Input: - buffer: byte array
         *        - offset: starting offset inside the array
         *        - count: num of bytes to compress starting from the offset
         * Output: compressed byte array block, the structure is:
         *         - magic word
         *         - max segment size
         *         - total compressed size
         *         - total uncompressed size
         *         - segment list
         *         - compressed data list
         */
        public static byte[] Compress(byte[] buffer, int offset, int count)
        {
            if(buffer == null)
                throw new ArgumentNullException();
            if (count < 0)
                throw new FormatException();
            if (offset + count > buffer.Length)
                throw new IndexOutOfRangeException();

            MemoryStream headBlock = new MemoryStream();
            MemoryStream dataBlock = new MemoryStream();
            DeflaterOutputStream zipStream;

            int numSeg = (int)Math.Ceiling((double)count / (double)maxSegmentSize);

            headBlock.WriteValueU32(magic);
            headBlock.WriteValueU32(maxSegmentSize);
            headBlock.WriteValueU32(0x0);            //total compressed size, still to calculate
            headBlock.WriteValueS32(count);          //total uncompressed size

            for (int i = count; i > 0; i -= (int)maxSegmentSize)
            {
                int copyBytes = Math.Min(i, (int)maxSegmentSize);
                uint precCompSize = (uint)dataBlock.Length;
                zipStream = new DeflaterOutputStream(dataBlock);
                zipStream.Write(buffer, offset + (count - i), copyBytes);
                zipStream.Flush();
                zipStream.Finish();
                headBlock.WriteValueU32((uint)dataBlock.Length - precCompSize); //compressed segment size
                headBlock.WriteValueS32(copyBytes); //uncompressed segment size
                //Console.WriteLine("  Segment size: {0}, total read: {1}, compr size: {2}", maxSegmentSize, copyBytes, (uint)dataBlock.Length - precCompSize);
            }

            headBlock.Seek(8, SeekOrigin.Begin);
            headBlock.WriteValueS32((int)dataBlock.Length); // total compressed size

            byte[] finalBlock = new byte[headBlock.Length + dataBlock.Length];
            Buffer.BlockCopy(headBlock.ToArray(), 0, finalBlock, 0, (int)headBlock.Length);
            Buffer.BlockCopy(dataBlock.ToArray(), 0, finalBlock, (int)headBlock.Length, (int)dataBlock.Length);
            headBlock.Close();
            dataBlock.Close();

            return finalBlock;
        }
    public static string Compress(byte[] data)
    {
        using(var m = new MemoryStream())
        {
            switch(technique)
            {
            case "ZipStream":

                var br = new BinaryWriter(m);
                var z = new DeflaterOutputStream(m);
                br.Write(data.Length);
                z.Write(data, 0, data.Length);
                z.Flush();
                z.Close();
                break;
            }
            return technique + ":" + Convert.ToBase64String(m.GetBuffer());
        }
    }
Ejemplo n.º 7
0
	public static string Compress(byte[] data, ICodeProgress progress)
	{
		using(var m = new MemoryStream())
		{
			switch(technique)
			{
			case "ZipStream":
				
				var br = new BinaryWriter(m);
				var z = new DeflaterOutputStream(m);
				br.Write(data.Length);
				z.Write(data, 0, data.Length);
				z.Flush();
				z.Close();
				break;
			case "7Zip":
			default:
				return Convert.ToBase64String(SevenZipRadical.Compression.LZMA.SevenZipRadicalHelper.Compress(data, progress));
			}
			return technique + ":" + Convert.ToBase64String(m.GetBuffer());
		}
	}
Ejemplo n.º 8
0
 public static string Compress(byte[] data)
 {
     string result;
     using (MemoryStream memoryStream = new MemoryStream())
     {
         string text = CompressionHelper.technique;
         if (text != null)
         {
             if (CompressionHelper.<>f__switch$map1F == null)
             {
                 CompressionHelper.<>f__switch$map1F = new Dictionary<string, int>(1)
                 {
                     {
                         "ZipStream",
                         0
                     }
                 };
             }
             int num;
             if (CompressionHelper.<>f__switch$map1F.TryGetValue(text, out num))
             {
                 if (num == 0)
                 {
                     BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
                     DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(memoryStream);
                     binaryWriter.Write(data.Length);
                     deflaterOutputStream.Write(data, 0, data.Length);
                     deflaterOutputStream.Flush();
                     deflaterOutputStream.Close();
                 }
             }
         }
         result = CompressionHelper.technique + ":" + Convert.ToBase64String(memoryStream.GetBuffer());
     }
     return result;
 }
Ejemplo n.º 9
0
        public static byte[] Compress( byte []b, int offset, int len )
        {
            try
            {
                /*zLib e = new zLib();
                byte []compressedData = null;
                e.CompressStream( b, ref compressedData );*/

                MemoryStream ms = new MemoryStream();
                DeflaterOutputStream defl = new DeflaterOutputStream( ms );

                defl.Write(b, offset, len );
                defl.Flush();
                defl.Close();
                byte[] compressedData = (byte[])ms.ToArray();
                //HexViewer.View( compressedData, 0, compressedData.Length );
                return compressedData;
            }
            catch(Exception e)
            {
                Console.WriteLine( e.Message );
                return null;
            }
        }
Ejemplo n.º 10
0
		/// <summary>
		/// deflates (zlib) a VideoCopy, returning a byte array suitable for insertion into a JMD file
		/// the byte array includes width and height dimensions at the beginning
		/// this is run asynchronously for speedup, as compressing can be slow
		/// </summary>
		/// <param name="v">video frame to compress</param>
		/// <returns>zlib compressed frame, with width and height prepended</returns>
		byte[] GzipFrame(VideoCopy v)
		{
			MemoryStream m = new MemoryStream();
			// write frame height and width first
			m.WriteByte((byte)(v.BufferWidth >> 8));
			m.WriteByte((byte)(v.BufferWidth & 255));
			m.WriteByte((byte)(v.BufferHeight >> 8));
			m.WriteByte((byte)(v.BufferHeight & 255));
			var g = new DeflaterOutputStream(m, new Deflater(token.compressionlevel));
			g.IsStreamOwner = false; // leave memory stream open so we can pick its contents
			g.Write(v.VideoBuffer, 0, v.VideoBuffer.Length);
			g.Flush();
			g.Close();
			byte[] ret = m.GetBuffer();
			Array.Resize(ref ret, (int)m.Length);
			m.Close();
			return ret;
		}
		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;
		}
        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;
        }
Ejemplo n.º 13
0
        private static void WriteDataChunks()
        {
            byte[] pixels = _image.ToByteArray();

            byte[] data = new byte[_image.PixelWidth * _image.PixelHeight * 4 + _image.PixelHeight];

            int rowLength = _image.PixelWidth * 4 + 1;

            for (int y = 0; y < _image.PixelHeight; y++)
            {
                byte compression = 0;
                if (y > 0)
                {
                    compression = 2;
                }
                data[y * rowLength] = compression;

                for (int x = 0; x < _image.PixelWidth; x++)
                {
                    // Calculate the offset for the new array.
                    int dataOffset = y * rowLength + x * 4 + 1;

                    // Calculate the offset for the original pixel array.
                    int pixelOffset = (y * _image.PixelWidth + x) * 4;

                    data[dataOffset + 0] = pixels[pixelOffset + 0];
                    data[dataOffset + 1] = pixels[pixelOffset + 1];
                    data[dataOffset + 2] = pixels[pixelOffset + 2];
                    data[dataOffset + 3] = pixels[pixelOffset + 3];

                    if (y > 0)
                    {
                        int lastOffset = ((y - 1) * _image.PixelWidth + x) * 4;

                        data[dataOffset + 0] -= pixels[lastOffset + 0];
                        data[dataOffset + 1] -= pixels[lastOffset + 1];
                        data[dataOffset + 2] -= pixels[lastOffset + 2];
                        data[dataOffset + 3] -= pixels[lastOffset + 3];
                    }
                }
            }

            byte[] buffer = null;
            int bufferLength = 0;

            MemoryStream memoryStream = null;
            try
            {
                memoryStream = new MemoryStream();

                using (DeflaterOutputStream zStream = new DeflaterOutputStream(memoryStream))
                {
                    zStream.Write(data, 0, data.Length);
                    zStream.Flush();
                    zStream.Finish();

                    bufferLength = (int)memoryStream.Length;
                    buffer = memoryStream.GetBuffer();
                }
            }
            finally
            {
                if (memoryStream != null)
                {
                    memoryStream.Dispose();
                }
            }

            int numChunks = bufferLength / MaxBlockSize;

            if (bufferLength % MaxBlockSize != 0)
            {
                numChunks++;
            }

            for (int i = 0; i < numChunks; i++)
            {
                int length = bufferLength - i * MaxBlockSize;

                if (length > MaxBlockSize)
                {
                    length = MaxBlockSize;
                }

                WriteChunk(PngChunkTypes.Data, buffer, i * MaxBlockSize, length);
            }
        }
Ejemplo n.º 14
0
        private static byte[] getOutputByCompress(byte[] toByteArray) {
            int unCompressedLength = 0;
            int compressedLength = 0;

            byte[] input = toByteArray;
            unCompressedLength = input.Length;

            MemoryStream memoryStream = new MemoryStream();
            Deflater compressor = new Deflater();
            DeflaterOutputStream defos = new DeflaterOutputStream(memoryStream, compressor);
            defos.Write(input, 0, input.Length);
            defos.Flush();
            defos.Finish();
            byte[] output = memoryStream.ToArray();
            compressedLength = output.Length;

            memoryStream.Close();
            defos.Close();

            //set compress flag and compressedLength, unCompressedLength
            byte[] sendData = new byte[output.Length + TransferUtil.getLengthOfByte() + TransferUtil.getLengthOfInt() + TransferUtil.getLengthOfInt()];
            sendData[0] = TransferObject.COMPRESS_FLAG; //0:normal; 1:compress
            TransferOutputStream fos = new TransferOutputStream(sendData);
            fos.skipAByte();
            fos.writeInt(unCompressedLength);
            fos.writeInt(compressedLength);
            Array.Copy(output, 0, sendData, TransferUtil.getLengthOfByte() + TransferUtil.getLengthOfInt() + TransferUtil.getLengthOfInt(), output.Length);

            return sendData;
        }
Ejemplo n.º 15
0
 /// <summary>
 /// Compress the specified data.
 /// </summary>
 /// <param name='data'>
 /// Data.
 /// </param>
 static string Compress(byte[] data)
 {
     MemoryStream ms = new MemoryStream();
     BinaryWriter bw = new BinaryWriter(ms);
     DeflaterOutputStream zs = new DeflaterOutputStream(ms);
     bw.Write(data.Length);
     zs.Write(data,0,data.Length);
     zs.Flush();
     zs.Close ();
     bw.Close();
     return "ZipStream:" + Convert.ToBase64String(ms.GetBuffer());
 }
Ejemplo n.º 16
0
 public static byte[] ZipBytes(byte[] input)
 {
     MemoryStream ms = new MemoryStream();
     Deflater zipper = new Deflater();
     zipper.SetLevel(5);
     Stream st = new DeflaterOutputStream(ms, zipper);
     st.Write(input, 0, input.Length);
     st.Flush();
     st.Close();
     byte[] result = (byte[])ms.ToArray();
     return result;
 }