示例#1
0
        public void StreamCompression()
        {
            var plain = PlainText.ToStream();

            Assert.IsTrue(plain.Length > 0);

            foreach (var compressor in _compressors)
            {
                if (IsDebugEnabled)
                {
                    log.Debug("Compressor : " + compressor.GetType().FullName);
                }

                Compressor.InnerCompressor = compressor;

                plain.SetStreamPosition();
                using (var compressed = Compressor.Compress(plain)) {
                    compressed.SetStreamPosition();
                    using (var restored = Compressor.Decompress(compressed)) {
                        restored.SetStreamPosition();
                        Assert.AreEqual(PlainText, restored.ToText());
                    }
                }
            }
        }
        public void Adaptor_Compress_Decompress_By_Stream_InParallel()
        {
            var plainStream = PlainText.ToStream();

            Parallel.ForEach(_compressors,
                             compressor => {
                var adapter = new CompressAdapter(compressor);

                var compressed     = adapter.Compress(plainStream);
                var recoveryStream = new MemoryStream(compressed.Length * 2);
                adapter.Decompress(compressed, recoveryStream);

                // 데이터가 써졌으므로, 다시 원 위치로 설정해줘야 합니다.
                recoveryStream.SetStreamPosition();

                Assert.IsTrue(recoveryStream.Length > 0);
                Assert.AreEqual(PlainText, recoveryStream.ToText());
            });
        }
        public void BZip2_Compress_Extract_Test()
        {
            var plainStream = PlainText.ToStream();

            plainStream.Seek(0, SeekOrigin.Begin);

            var plainData = Encoding.UTF8.GetBytes(PlainText);

            byte[] compressedData;
            byte[] extractedData;

            // Compress
            using (var compressedStream = new MemoryStream())
                using (var bz2 = new BZip2OutputStream(compressedStream)) {
                    bz2.Write(plainData, 0, plainData.Length);
                    bz2.Close();
                    compressedData = compressedStream.ToArray();
                }

            Assert.IsNotNull(compressedData);

            // Array.Resize(ref compressedData, compressedData.Length+1);
            // compressedData[compressedData.Length - 1] = (byte)0;

            // Extract
            using (var compressedStream = new MemoryStream(compressedData))
                using (var bz2 = new BZip2InputStream(compressedStream))
                    using (var extractedStream = new MemoryStream()) {
                        StreamTool.CopyStreamToStream(bz2, extractedStream);
                        extractedData = extractedStream.ToArray();
                    }


            Assert.IsNotNull(extractedData);
            string extractedText = Encoding.UTF8.GetString(extractedData).TrimEnd('\0');

            Assert.AreEqual(PlainText, extractedText);
        }
        public void CompressStreamAsyncTest()
        {
            var plain = PlainText.ToStream();

            Assert.IsTrue(plain.Length > 0);

            foreach (var compressor in _compressors)
            {
                if (IsDebugEnabled)
                {
                    log.Debug("Compressor : " + compressor.GetType().FullName);
                }

                plain.SetStreamPosition();
                using (var compressed = compressor.CompressStreamAsync(plain).Result) {
                    compressed.SetStreamPosition();
                    using (var restored = compressor.DecompressStreamAsync(compressed).Result) {
                        restored.SetStreamPosition();
                        Assert.AreEqual(PlainText, restored.ToText());
                    }
                }
            }
        }
        public void GZip_Compress_Extract_Test()
        {
            var plainStream = PlainText.ToStream();

            plainStream.Seek(0, SeekOrigin.Begin);

            var plainData = Encoding.UTF8.GetBytes(PlainText);

            byte[] compressedData;
            byte[] extractedData;

            // Compress
            using (var compressedStream = new MemoryStream())
                using (var gzs = new GZipOutputStream(compressedStream)) {
                    gzs.SetLevel(5);
                    gzs.Write(plainData, 0, plainData.Length);
                    gzs.Finish();
                    compressedData = compressedStream.ToArray();
                }

            Assert.IsNotNull(compressedData);

            // Extract
            using (var compressedStream = new MemoryStream(compressedData)) {
                // compressedStream.Seek(0, SeekOrigin.Begin);
                using (var gzs = new GZipInputStream(compressedStream))
                    using (var extractedStream = new MemoryStream()) {
                        StreamTool.CopyStreamToStream(gzs, extractedStream);
                        extractedData = extractedStream.ToArray();
                    }
            }

            Assert.IsNotNull(extractedData);
            string extractedText = Encoding.UTF8.GetString(extractedData).TrimEnd('\0');

            Assert.AreEqual(PlainText, extractedText);
        }