Example #1
0
        public void CompressUncompressEmpty()
        {
            var compressed = CompressTool.Compress(new byte[0]);

            Assert.IsTrue(compressed.Length > 0);
            Assert.AreEqual(0, CompressTool.Uncompress(compressed).Length);
        }
Example #2
0
        public override byte[] OnSerialize(ImageFile.FileData data)
        {
            var rawData = data.ToByteArray();
            var newData = CompressTool.Compress(rawData, CompressTool.CompressAlgorithm.Zstd);

            UnityEngine.Debug.LogFormat("Raw data size {0} KB, Compressed data size {1}",
                                        rawData.Length / 1024, newData.Length > 1024?newData.Length / 1024 + "KB": newData.Length + "B");
            return(newData);
        }
Example #3
0
        public void CompressUncompressTest()
        {
            var data       = _dataCreator.GenerateTestData(1024 * 1024);
            var compressed = CompressTool.Compress(data);

            Assert.IsTrue(compressed.Length > 0);

            Assert.IsTrue(data.SequenceEqual(CompressTool.Uncompress(compressed)));
        }
Example #4
0
        public void CompressRange()
        {
            var input  = Encoding.ASCII.GetBytes("ByeHelloBye");
            var output = new byte[100];
            var length = CompressTool.Compress(input, 3, 5, output, 10);

            Assert.AreEqual("Hello",
                            Encoding.ASCII.GetString(CompressTool.Uncompress(output.Skip(10).Take(length).ToArray())));
        }
Example #5
0
 /// <summary>
 /// Resets this frame to contain compressed data.
 /// </summary>
 /// <param name="data">Input buffer containing uncompressed data that is compressed by this method before being stored in the frame.</param>
 /// <param name="offset">Offset of uncompressed data in the input buffer.</param>
 /// <param name="count">Size of uncompressed data in the input buffer. Maximum data size is 64KB.</param>
 public void SetCompressed(byte[] data, int offset, int count)
 {
     CheckRange(data, offset, count);
     CheckMaxFrameSize(count);
     EnsureBuffer(CompressTool.GetMaxCompressedLength(count));
     BufferUsage = CompressTool.Compress(data, offset, count, Buffer, 0);
     DataLength  = count;
     Checksum    = ComputeMasked(data, offset, count);
     Type        = SnappyFrameType.Compressed;
 }
Example #6
0
        public void UncompressRange()
        {
            var howdy  = Encoding.ASCII.GetBytes("Howdy");
            var padded = howdy.Take(3).Concat(CompressTool.Compress(Encoding.ASCII.GetBytes("Hello"))).Concat(howdy.Skip(3)).ToArray();
            var output = new byte[100];
            var length = CompressTool.Uncompress(padded, 3, padded.Length - 5, output, 10);

            Assert.AreEqual(5, length);
            Assert.AreEqual("Hello", Encoding.ASCII.GetString(output.Skip(10).Take(5).ToArray()));
        }
Example #7
0
        public void GetUncompressedLengthExceptions()
        {
            var uncompressed = Encoding.ASCII.GetBytes("Hello, hello, howdy?");
            var compressed   = CompressTool.Compress(uncompressed);
            var buffer       = new byte[100];

            TestException(() => { CompressTool.GetUncompressedLength(null, 0, 3); }, typeof(ArgumentNullException));

            TestException(() => { CompressTool.GetUncompressedLength(compressed, -1, uncompressed.Length); }, typeof(ArgumentOutOfRangeException));
            TestException(() => { CompressTool.GetUncompressedLength(compressed, 0, -1); }, typeof(ArgumentOutOfRangeException));
            TestException(() => { CompressTool.GetUncompressedLength(compressed, compressed.Length - 2, 4); }, typeof(ArgumentOutOfRangeException));

            TestException(() => { CompressTool.GetUncompressedLength(compressed, 0, 0); }, typeof(System.IO.IOException));
            TestException(() => { CompressTool.GetUncompressedLength(compressed, compressed.Length, 0); }, typeof(System.IO.IOException));
        }
