Ejemplo n.º 1
0
 public void InvalidQuality()
 {
     Assert.Throws <ArgumentOutOfRangeException>("quality", () => new BrotliEncoder(-1, 11));
     Assert.Throws <ArgumentOutOfRangeException>("quality", () => new BrotliEncoder(12, 11));
     Assert.Throws <ArgumentOutOfRangeException>("quality", () => BrotliEncoder.TryCompress(new ReadOnlySpan <byte>(), new Span <byte>(), out int bytesWritten, -1, 13));
     Assert.Throws <ArgumentOutOfRangeException>("quality", () => BrotliEncoder.TryCompress(new ReadOnlySpan <byte>(), new Span <byte>(), out int bytesWritten, 12, 13));
 }
Ejemplo n.º 2
0
        public void Compress_WithEmptyDestination()
        {
            string testFile = UncompressedTestFile();

            byte[] correctUncompressedBytes = File.ReadAllBytes(testFile);
            byte[] compressedBytes          = File.ReadAllBytes(CompressedTestFile(testFile));
            byte[] empty = new byte[0];
            ReadOnlySpan <byte> source      = new ReadOnlySpan <byte>(correctUncompressedBytes);
            Span <byte>         destination = new Span <byte>(empty);

            Assert.False(BrotliEncoder.TryCompress(source, destination, out int bytesWritten), "TryCompress completed successfully but should have failed due to too short of a destination array");
            Assert.Equal(0, bytesWritten);

            BrotliEncoder encoder;
            var           result = encoder.Compress(source, destination, out int bytesConsumed, out bytesWritten, false);

            Assert.Equal(0, bytesWritten);
            Assert.Equal(0, bytesConsumed);
            Assert.Equal(OperationStatus.DestinationTooSmall, result);

            result = encoder.Compress(source, destination, out bytesConsumed, out bytesWritten, isFinalBlock: true);
            Assert.Equal(0, bytesWritten);
            Assert.Equal(0, bytesConsumed);
            Assert.Equal(OperationStatus.DestinationTooSmall, result);
        }
Ejemplo n.º 3
0
        public void Compress_WithEmptySource()
        {
            string testFile = UncompressedTestFile();

            byte[] sourceBytes              = new byte[0];
            byte[] destinationBytes         = new byte[100000];
            ReadOnlySpan <byte> source      = new ReadOnlySpan <byte>(sourceBytes);
            Span <byte>         destination = new Span <byte>(destinationBytes);

            Assert.True(BrotliEncoder.TryCompress(source, destination, out int bytesWritten));
            // The only byte written should be the Brotli end of stream byte which varies based on the window/quality
            Assert.Equal(1, bytesWritten);

            BrotliEncoder encoder;
            var           result = encoder.Compress(source, destination, out int bytesConsumed, out bytesWritten, false);

            Assert.Equal(0, bytesWritten);
            Assert.Equal(0, bytesConsumed);
            Assert.Equal(OperationStatus.Done, result);

            result = encoder.Compress(source, destination, out bytesConsumed, out bytesWritten, isFinalBlock: true);
            Assert.Equal(1, bytesWritten);
            Assert.Equal(0, bytesConsumed);
            Assert.Equal(OperationStatus.Done, result);
        }
Ejemplo n.º 4
0
        public async Task WriteRequestContentAsync(WebRequest request, HttpContent content)
        {
            byte[] payload;
            if (content is StreamContent)
            {
                // TODO
                throw new NotSupportedException();
            }
            else
            {
                payload = await content.ReadAsByteArrayAsync().ConfigureAwait(false);
            }

            if (request.Headers[HttpRequestHeader.ContentEncoding] == "br")
            {
                byte[] encoded = new byte[BrotliEncoder.GetMaxCompressedLength(payload.Length)];
                if (BrotliEncoder.TryCompress(payload, encoded, out int bytesWritten))
                {
                    payload = new byte[bytesWritten];
                    Buffer.BlockCopy(encoded, 0, payload, 0, bytesWritten);
                }
            }

            request.ContentLength = payload.Length;
            using (Stream output = await request.GetRequestStreamAsync().ConfigureAwait(false))
            {
                await output.WriteAsync(payload, 0, payload.Length).ConfigureAwait(false);
            }
        }
