public void IsGZipped()
        {
            string input = "eyup!";

            byte[] compressedData = CompressionUtils.Gzip(input);

            Assert.IsTrue(CompressionUtils.IsGZipped(compressedData));
        }
        public void TestString()
        {
            string input = "eyup!";

            byte[] compressedData   = CompressionUtils.Gzip(input);
            string uncompressedData = CompressionUtils.UnGzipToString(compressedData);

            Assert.AreEqual(input, uncompressedData);
        }
예제 #3
0
        /// <summary>
        ///     Encodes and compresses this container.
        /// </summary>
        /// <returns> The buffer. </returns>
        /// <exception cref="IOException"> if an I/O error occurs. </exception>
        public ByteBuffer Encode()
        {
            var data = GetData(); // so we have a read only view, making this method thread safe

            /* grab the data as a byte array for compression */
            var bytes = new byte[data.limit()];

            data.mark();
            data.get(bytes);
            data.reset();

            /* compress the data */
            byte[] compressed;
            if (type == COMPRESSION_NONE)
            {
                compressed = bytes;
            }
            else if (type == COMPRESSION_GZIP)
            {
                compressed = CompressionUtils.Gzip(bytes);
            }
            else if (type == COMPRESSION_BZIP2)
            {
                compressed = CompressionUtils.Bzip2(bytes);
            }
            else
            {
                throw new IOException("Invalid compression type");
            }

            /* calculate the size of the header and trailer and allocate a buffer */
            var header = 5 + (type == COMPRESSION_NONE ? 0 : 4) + (IsVersioned() ? 2 : 0);
            var buf    = ByteBuffer.allocate(header + compressed.Length);

            /* write the header, with the optional uncompressed length */
            buf.put((byte)type);
            buf.putInt(compressed.Length);
            if (type != COMPRESSION_NONE)
            {
                buf.putInt(data.limit());
            }

            /* write the compressed length */
            buf.put(compressed);

            /* write the trailer with the optional version */
            if (IsVersioned())
            {
                buf.putShort((short)version);
            }

            /* flip the buffer and return it */
            return((ByteBuffer)buf.flip());
        }
        public void TestBytes()
        {
            string input = "eyup!";

            byte[] inputBytes = Encoding.UTF8.GetBytes(input);

            byte[] compressedData   = CompressionUtils.Gzip(inputBytes);
            byte[] uncompressedData = CompressionUtils.UnGZipToBytes(compressedData);

            Assert.IsTrue(inputBytes.SequenceEqual(uncompressedData));
        }
        public async Task PutAsync()
        {
            DistributedCacheWrapperWithCompression distributedCacheWrapperWithCompression = new DistributedCacheWrapperWithCompression(_distributedCache.Object);

            await distributedCacheWrapperWithCompression.PutAsync(_key, _testClass, _ttl, CancellationToken.None, true);

            byte[] result = Utf8Json.JsonSerializer.Serialize(_testClass, StandardResolver.AllowPrivate);

            byte[] compressedBytes = CompressionUtils.Gzip(result);

            _distributedCache.Verify(x => x.SetAsync(It.Is <string>(y => y == _key), It.Is <byte[]>(y => y.SequenceEqual(compressedBytes)), It.Is <DistributedCacheEntryOptions>(y => y.AbsoluteExpirationRelativeToNow.Value == _ttl.Timespan), It.IsAny <CancellationToken>()));
        }
        public async Task TryGetAsync_Compressed()
        {
            byte[] serialisedTestClass = Utf8Json.JsonSerializer.Serialize(_testClass, StandardResolver.AllowPrivate);

            byte[] compressedSerialisedTestClasss = CompressionUtils.Gzip(serialisedTestClass);

            _distributedCache.Setup(x => x.GetAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())).ReturnsAsync(compressedSerialisedTestClasss);

            DistributedCacheWrapperWithCompression distributedCacheWrapperWithCompression = new DistributedCacheWrapperWithCompression(_distributedCache.Object);

            (bool, object)result = await distributedCacheWrapperWithCompression.TryGetAsync <TestClass>(_key, CancellationToken.None, true);

            Assert.IsTrue(result.Item1);
            Assert.AreEqual(_testClass.Property, ((TestClass)result.Item2).Property);
        }