Example #8
0
 private void DoUncompressionBenchmark(int size)
 {
     Benchmark.Run("Uncompressing", size, benchmark =>
     {
         var compressed = CompressTool.Compress(benchmark.Input);
         var roundtrip  = new byte[benchmark.Input.Length];
         int length     = 0;
         benchmark.Stopwatch.Start();
         for (int i = 0; i < benchmark.Iterations; ++i)
         {
             length = CompressTool.Uncompress(compressed, 0, compressed.Length, roundtrip, 0);
         }
         benchmark.Stopwatch.Stop();
         CollectionAssert.AreEqual(benchmark.Input, roundtrip);
     });
 }
Example #9
0
        private void DoCompressionOneParaBenchmark(int size)
        {
            Benchmark.Run("Compressing", size, benchmark =>
            {
                int length = 0;
                benchmark.Stopwatch.Start();
                for (int i = 0; i < benchmark.Iterations; ++i)
                {
                    var output = CompressTool.Compress(benchmark.Input);
                    length     = output.Length;
                }
                benchmark.Stopwatch.Stop();


                benchmark.Note = String.Format(" ({0:0.00 %})", length / (double)benchmark.Input.Length);
            });
        }
Example #10
0
        /// <summary>
        /// Create json file
        /// </summary>
        private void CreateJsonFile(string path, string savePath)
        {
            csFileName = Path.GetFileNameWithoutExtension(path);
            fileName   = csFileName + ".json";
            //if (!cbIsDat.Checked)
            //    fileName = "__" + fileName;

            bool   showOutput = checkBoxShowOutput.Checked;
            string strJson    = ExcelToJsonStr(path, cbIsCsFile.Checked);

            if (strJson != string.Empty && strJson != null)
            {
                savePath = savePath + "\\" + fileName;
                string writeStr = string.Empty;
                if (cbIsDat.Checked)
                {
                    writeStr = CompressTool.Compress(strJson);
                }
                else
                {
                    writeStr = strJson;
                }
                FileStream   fs = new FileStream(savePath, FileMode.Create);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(writeStr);
                sw.Close();
                txtInfo.Text += "Generated: " + fileName + "\r\n";
                if (showOutput)
                {
                    textJsonInfo.Text += "Json: " + fileName + "\r\n" + strJson + "\r\n\r\n";
                }
            }
            else
            {
                txtInfo.Text      += fileName + " not generated ERROR\r\n";
                textJsonInfo.Text += fileName + " not generated ERROR\r\n";
            }

            if (showOutput)
            {
                this.textJsonInfo.Select(this.textJsonInfo.TextLength, 0);
                this.textJsonInfo.ScrollToCaret();
                this.txtInfo.Select(this.textJsonInfo.TextLength, 0);
                this.txtInfo.ScrollToCaret();
            }
        }
Example #11
0
 private void DoCompressionBenchmark(int size)
 {
     Benchmark.Run("Compressing", size, benchmark =>
     {
         var output = new byte[CompressTool.GetMaxCompressedLength(benchmark.Input.Length)];
         int length = 0;
         benchmark.Stopwatch.Start();
         for (int i = 0; i < benchmark.Iterations; ++i)
         {
             length = CompressTool.Compress(benchmark.Input, 0, benchmark.Input.Length, output, 0);
         }
         benchmark.Stopwatch.Stop();
         var roundtrip       = new byte[benchmark.Input.Length];
         var roundtripLength = CompressTool.Uncompress(output, 0, length, roundtrip, 0);
         Assert.IsTrue(benchmark.Input.SequenceEqual(roundtrip.Take(roundtripLength)));
         benchmark.Note = String.Format(" ({0:0.00 %})", length / (double)benchmark.Input.Length);
     });
 }