Ejemplo n.º 5
0
        public int CompressBrotliDefault()
        {
            int         a    = 0;
            int         t    = 0;
            Span <byte> span = new Span <byte>();

            if (TestType == Types.ShortSentence)
            {
                for (int i = 0; i < _sentences.GetLength(0); i++)
                {
                    Span <byte> compressed = new Span <byte>(new byte[BrotliEncoder.GetMaxCompressedLength(_sentences[i].Length)]);
                    BrotliEncoder.TryCompress(_sentences[i], compressed, out t);
                    a++;
                }
            }
            if (TestType == Types.Word)
            {
                for (int i = 0; i < _words.GetLength(0); i++)
                {
                    Span <byte> compressed = new Span <byte>(new byte[BrotliEncoder.GetMaxCompressedLength(_words[i].Length)]);
                    BrotliEncoder.TryCompress(_words[i], compressed, out t);
                    a++;
                }
            }

            if (TestType == Types.LongText)
            {
                Span <byte> compressed = new Span <byte>(new byte[BrotliEncoder.GetMaxCompressedLength(_longText.Length)]);
                BrotliEncoder.TryCompress(_longText, compressed, out t);
            }

            return(a);
        }
Ejemplo n.º 6
0
 public void InvalidWindow()
 {
     Assert.Throws <ArgumentOutOfRangeException>("window", () => new BrotliEncoder(10, -1));
     Assert.Throws <ArgumentOutOfRangeException>("window", () => new BrotliEncoder(10, 9));
     Assert.Throws <ArgumentOutOfRangeException>("window", () => new BrotliEncoder(10, 25));
     Assert.Throws <ArgumentOutOfRangeException>("window", () => BrotliEncoder.TryCompress(new ReadOnlySpan <byte>(), new Span <byte>(), out int bytesWritten, 6, -1));
     Assert.Throws <ArgumentOutOfRangeException>("window", () => BrotliEncoder.TryCompress(new ReadOnlySpan <byte>(), new Span <byte>(), out int bytesWritten, 6, 9));
     Assert.Throws <ArgumentOutOfRangeException>("window", () => BrotliEncoder.TryCompress(new ReadOnlySpan <byte>(), new Span <byte>(), out int bytesWritten, 6, 25));
 }
Ejemplo n.º 7
0
        internal static int WriteBrotli(DirectBuffer source, DirectBuffer destination)
        {
            if (BrotliEncoder.TryCompress(source.Span, destination.Span, out var bytesWritten,
                                          Settings.BrotliCompressionLevel, 10))
            {
                return(bytesWritten);
            }

            return(0);
        }
        public Span <byte> Compress(Span <byte> bytes, Span <byte> target)
        {
            var needMinLength = BrotliEncoder.GetMaxCompressedLength(bytes.Length);

            Guard.Argument(target.Length).GreaterThan(needMinLength);

            var success = BrotliEncoder.TryCompress(bytes, target, out var bytesWritten, 2, 10);

            Guard.Argument(success).Require(true);

            return(target.Slice(0, bytesWritten));
        }
