/// <exception cref="System.IO.IOException"/>
        private byte[] CompressDecompressZlib(byte[] rawData, ZlibCompressor zlibCompressor
                                              , ZlibDecompressor zlibDecompressor)
        {
            int cSize = 0;

            byte[] compressedByte      = new byte[rawData.Length];
            byte[] decompressedRawData = new byte[rawData.Length];
            zlibCompressor.SetInput(rawData, 0, rawData.Length);
            zlibCompressor.Finish();
            while (!zlibCompressor.Finished())
            {
                cSize = zlibCompressor.Compress(compressedByte, 0, compressedByte.Length);
            }
            zlibCompressor.Reset();
            Assert.True(zlibDecompressor.GetBytesWritten() == 0);
            Assert.True(zlibDecompressor.GetBytesRead() == 0);
            Assert.True(zlibDecompressor.NeedsInput());
            zlibDecompressor.SetInput(compressedByte, 0, cSize);
            NUnit.Framework.Assert.IsFalse(zlibDecompressor.NeedsInput());
            while (!zlibDecompressor.Finished())
            {
                zlibDecompressor.Decompress(decompressedRawData, 0, decompressedRawData.Length);
            }
            Assert.True(zlibDecompressor.GetBytesWritten() == rawData.Length
                        );
            Assert.True(zlibDecompressor.GetBytesRead() == cSize);
            zlibDecompressor.Reset();
            Assert.True(zlibDecompressor.GetRemaining() == 0);
            Assert.AssertArrayEquals("testZlibCompressorDecompressorWithConfiguration array equals error"
                                     , rawData, decompressedRawData);
            return(decompressedRawData);
        }
Пример #2
0
 static ZlibFactory()
 {
     if (NativeCodeLoader.IsNativeCodeLoaded())
     {
         nativeZlibLoaded = ZlibCompressor.IsNativeZlibLoaded() && ZlibDecompressor.IsNativeZlibLoaded
                                ();
         if (nativeZlibLoaded)
         {
             Log.Info("Successfully loaded & initialized native-zlib library");
         }
         else
         {
             Log.Warn("Failed to load/initialize native-zlib library");
         }
     }
 }
        public virtual void TestZlibCompressDecompress()
        {
            byte[] rawData     = null;
            int    rawDataSize = 0;

            rawDataSize = 1024 * 64;
            rawData     = Generate(rawDataSize);
            try
            {
                ZlibCompressor   compressor   = new ZlibCompressor();
                ZlibDecompressor decompressor = new ZlibDecompressor();
                NUnit.Framework.Assert.IsFalse("testZlibCompressDecompress finished error", compressor
                                               .Finished());
                compressor.SetInput(rawData, 0, rawData.Length);
                Assert.True("testZlibCompressDecompress getBytesRead before error"
                            , compressor.GetBytesRead() == 0);
                compressor.Finish();
                byte[] compressedResult = new byte[rawDataSize];
                int    cSize            = compressor.Compress(compressedResult, 0, rawDataSize);
                Assert.True("testZlibCompressDecompress getBytesRead ather error"
                            , compressor.GetBytesRead() == rawDataSize);
                Assert.True("testZlibCompressDecompress compressed size no less then original size"
                            , cSize < rawDataSize);
                decompressor.SetInput(compressedResult, 0, cSize);
                byte[] decompressedBytes = new byte[rawDataSize];
                decompressor.Decompress(decompressedBytes, 0, decompressedBytes.Length);
                Assert.AssertArrayEquals("testZlibCompressDecompress arrays not equals ", rawData
                                         , decompressedBytes);
                compressor.Reset();
                decompressor.Reset();
            }
            catch (IOException ex)
            {
                NUnit.Framework.Assert.Fail("testZlibCompressDecompress ex !!!" + ex);
            }
        }
Пример #4
0
 public static string GetLibraryName()
 {
     return(ZlibCompressor.GetLibraryName());
 }