Example #12
0
 private Task DoAsyncUncompressionOneParaBenchmark(int size)
 {
     return(Task.Factory.StartNew(() =>
     {
         Benchmark.Run("Uncompressing", size, benchmark =>
         {
             var compressed = CompressTool.Compress(benchmark.Input);
             var roundtrip = new byte[benchmark.Input.Length];
             int length = 0;
             benchmark.Stopwatch.Start();
             for (int i = 0; i < benchmark.Iterations; ++i)
             {
                 roundtrip = CompressTool.Uncompress(compressed);
                 length = roundtrip.Length;
             }
             benchmark.Stopwatch.Stop();
             CollectionAssert.AreEqual(benchmark.Input, roundtrip);
         });
     }));
 }
Example #13
0
 private Task DoAsyncCompressionOneParaBenchmark(int size)
 {
     return(Task.Factory.StartNew(() =>
     {
         Benchmark.Run("Compressing", size, benchmark =>
         {
             var output = new byte[CompressTool.GetMaxCompressedLength(benchmark.Input.Length)];
             int length = 0;
             benchmark.Stopwatch.Start();
             for (int i = 0; i < benchmark.Iterations; ++i)
             {
                 output = CompressTool.Compress(benchmark.Input);
                 length = output.Length;
             }
             benchmark.Stopwatch.Stop();
             var roundtrip = CompressTool.Uncompress(output);
             Assert.IsTrue(benchmark.Input.SequenceEqual(roundtrip));
             benchmark.Note = String.Format(" ({0:0.00 %})", length / (double)benchmark.Input.Length);
         });
     }));
 }
Example #14
0
            public override byte[] OnSerialize(Data data)
            {
                var raw = Serilization.ObjectToByteArray(data);

                return(CompressTool.Compress(raw));
            }
Example #15
0
        public void CompressExceptions()
        {
            var input  = new byte[100];
            var output = new byte[100];

            try
            {
                CompressTool.Compress(null);
                Assert.Fail("shouldn't reach there");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(ArgumentNullException));
            }


            try
            {
                CompressTool.Compress(null, 0, 3, output, 0);
                Assert.Fail("shouldn't reach there");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(ArgumentNullException));
            }


            try
            {
                CompressTool.Compress(input, 0, 3, null, 0);
                Assert.Fail("shouldn't reach there");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(ArgumentNullException));
            }

            try
            {
                CompressTool.Compress(input, -1, 3, output, 0);
                Assert.Fail("shouldn't reach there");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(ArgumentOutOfRangeException));
            }

            try
            {
                CompressTool.Compress(input, -1, 3, output, 0);
                Assert.Fail("shouldn't reach there");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(ArgumentOutOfRangeException));
            }

            try
            {
                CompressTool.Compress(input, 0, -1, output, 0);
                Assert.Fail("shouldn't reach there");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(ArgumentOutOfRangeException));
            }

            try
            {
                CompressTool.Compress(input, 90, 20, output, 0);
                Assert.Fail("shouldn't reach there");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(ArgumentOutOfRangeException));
            }

            try
            {
                CompressTool.Compress(input, 0, 3, output, -1);
                Assert.Fail("shouldn't reach there");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(ArgumentOutOfRangeException));
            }

            try
            {
                CompressTool.Compress(input, 0, 3, output, 100);
                Assert.Fail("shouldn't reach there");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(ArgumentOutOfRangeException));
            }

            try
            {
                CompressTool.Compress(input, 0, 3, output, 101);
                Assert.Fail("shouldn't reach there");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(ArgumentOutOfRangeException));
            }

            try
            {
                CompressTool.Compress(input, 0, 100, new byte[3], 0);
                Assert.Fail("shouldn't reach there");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(ArgumentOutOfRangeException));
            }
        }
Example #16
0
 public void CompressUncompressSimple()
 {
     Assert.AreEqual("Hello",
                     Encoding.ASCII.GetString(
                         CompressTool.Uncompress(CompressTool.Compress(Encoding.ASCII.GetBytes("Hello")))));
 }