Ejemplo n.º 9
0
        public void WriteFully(string testFile)
        {
            byte[] correctUncompressedBytes = File.ReadAllBytes(testFile);
            byte[] compressedBytes          = new byte[BrotliEncoder.GetMaxCompressedLength(correctUncompressedBytes.Length)];
            byte[] actualUncompressedBytes  = new byte[BrotliEncoder.GetMaxCompressedLength(correctUncompressedBytes.Length)];

            Span <byte> destination = new Span <byte>(compressedBytes);

            Assert.True(BrotliEncoder.TryCompress(correctUncompressedBytes, destination, out int bytesWritten));
            Assert.True(BrotliDecoder.TryDecompress(destination, actualUncompressedBytes, out bytesWritten));
            Assert.Equal(correctUncompressedBytes.Length, bytesWritten);

            Assert.Equal <byte>(correctUncompressedBytes, actualUncompressedBytes.AsSpan(0, correctUncompressedBytes.Length).ToArray());
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Compresses data. Uses <see cref="BrotliEncoder"/>.
        /// </summary>
        /// <param name="data">Data. See also: <see cref="MemoryMarshal.AsBytes"/>, <see cref="CollectionsMarshal.AsSpan"/>.</param>
        /// <param name="level">Compression level, 0 (no compression) to 11 (maximal compression). Default 6. Bigger levels don't make much smaller but can make much slower.</param>
        /// <exception cref="ArgumentOutOfRangeException">Invalid <i>level</i>.</exception>
        /// <exception cref="OutOfMemoryException"></exception>
        public static unsafe byte[] BrotliCompress(ReadOnlySpan <byte> data, int level = 6)
        {
            int n = BrotliEncoder.GetMaxCompressedLength(data.Length);
            var b = MemoryUtil.Alloc(n);

            try {
                if (!BrotliEncoder.TryCompress(data, new(b, n), out n, level, 22))
                {
                    throw new AuException();
                }
                return(new Span <byte>(b, n).ToArray());
            }
            finally { MemoryUtil.Free(b); }
        }
Ejemplo n.º 11
0
        private static byte[] CompressBrotli(ReadOnlySpan <byte> chunk, out int compressedLength)
        {
            compressedLength = 0;
            int maxLength = BrotliEncoder.GetMaxCompressedLength(chunk.Length);
            var buffer    = ArrayPool <byte> .Shared.Rent(maxLength);

            if (!BrotliEncoder.TryCompress(chunk, buffer, out int written, 11, 22))
            {
                ArrayPool <byte> .Shared.Return(buffer);

                return(null);
            }

            compressedLength = written;
            return(buffer);
        }
Ejemplo n.º 12
0
        private static Task WriteBrotliCompressedDynamicResponse(ReadOnlySpan <byte> input, HttpResponse response)
        {
            byte[] output    = null;
            var    arrayPool = ArrayPool <byte> .Shared;

            try
            {
                output = arrayPool.Rent(BrotliEncoder.GetMaxCompressedLength(input.Length));
                if (BrotliEncoder.TryCompress(input, output, out var bytesWritten, 4, 22))
                {
                    response.ContentLength            = bytesWritten;
                    response.Headers[ContentEncoding] = Brotli;
                    return(response.Body.WriteAsync(output, 0, bytesWritten));
                }
                else
                {
                    return(TryCompressFalse());
                }
            }
Ejemplo n.º 13
0
        public async Task FasterLog_Test1Write()
        {
            const int entryLength = 1 << 10;

            byte[] staticEntry1       = new byte[entryLength];
            byte[] staticEntry1Brotli = new byte[entryLength];
            for (int i = 0; i < entryLength; i++)
            {
                staticEntry1[i] = (byte)i;
            }
            byte[] staticEntry2       = new byte[entryLength];
            byte[] staticEntry2Brotli = new byte[entryLength];
            for (int i = 0; i < entryLength; i++)
            {
                staticEntry2[i] = (byte)(entryLength - i);
            }
            var path = GetPath();

            using (var logCommitManager = new DeviceLogCommitCheckpointManager(
                       new LocalStorageNamedDeviceFactory(),
                       new DefaultCheckpointNamingScheme(path), true, false)
                   ) {
                using (IDevice device = Devices.CreateLogDevice(path + "hlog.log")) {
                    //FasterLogScanIterator iter;
                    var logSettings = new FASTER.core.FasterLogSettings()
                    {
                        LogDevice        = device,
                        LogChecksum      = LogChecksumType.PerEntry,
                        LogCommitManager = logCommitManager,
                    };

                    using (var log = await FASTER.core.FasterLog.CreateAsync(logSettings)) {
                        //log.TruncateUntilPageStart(0);
                        //await log.CommitAsync();
                        for (int i = 0; i < 1000; i++)
                        {
                            if (BrotliEncoder.TryCompress(staticEntry1, staticEntry1Brotli, out var bytesWritten1, 4, 10))
                            {
                                await log.EnqueueAsync(new Memory <byte>(staticEntry1Brotli).Slice(0, bytesWritten1));
                            }
Ejemplo n.º 14
0
        public void TestCompressionBrotli()
        {
            var watch = new Stopwatch();

            watch.Start();

            string exeName  = Assembly.GetExecutingAssembly().Location;
            string fileName = Path.Combine(Path.GetDirectoryName(exeName), @"..\..\..\..\..\..\Data\mnist_png.zip");

            long compLength = 0;

            using var zip = ZipFile.OpenRead(fileName);
            foreach (var entry in zip.Entries)
            {
                if (string.Compare(Path.GetExtension(entry.FullName), ".png", true) != 0)
                {
                    continue;
                }

                using var stream    = entry.Open();
                using var memStream = new MemoryStream();
                stream.CopyTo(memStream);
                byte[] image = memStream.ToArray();
                Utils.ReadStreamToBuffer(stream, image);

                int    maxCompSize = BrotliEncoder.GetMaxCompressedLength(image.Length);
                byte[] compImage   = new byte[maxCompSize];
                if (!BrotliEncoder.TryCompress(new ReadOnlySpan <byte>(image), new Span <byte>(compImage), out int bytesWritten, 11, 16))
                {
                    throw new Exception("Cannot compress image");
                }

                compLength += bytesWritten;
            }

            watch.Stop();
            output.WriteLine($"Brotli: {watch.ElapsedMilliseconds:N0} msec.");
            output.WriteLine($"Summary length: {compLength:N0}");
        }
Ejemplo n.º 15
0
 public bool TryCompress(byte[] source, int sourceOffset, int sourceLength, byte[] destination, int destinationOffset, int destinationLength, out int written)
 {
     return(BrotliEncoder.TryCompress(new ReadOnlySpan <byte>(source, sourceOffset, sourceLength),
                                      new Span <byte>(destination, destinationOffset, destinationLength), out written, _quality, _window));
 }
Ejemplo n.º 16
0
 private static void Compress_WithoutState(ReadOnlySpan <byte> input, Span <byte> output)
 {
     BrotliEncoder.TryCompress(input, output, out int bytesWritten);
 }