Ejemplo n.º 1
0
        public void SetPictureData(byte[] pictureData)
        {
            base.PictureData = (pictureData);
            UncompressedSize = (pictureData.Length);

            // info of chicago project:
            // "... LZ compression algorithm in the format used by GNU Zip deflate/inflate with a 32k window ..."
            // not sure what to do, when lookup tables exceed 32k ...

            try
            {
                MemoryStream         bos = new MemoryStream();
                DeflaterOutputStream dos = new DeflaterOutputStream(bos);
                dos.Write(pictureData, 0, pictureData.Length);
                dos.Dispose();
                raw_pictureData = bos.ToArray();
            }
            catch (IOException e)
            {
                throw new RuntimeException("Can't compress metafile picture data", e);
            }

            CompressedSize = (raw_pictureData.Length);
            IsCompressed   = (true);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Compress the contents of the provided array
 /// </summary>
 /// <param name="data">An uncompressed byte array</param>
 /// <returns></returns>
 public static byte[] Compress(byte[] data)
 {
     using (MemoryStream out1 = new MemoryStream())
     {
         Deflater             deflater             = new Deflater(0, false);
         DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(out1, deflater);
         try
         {
             //for (int i = 0; i < data.Length; i++)
             //deflaterOutputStream.WriteByte(data[i]);
             deflaterOutputStream.Write(data, 0, data.Length);   //Tony Qu changed the code
             return(out1.ToArray());
         }
         catch (IOException e)
         {
             throw new RecordFormatException(e.ToString());
         }
         finally
         {
             out1.Dispose();
             if (deflaterOutputStream != null)
             {
                 deflaterOutputStream.Dispose();
             }
         }
     }
 }
        public void DeflatorStreamOwnership()
        {
            var memStream = new TrackedMemoryStream();
            var s         = new DeflaterOutputStream(memStream);

            Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially");
            Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially");

#if NET451
            s.Close();
#elif NETCOREAPP1_0
            s.Dispose();
#endif

            Assert.IsTrue(memStream.IsClosed, "Should be closed after parent owner close");
            Assert.IsTrue(memStream.IsDisposed, "Should be disposed after parent owner close");

            memStream = new TrackedMemoryStream();
            s         = new DeflaterOutputStream(memStream);

            Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially");
            Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially");

            s.IsStreamOwner = false;
#if NET451
            s.Close();
#elif NETCOREAPP1_0
            s.Dispose();
#endif

            Assert.IsFalse(memStream.IsClosed, "Should not be closed after parent owner close");
            Assert.IsFalse(memStream.IsDisposed, "Should not be disposed after parent owner close");
        }
Ejemplo n.º 4
0
        public void WriteToStream(IPacketCodec rawCodec, Func <int> compressThresholdCallback)
        {
            var threshold = compressThresholdCallback();

            if (threshold == 0 || DataLength < threshold)
            {
                PacketLength = (int)BaseStream.Length;
                rawCodec.WriteVarInt(DataLength);
                Content.CopyTo(rawCodec.BaseStream, DataLength);
                return;
            }
            var stream  = new MemoryStream();
            var content = Content.Clone(stream);

            content.WriteVarInt(DataLength);
            var compressedStream = new DeflaterOutputStream(stream);

            Content.CopyTo(compressedStream, DataLength);
            compressedStream.Flush();
            stream.Position = 0;
            PacketLength    = (int)stream.Length;
            rawCodec.WriteVarInt(PacketLength);
            content.CopyTo(rawCodec.BaseStream, PacketLength);
            compressedStream.Dispose();
            stream.Dispose();
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 压缩字符串数据
        /// </summary>
        /// <param name="data">压缩字符串</param>
        /// <param name="result">返回压缩内容</param>
        /// <returns></returns>
        public static bool Compress(string data, ref string result)
        {
            try
            {
                byte[] buffer;
                using (MemoryStream ms = new MemoryStream())
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    bf.Serialize(ms, data);
                    buffer = ms.ToArray();
                    ms.Close();
                    ms.Dispose();
                }

                using (MemoryStream msout = new MemoryStream())
                {
                    Deflater defl = new Deflater();
                    using (DeflaterOutputStream mem1 = new DeflaterOutputStream(msout, defl))
                    {
                        mem1.Write(buffer, 0, buffer.Length);
                        mem1.Close();
                        mem1.Dispose();
                    }
                    result = Convert.ToBase64String(msout.ToArray());
                    msout.Close();
                    msout.Dispose();
                }
                return(true);
            }
            catch (Exception ex)
            {
                result = ex.Message;
                return(false);
            }
        }
Ejemplo n.º 6
0
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         ost.Dispose();
         base.Dispose(disposing);
     }
 }
Ejemplo n.º 7
0
        /// <exception cref="System.IO.IOException"/>
        public virtual void WriteIccProfile(byte[] data)
        {
            MemoryStream stream = new MemoryStream();

            stream.Write((byte)'I');
            stream.Write((byte)'C');
            stream.Write((byte)'C');
            stream.Write(0);
            stream.Write(0);
            DeflaterOutputStream zip = new DeflaterOutputStream(stream);

            zip.Write(data);
            zip.Dispose();
            WriteChunk(iCCP, stream.ToArray());
        }
Ejemplo n.º 8
0
        public virtual void TestImageCompressLevel()
        {
            byte[] b = ImageDataFactory.Create(sourceFolder + "berlin2013.jpg").GetData();
            ByteArrayOutputStream image = new ByteArrayOutputStream();

            image.AssignBytes(b, b.Length);
            MemoryStream         byteArrayStream1 = new ByteArrayOutputStream();
            DeflaterOutputStream zip = new DeflaterOutputStream(byteArrayStream1, 9);

            image.WriteTo(zip);
            MemoryStream         byteArrayStream2 = new ByteArrayOutputStream();
            DeflaterOutputStream zip2             = new DeflaterOutputStream(byteArrayStream2, -1);

            image.WriteTo(zip2);
            NUnit.Framework.Assert.IsTrue(byteArrayStream1.Length == byteArrayStream2.Length);
            zip.Dispose();
            zip2.Dispose();
        }
Ejemplo n.º 9
0
        /// <exception cref="System.IO.IOException"/>
        public virtual void WriteData(byte[] data, int stride)
        {
            MemoryStream         stream = new MemoryStream();
            DeflaterOutputStream zip    = new DeflaterOutputStream(stream);
            int k;

            for (k = 0; k < data.Length - stride; k += stride)
            {
                zip.Write(0);
                zip.Write(data, k, stride);
            }
            int remaining = data.Length - k;

            if (remaining > 0)
            {
                zip.Write(0);
                zip.Write(data, k, remaining);
            }
            zip.Dispose();
            WriteChunk(IDAT, stream.ToArray());
        }